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

Lines changed: 17 additions & 7 deletions
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

Lines changed: 5 additions & 6 deletions
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

Lines changed: 0 additions & 2 deletions
This file was deleted.

dist/gson-query.js.LICENSE.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/get.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

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

Lines changed: 14 additions & 9 deletions
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

Lines changed: 0 additions & 75 deletions
This file was deleted.

lib/index.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/index.ts

Lines changed: 4 additions & 0 deletions
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

Lines changed: 10 additions & 14 deletions
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 };

0 commit comments

Comments
 (0)