Skip to content

Commit 915f50f

Browse files
author
Sascha Goldhofer
committed
Refactor gson-query to typescript
1 parent c099143 commit 915f50f

39 files changed

+440
-1203
lines changed

.eslintrc

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
{
2+
"extends": [
3+
"eslint:recommended",
4+
"plugin:@typescript-eslint/eslint-recommended",
5+
"plugin:@typescript-eslint/recommended"
6+
],
27
"parser": "@typescript-eslint/parser",
38
"plugins": [
4-
"@typescript-eslint"
9+
"@typescript-eslint",
10+
"array-func",
11+
"node",
12+
"optimize-regex",
13+
"promise"
514
],
615

716
"parserOptions": {
8-
"ecmaVersion": 6,
17+
"ecmaVersion": 2018,
918
"sourceType": "module"
1019
},
1120

1221
"env": {
1322
"es6": true,
14-
"node": true
23+
"node": true,
24+
"mocha": true
1525
},
1626

1727
"settings": {
@@ -24,10 +34,10 @@
2434
}
2535
},
2636

27-
"extends": "eslint:recommended",
2837
"rules": {
29-
"semi": ["error", "always"],
30-
"prefer-const": "error",
31-
"no-var": "error"
38+
"@typescript-eslint/explicit-function-return-type": "off",
39+
"@typescript-eslint/explicit-module-boundary-types": "off",
40+
"@typescript-eslint/ban-ts-ignore": "off",
41+
"@typescript-eslint/no-explicit-any": "off"
3242
}
3343
}

.gitignore

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
node_modules
2-
status.png
3-
build
4-
bower_components
5-
*.sublime-workspace
61
*.sublime-project
7-
/.nyc_output
2+
*.sublime-workspace
83
.DS_Store
4+
/.nyc_output
5+
/dev
6+
/node_modules
7+
/types

dist/gson-query.js

-2
This file was deleted.

dist/gson-query.js.LICENSE.txt

-1
This file was deleted.

lib/get.d.ts

-2
This file was deleted.

lib/get.js renamed to lib/get.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const { parse } = require("./parser");
2-
const { run, VALUE_INDEX, POINTER_INDEX } = require("./interpreter");
1+
import { parse } from "./parser";
2+
import { run, VALUE_INDEX, POINTER_INDEX } from "./interpreter";
3+
import { Input } from "./types";
34

45

56
const returnTypes = {
@@ -15,7 +16,14 @@ const returnTypes = {
1516

1617
Object.keys(returnTypes).forEach(prop => (get[prop.toUpperCase()] = prop));
1718

18-
function get(data, queryString, returnType = "value") {
19+
export enum ReturnType {
20+
POINTER = "pointer",
21+
VALUE = "value",
22+
ALL = "all",
23+
MAP = "map"
24+
}
25+
26+
export default function get(data: Input, queryString: string, returnType: ReturnType|Function = ReturnType.VALUE) {
1927
if (queryString == null) {
2028
return [];
2129
}
@@ -34,14 +42,11 @@ function get(data, queryString, returnType = "value") {
3442
}
3543

3644
const result = run(data, ast);
37-
if (returnTypes[returnType]) {
38-
return returnTypes[returnType](result);
39-
} else if (typeof returnType === "function") {
45+
if (typeof returnType === "function") {
4046
return result.map(r => returnType(...r));
47+
} else if (returnTypes[returnType]) {
48+
return returnTypes[returnType](result);
4149
}
4250

4351
return result;
4452
}
45-
46-
47-
module.exports = get;

lib/index.d.ts

-75
This file was deleted.

lib/index.js

-4
This file was deleted.

lib/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export { default as get } from "./get";
2+
export { default as set } from "./set";
3+
export { default as split } from "./split";
4+
export { default as remove } from "./remove";

lib/interpreter/index.js renamed to lib/interpreter/index.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { expand, select, cache } = require("./nodes");
2-
const { VALUE_INDEX, KEY_INDEX, PARENT_INDEX, POINTER_INDEX } = require("./keys");
1+
import { expand, select, cache } from "./nodes";
2+
import { VALUE_INDEX, KEY_INDEX, PARENT_INDEX, POINTER_INDEX } from "./keys";
33

44

55
function collect(func, input, node, pointer) {
@@ -92,7 +92,7 @@ function skip(data, ast, pointer) {
9292
}
9393

9494

95-
function runNode(data, ast, pointer) {
95+
function runNode(data, ast, pointer?) {
9696
let result;
9797
if (ast.type === "query") {
9898
result = query(data, ast, pointer);
@@ -108,14 +108,10 @@ function runNode(data, ast, pointer) {
108108
}
109109

110110

111-
module.exports = {
112-
VALUE_INDEX,
113-
KEY_INDEX,
114-
PARENT_INDEX,
115-
POINTER_INDEX,
116-
run(data, ast) {
117-
cache.reset();
118-
cache.mem.push(data);
119-
return runNode([[data, null, null, "#"]], ast);
120-
}
121-
};
111+
export default function run(data, ast) {
112+
cache.reset();
113+
cache.mem.push(data);
114+
return runNode([[data, null, null, "#"]], ast);
115+
}
116+
117+
export { run, VALUE_INDEX, KEY_INDEX, PARENT_INDEX, POINTER_INDEX };

lib/interpreter/keys.js

-6
This file was deleted.

lib/interpreter/keys.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const VALUE_INDEX = 0;
2+
export const KEY_INDEX = 1;
3+
export const PARENT_INDEX = 2;
4+
export const POINTER_INDEX = 3;

lib/interpreter/nodes.js renamed to lib/interpreter/nodes.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const o = require("gson-conform");
1+
import o from "gson-conform";
22
const join = (a, b) => `${a}/${b}`;
3-
const { VALUE_INDEX, POINTER_INDEX } = require("./keys");
3+
import { VALUE_INDEX, POINTER_INDEX } from "./keys";
44

55
const toString = Object.prototype.toString;
66
const rContainer = /Object|Array/;
@@ -138,8 +138,4 @@ function expressionMatches(value, cmp, test) {
138138
}
139139

140140

141-
module.exports = {
142-
expand,
143-
select,
144-
cache
145-
};
141+
export { expand, select, cache };

lib/parser/grammar.js renamed to lib/parser/grammar.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const propertyRegex = "[^?/{}*,()#]+";
1+
export const propertyRegex = "[^?/{}*,()#]+";
22

33
// W3C ENBF grammar
44
// https://github.com/lys-lang/node-ebnf/blob/master/test/W3CEBNF.spec.ts
55
// https://www.w3.org/TR/xml/#sec-notation
6-
const enbf = `
6+
export default `
77
root ::= ("#" recursion | recursion | (query | pattern) recursion* | "#" SEP? | SEP)
88
recursion ::= (SEP query | pattern)*
99
@@ -32,9 +32,3 @@ orPattern ::= S? "," S?
3232
3333
S ::= [ ]*
3434
`;
35-
36-
37-
module.exports = {
38-
enbf,
39-
propertyRegex
40-
};
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { default: EBNF } = require("ebnf/dist/Grammars/W3CEBNF");
2-
const { enbf } = require("./grammar");
1+
import { default as EBNF } from "ebnf/dist/Grammars/W3CEBNF";
2+
import enbf from "./grammar";
33

44
const valid = /(children|text|type|start|end|rest|errors|fullText|\d+)/;
55
const subset = /(children|text|type|\d+)/;
@@ -9,8 +9,6 @@ const toSmallJSON = (ast) => JSON.stringify(ast, (key, value) => (key === "" ||
99
const parser = new EBNF.Parser(enbf);
1010

1111

12-
module.exports = {
13-
parse: (query) => parser.getAST(query),
14-
reduce: (ast) => JSON.parse(toSmallJSON(ast)),
15-
toJSON
16-
};
12+
export const parse = (query) => parser.getAST(query);
13+
export const reduce = (ast) => JSON.parse(toSmallJSON(ast));
14+
export { toJSON };

lib/remove.d.ts

-2
This file was deleted.

lib/remove.js

-23
This file was deleted.

lib/remove.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pointerDelete from "gson-pointer/lib/delete";
2+
import removeUndefinedItems from "gson-pointer/lib/removeUndefinedItems";
3+
import get, { ReturnType } from "./get";
4+
import { PARENT_INDEX, POINTER_INDEX } from "./interpreter/keys";
5+
6+
7+
export default function queryRemove(input, jsonPointer, returnRemoved = false) {
8+
const removed = [];
9+
const matches = get(input, jsonPointer, ReturnType.ALL);
10+
matches.forEach(function (match) {
11+
removed.push(match[0]);
12+
pointerDelete(input, match[POINTER_INDEX], true);
13+
});
14+
matches.forEach(function (match) {
15+
if (Array.isArray(match[PARENT_INDEX])) {
16+
removeUndefinedItems(match[PARENT_INDEX]);
17+
}
18+
});
19+
return returnRemoved ? removed : input;
20+
}

lib/set.d.ts

-2
This file was deleted.

0 commit comments

Comments
 (0)