Skip to content

Commit 4f69726

Browse files
author
Sascha Goldhofer
committed
chore: cleanup
1 parent 81ecedb commit 4f69726

23 files changed

+169
-2905
lines changed

.npmignore

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/test/
2-
/dev/
3-
/release/
4-
/docs/
5-
/test/
6-
.*
71
*.dat
82
*.gz
93
*.log
@@ -12,11 +6,16 @@
126
*.sublime*
137
*.tmp
148
*.zip
9+
.*
1510
.DS_Store
1611
.editorconfig
1712
.eslintrc
1813
.gitignore
1914
.nyc_output
15+
/docs/
16+
/lib/
17+
/test/
18+
/test/
2019
build
2120
dev
2221
docs
@@ -25,3 +24,4 @@ test
2524
Thumbs.db
2625
tsconfig.json
2726
webpack.config.js
27+
yarn-error.log

dist/index.d.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import get, { ReturnType, ResultCallback } from "./lib/get";
2-
import set from "./lib/set";
3-
import split from "./lib/split";
4-
import remove from "./lib/remove";
1+
import { get, ReturnType, ResultCallback } from "./lib/get";
2+
import { set } from "./lib/set";
3+
import { split } from "./lib/split";
4+
import { remove } from "./lib/remove";
55
export { get, set, split, remove, ReturnType };
66
declare const _default: {
77
get: typeof get;
@@ -12,4 +12,4 @@ declare const _default: {
1212
};
1313
export default _default;
1414
export type { ResultCallback };
15-
export type { Input, JSONPointer, QueryResult } from "./lib/types";
15+
export type { Input, JsonPointer, QueryResult } from "./lib/types";

dist/gsonQuery.js renamed to dist/jsonQuery.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

dist/lib/get.d.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Input, JSONPointer } from "./types";
1+
import { Input, JsonPointer } from "./types";
22
export declare enum ReturnType {
33
POINTER = "pointer",
44
VALUE = "value",
@@ -7,18 +7,17 @@ export declare enum ReturnType {
77
}
88
export type ResultCallback = (value: any, property: string | null, parent: {
99
[p: string]: any;
10-
} | Array<any> | null, pointer: JSONPointer) => any;
10+
} | Array<any> | null, pointer: JsonPointer) => any;
1111
/**
1212
* Runs query on input data and returns the results
1313
* @param data - input data
14-
* @param queryString - gson-query string
14+
* @param queryString - json-query string
1515
* @param returnType - result format or a custom callback
1616
*/
17-
declare function get(data: Input, queryString: string, returnType?: ReturnType | ResultCallback): any;
18-
declare namespace get {
17+
export declare function get(data: Input, queryString: string, returnType?: ReturnType | ResultCallback): any;
18+
export declare namespace get {
1919
var POINTER: ReturnType;
2020
var VALUE: ReturnType;
2121
var ALL: ReturnType;
2222
var MAP: ReturnType;
2323
}
24-
export default get;

dist/lib/interpreter/index.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { VALUE_INDEX, KEY_INDEX, PARENT_INDEX, POINTER_INDEX } from "./keys";
22
import { QueryResult, Input } from "../types";
33
import { IToken } from "ebnf";
4-
export default function run(data: Input, ast: IToken): Array<QueryResult>;
5-
export { run, VALUE_INDEX, KEY_INDEX, PARENT_INDEX, POINTER_INDEX };
4+
export declare function run(data: Input, ast: IToken): Array<QueryResult>;
5+
export { VALUE_INDEX, KEY_INDEX, PARENT_INDEX, POINTER_INDEX };

dist/lib/interpreter/nodes.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ declare const cache: {
55
reset(): void;
66
};
77
declare const expand: {
8-
any(node: IToken, entry: any): any;
8+
any(node: IToken, entry: any): any[][];
99
all(node: IToken, entry: any): any[];
10-
regex(node: IToken, entry: any): any;
10+
regex(node: IToken, entry: any): any[][];
1111
};
1212
declare const select: {
1313
escaped: (node: IToken, entry: any) => any[];

dist/lib/parser/grammar.d.ts

-3
This file was deleted.

dist/lib/parser/jsonQueryGrammar.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export declare const propertyRegex = "[^?/{}*,()#]+";
2+
export declare const jsonQueryGrammar: string;

dist/lib/remove.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Runs query on input data and removes matching properties from results
33
* @param data - input data
4-
* @param queryString - gson-query string
4+
* @param queryString - json-query string
55
* @param [returnRemoved] - if true, will returned removed properties, else input-data is removed
66
*/
7-
export default function queryRemove(data: any, queryString: any, returnRemoved?: boolean): any;
7+
export declare function remove(data: any, queryString: any, returnRemoved?: boolean): any;

dist/lib/set.d.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ export declare enum InsertMode {
66
/**
77
* Runs query on input data and assigns a value to query-results.
88
* @param data - input data
9-
* @param queryString - gson-query string
9+
* @param queryString - json-query string
1010
* @param value - value to assign
1111
* @param [force] - whether to replace or insert into arrays
1212
*/
13-
declare function set<T extends Input>(data: T, queryString: string, value: any, force?: InsertMode): T;
14-
declare namespace set {
13+
export declare function set<T extends Input>(data: T, queryString: string, value: any, force?: InsertMode): T;
14+
export declare namespace set {
1515
var REPLACE_ITEMS: InsertMode;
1616
var INSERT_ITEMS: InsertMode;
1717
}
18-
export default set;

dist/lib/split.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/**
22
* Returns queryString as a list of property-queries
33
*/
4-
export default function split(queryString?: string): Array<string>;
4+
export declare function split(queryString?: string): Array<string>;

dist/lib/types.d.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
export type Input = {
22
[p: string]: any;
33
} | Array<any>;
4-
export type JSONPointer = string;
5-
export type QueryResult = [any, string | null, {
6-
[p: string]: any;
7-
} | Array<any> | null, JSONPointer];
4+
export type JsonPointer = string;
5+
export type QueryResult = [
6+
any,
7+
string | null,
8+
{
9+
[p: string]: any;
10+
} | Array<any> | null,
11+
JsonPointer
12+
];

dist/module/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import get, { ReturnType } from "./lib/get";
2-
import set from "./lib/set";
3-
import split from "./lib/split";
4-
import remove from "./lib/remove";
1+
import { get, ReturnType } from "./lib/get";
2+
import { set } from "./lib/set";
3+
import { split } from "./lib/split";
4+
import { remove } from "./lib/remove";
55
export { get, set, split, remove, ReturnType };
66
export default { get, set, split, remove, ReturnType };

dist/module/lib/get.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { parse } from "./parser";
22
import { run, VALUE_INDEX, POINTER_INDEX } from "./interpreter";
33
const returnTypes = {
4-
value: r => r.map(e => e[VALUE_INDEX]),
5-
pointer: r => r.map(e => e[POINTER_INDEX]),
6-
all: r => r,
7-
map: r => {
4+
value: (r) => r.map((e) => e[VALUE_INDEX]),
5+
pointer: (r) => r.map((e) => e[POINTER_INDEX]),
6+
all: (r) => r,
7+
map: (r) => {
88
const map = {};
9-
r.forEach(e => (map[e[POINTER_INDEX]] = e[VALUE_INDEX]));
9+
r.forEach((e) => (map[e[POINTER_INDEX]] = e[VALUE_INDEX]));
1010
return map;
11-
}
11+
},
1212
};
1313
export var ReturnType;
1414
(function (ReturnType) {
@@ -25,10 +25,10 @@ get.MAP = ReturnType.MAP;
2525
/**
2626
* Runs query on input data and returns the results
2727
* @param data - input data
28-
* @param queryString - gson-query string
28+
* @param queryString - json-query string
2929
* @param returnType - result format or a custom callback
3030
*/
31-
export default function get(data, queryString, returnType = ReturnType.VALUE) {
31+
export function get(data, queryString, returnType = ReturnType.VALUE) {
3232
if (queryString == null) {
3333
return [];
3434
}

dist/module/lib/interpreter/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function reduce(func, input, node, pointer) {
1919
}
2020
function query(data, ast, pointer) {
2121
let result = data;
22-
ast.children.forEach(node => {
22+
ast.children.forEach((node) => {
2323
if (expand[node.type]) {
2424
result = collect(expand[node.type], result, node, pointer);
2525
}
@@ -35,7 +35,7 @@ function query(data, ast, pointer) {
3535
function runPatternOnce(inputSet, ast, pointer) {
3636
const resultingSet = [];
3737
let workingSet = inputSet;
38-
ast.children.forEach(node => {
38+
ast.children.forEach((node) => {
3939
if (node.type === "orPattern") {
4040
resultingSet.push(...workingSet);
4141
workingSet = inputSet;
@@ -58,7 +58,7 @@ function getIterationCount(quantifier) {
5858
}
5959
function pattern(data, ast, pointer) {
6060
const result = [];
61-
const quantifier = ast.children.find(node => node.type === "quantifier");
61+
const quantifier = ast.children.find((node) => node.type === "quantifier");
6262
const iterationCount = getIterationCount(quantifier && quantifier.text);
6363
let workingSet = data;
6464
if (quantifier && quantifier.text === "*") {
@@ -74,7 +74,7 @@ function pattern(data, ast, pointer) {
7474
}
7575
function skip(data, ast, pointer) {
7676
let result = data;
77-
ast.children.forEach(n => (result = runNode(result, n, pointer)));
77+
ast.children.forEach((n) => (result = runNode(result, n, pointer)));
7878
return result;
7979
}
8080
function runNode(data, ast, pointer) {
@@ -93,9 +93,9 @@ function runNode(data, ast, pointer) {
9393
cache.mem.push(data);
9494
return result;
9595
}
96-
export default function run(data, ast) {
96+
export function run(data, ast) {
9797
cache.reset();
9898
cache.mem.push(data);
9999
return runNode([[data, null, null, "#"]], ast);
100100
}
101-
export { run, VALUE_INDEX, KEY_INDEX, PARENT_INDEX, POINTER_INDEX };
101+
export { VALUE_INDEX, KEY_INDEX, PARENT_INDEX, POINTER_INDEX };

dist/module/lib/interpreter/nodes.js

+60-16
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
1-
import o from "gson-conform";
21
const join = (a, b) => `${a}/${b}`;
32
import { VALUE_INDEX, POINTER_INDEX } from "./keys";
43
const toString = Object.prototype.toString;
54
const rContainer = /Object|Array/;
6-
const isContainer = v => rContainer.test(toString.call(v));
7-
const getTypeOf = v => toString.call(v).match(/\s([^\]]+)\]/).pop().toLowerCase();
5+
const isContainer = (v) => rContainer.test(toString.call(v));
6+
const getTypeOf = (v) => toString
7+
.call(v)
8+
.match(/\s([^\]]+)\]/)
9+
.pop()
10+
.toLowerCase();
811
function nodeAsRegex(node) {
912
return new RegExp(node.text.replace(/(^{|}$)/g, ""));
1013
}
14+
/**
15+
* Iterates over object or array, passing each key, value and parentObject to the callback
16+
* @param value - to iterate
17+
* @param callback - receiving key on given input value
18+
*/
19+
function forEach(parent, callback) {
20+
if (Array.isArray(parent)) {
21+
parent.forEach(callback);
22+
}
23+
else if (Object.prototype.toString.call(parent) === "[object Object]") {
24+
Object.keys(parent).forEach(function (key) {
25+
callback(parent[key], key, parent);
26+
});
27+
}
28+
}
29+
/**
30+
* Returns all keys of the given input data
31+
* @param value
32+
* @return {Array} containing keys of given value
33+
*/
34+
function getKeys(value) {
35+
if (Array.isArray(value)) {
36+
return value.map(function (value, index) {
37+
return `${index}`;
38+
});
39+
}
40+
if (Object.prototype.toString.call(value) === "[object Object]") {
41+
return Object.keys(value);
42+
}
43+
return [];
44+
}
1145
const cache = {
1246
mem: [],
1347
get(entry, prop) {
@@ -22,18 +56,23 @@ const cache = {
2256
},
2357
reset() {
2458
cache.mem.length = 0;
25-
}
59+
},
2660
};
2761
const expand = {
2862
any(node, entry) {
2963
const value = entry[VALUE_INDEX];
30-
return o.keys(value)
64+
return (getKeys(value)
3165
// .map(prop => cache.get(entry, prop));
32-
.map(prop => [value[prop], prop, value, join(entry[POINTER_INDEX], prop)]);
66+
.map((prop) => [
67+
value[prop],
68+
prop,
69+
value,
70+
join(entry[POINTER_INDEX], prop),
71+
]));
3372
},
3473
all(node, entry) {
3574
const result = [entry];
36-
o.forEach(entry[VALUE_INDEX], (value, prop) => {
75+
forEach(entry[VALUE_INDEX], (value, prop) => {
3776
const childEntry = cache.get(entry, prop);
3877
// const childEntry = [value, prop, entry[VALUE_INDEX], join(entry[POINTER_INDEX], prop)];
3978
childEntry && result.push(...expand.all(node, childEntry));
@@ -43,10 +82,15 @@ const expand = {
4382
regex(node, entry) {
4483
const regex = nodeAsRegex(node);
4584
const value = entry[VALUE_INDEX];
46-
return o.keys(value)
47-
.filter(prop => regex.test(prop))
48-
.map(prop => [value[prop], prop, value, join(entry[POINTER_INDEX], prop)]);
49-
}
85+
return getKeys(value)
86+
.filter((prop) => regex.test(prop))
87+
.map((prop) => [
88+
value[prop],
89+
prop,
90+
value,
91+
join(entry[POINTER_INDEX], prop),
92+
]);
93+
},
5094
};
5195
const select = {
5296
// alias to property (but escaped)
@@ -58,7 +102,7 @@ const select = {
58102
entry[VALUE_INDEX][prop],
59103
prop,
60104
entry[VALUE_INDEX],
61-
join(entry[POINTER_INDEX], prop)
105+
join(entry[POINTER_INDEX], prop),
62106
];
63107
}
64108
},
@@ -75,10 +119,10 @@ const select = {
75119
lookahead: (node, entry) => {
76120
let valid = true;
77121
let or = false;
78-
node.children.forEach(expr => {
122+
node.children.forEach((expr) => {
79123
if (expr.type === "expression") {
80124
const isValid = select.expression(expr, entry) !== undefined;
81-
valid = or === true ? (valid || isValid) : valid && isValid;
125+
valid = or === true ? valid || isValid : valid && isValid;
82126
}
83127
else {
84128
or = expr.type === "orExpr";
@@ -95,14 +139,14 @@ const select = {
95139
return undefined;
96140
}
97141
return expressionMatches(value[prop], cmp, test) ? entry : undefined;
98-
}
142+
},
99143
};
100144
function expressionMatches(value, cmp, test) {
101145
if (cmp === undefined) {
102146
return value !== undefined;
103147
}
104148
let valid;
105-
const valueString = "" + value;
149+
const valueString = `${value}`;
106150
if (test.type === "regex") {
107151
const regex = nodeAsRegex(test);
108152
valid = regex.test(valueString);

0 commit comments

Comments
 (0)