Skip to content

Commit d7776b0

Browse files
author
Sascha Goldhofer
committed
Fix lint issues
1 parent 915f50f commit d7776b0

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

lib/get.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parse } from "./parser";
22
import { run, VALUE_INDEX, POINTER_INDEX } from "./interpreter";
3-
import { Input } from "./types";
3+
import { Input, JSONPointer, QueryResult } from "./types";
44

55

66
const returnTypes = {
@@ -23,7 +23,9 @@ export enum ReturnType {
2323
MAP = "map"
2424
}
2525

26-
export default function get(data: Input, queryString: string, returnType: ReturnType|Function = ReturnType.VALUE) {
26+
export type ResultCallback = (value: any, property: string|null, parent: { [p: string]: any }|Array<any>|null, pointer: JSONPointer) => any;
27+
28+
export default function get(data: Input, queryString: string, returnType: ReturnType|ResultCallback = ReturnType.VALUE) {
2729
if (queryString == null) {
2830
return [];
2931
}
@@ -43,7 +45,7 @@ export default function get(data: Input, queryString: string, returnType: Return
4345

4446
const result = run(data, ast);
4547
if (typeof returnType === "function") {
46-
return result.map(r => returnType(...r));
48+
return result.map((r: QueryResult) => returnType(...r));
4749
} else if (returnTypes[returnType]) {
4850
return returnTypes[returnType](result);
4951
}

lib/interpreter/index.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { expand, select, cache } from "./nodes";
22
import { VALUE_INDEX, KEY_INDEX, PARENT_INDEX, POINTER_INDEX } from "./keys";
3+
import { QueryResult, Input, JSONPointer } from "../types";
34

5+
type WorkingSet = Array<QueryResult>;
46

5-
function collect(func, input, node, pointer) {
7+
8+
function collect(func, input: WorkingSet, node, pointer: JSONPointer) {
69
const result = [];
710
for (let i = 0, l = input.length; i < l; i += 1) {
811
result.push(...func(node, input[i], node, pointer));
@@ -11,7 +14,7 @@ function collect(func, input, node, pointer) {
1114
}
1215

1316

14-
function reduce(func, input, node, pointer) {
17+
function reduce(func, input, node, pointer: JSONPointer) {
1518
const result = [];
1619
for (let i = 0, l = input.length; i < l; i += 1) {
1720
const output = func(node, input[i], pointer);
@@ -23,7 +26,7 @@ function reduce(func, input, node, pointer) {
2326
}
2427

2528

26-
function query(data, ast, pointer) {
29+
function query(data: WorkingSet, ast, pointer: JSONPointer): WorkingSet {
2730
let result = data;
2831
ast.children.forEach(node => {
2932
if (expand[node.type]) {
@@ -40,7 +43,7 @@ function query(data, ast, pointer) {
4043
}
4144

4245

43-
function runPatternOnce(inputSet, ast, pointer) {
46+
function runPatternOnce(inputSet: WorkingSet, ast, pointer: JSONPointer): WorkingSet {
4447
const resultingSet = [];
4548
let workingSet = inputSet;
4649
ast.children.forEach(node => {
@@ -55,19 +58,19 @@ function runPatternOnce(inputSet, ast, pointer) {
5558
return resultingSet;
5659
}
5760

58-
function getIterationCount(quantifier) {
61+
function getIterationCount(quantifier?: string) {
5962
if (quantifier == null) {
6063
return 1; // default, simple group
6164
}
6265
if (quantifier === "*" || quantifier === "+") {
6366
return Infinity;
6467
}
65-
quantifier = parseInt(quantifier);
66-
return isNaN(quantifier) ? 1 : quantifier;
68+
const count = parseInt(quantifier);
69+
return isNaN(count) ? 1 : count;
6770
}
6871

6972

70-
function pattern(data, ast, pointer) {
73+
function pattern(data, ast, pointer: JSONPointer) {
7174
const result = [];
7275
const quantifier = ast.children.find(node => node.type === "quantifier");
7376
const iterationCount = getIterationCount(quantifier && quantifier.text);
@@ -85,14 +88,14 @@ function pattern(data, ast, pointer) {
8588
}
8689

8790

88-
function skip(data, ast, pointer) {
91+
function skip(data: WorkingSet, ast, pointer: JSONPointer) {
8992
let result = data;
9093
ast.children.forEach(n => (result = runNode(result, n, pointer)));
9194
return result;
9295
}
9396

9497

95-
function runNode(data, ast, pointer?) {
98+
function runNode(data: WorkingSet, ast, pointer?: JSONPointer): WorkingSet {
9699
let result;
97100
if (ast.type === "query") {
98101
result = query(data, ast, pointer);
@@ -108,7 +111,7 @@ function runNode(data, ast, pointer?) {
108111
}
109112

110113

111-
export default function run(data, ast) {
114+
export default function run(data: Input, ast): Array<QueryResult> {
112115
cache.reset();
113116
cache.mem.push(data);
114117
return runNode([[data, null, null, "#"]], ast);

test/unit/get.filter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint object-property-newline: 0 */
1+
/* eslint object-property-newline: "off", @typescript-eslint/ban-ts-comment: "off" */
22
import "mocha";
33
import { expect } from "chai";
44
import get from "../../lib/get";

test/unit/get.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint object-property-newline: 0 */
1+
/* eslint object-property-newline: "off", @typescript-eslint/ban-ts-comment: "off" */
22
import "mocha";
33
import { expect } from "chai";
44
import get, { ReturnType } from "../../lib/get";

0 commit comments

Comments
 (0)