1
1
import { expand , select , cache } from "./nodes" ;
2
2
import { VALUE_INDEX , KEY_INDEX , PARENT_INDEX , POINTER_INDEX } from "./keys" ;
3
+ import { QueryResult , Input , JSONPointer } from "../types" ;
3
4
5
+ type WorkingSet = Array < QueryResult > ;
4
6
5
- function collect ( func , input , node , pointer ) {
7
+
8
+ function collect ( func , input : WorkingSet , node , pointer : JSONPointer ) {
6
9
const result = [ ] ;
7
10
for ( let i = 0 , l = input . length ; i < l ; i += 1 ) {
8
11
result . push ( ...func ( node , input [ i ] , node , pointer ) ) ;
@@ -11,7 +14,7 @@ function collect(func, input, node, pointer) {
11
14
}
12
15
13
16
14
- function reduce ( func , input , node , pointer ) {
17
+ function reduce ( func , input , node , pointer : JSONPointer ) {
15
18
const result = [ ] ;
16
19
for ( let i = 0 , l = input . length ; i < l ; i += 1 ) {
17
20
const output = func ( node , input [ i ] , pointer ) ;
@@ -23,7 +26,7 @@ function reduce(func, input, node, pointer) {
23
26
}
24
27
25
28
26
- function query ( data , ast , pointer ) {
29
+ function query ( data : WorkingSet , ast , pointer : JSONPointer ) : WorkingSet {
27
30
let result = data ;
28
31
ast . children . forEach ( node => {
29
32
if ( expand [ node . type ] ) {
@@ -40,7 +43,7 @@ function query(data, ast, pointer) {
40
43
}
41
44
42
45
43
- function runPatternOnce ( inputSet , ast , pointer ) {
46
+ function runPatternOnce ( inputSet : WorkingSet , ast , pointer : JSONPointer ) : WorkingSet {
44
47
const resultingSet = [ ] ;
45
48
let workingSet = inputSet ;
46
49
ast . children . forEach ( node => {
@@ -55,19 +58,19 @@ function runPatternOnce(inputSet, ast, pointer) {
55
58
return resultingSet ;
56
59
}
57
60
58
- function getIterationCount ( quantifier ) {
61
+ function getIterationCount ( quantifier ?: string ) {
59
62
if ( quantifier == null ) {
60
63
return 1 ; // default, simple group
61
64
}
62
65
if ( quantifier === "*" || quantifier === "+" ) {
63
66
return Infinity ;
64
67
}
65
- quantifier = parseInt ( quantifier ) ;
66
- return isNaN ( quantifier ) ? 1 : quantifier ;
68
+ const count = parseInt ( quantifier ) ;
69
+ return isNaN ( count ) ? 1 : count ;
67
70
}
68
71
69
72
70
- function pattern ( data , ast , pointer ) {
73
+ function pattern ( data , ast , pointer : JSONPointer ) {
71
74
const result = [ ] ;
72
75
const quantifier = ast . children . find ( node => node . type === "quantifier" ) ;
73
76
const iterationCount = getIterationCount ( quantifier && quantifier . text ) ;
@@ -85,14 +88,14 @@ function pattern(data, ast, pointer) {
85
88
}
86
89
87
90
88
- function skip ( data , ast , pointer ) {
91
+ function skip ( data : WorkingSet , ast , pointer : JSONPointer ) {
89
92
let result = data ;
90
93
ast . children . forEach ( n => ( result = runNode ( result , n , pointer ) ) ) ;
91
94
return result ;
92
95
}
93
96
94
97
95
- function runNode ( data , ast , pointer ?) {
98
+ function runNode ( data : WorkingSet , ast , pointer ?: JSONPointer ) : WorkingSet {
96
99
let result ;
97
100
if ( ast . type === "query" ) {
98
101
result = query ( data , ast , pointer ) ;
@@ -108,7 +111,7 @@ function runNode(data, ast, pointer?) {
108
111
}
109
112
110
113
111
- export default function run ( data , ast ) {
114
+ export default function run ( data : Input , ast ) : Array < QueryResult > {
112
115
cache . reset ( ) ;
113
116
cache . mem . push ( data ) ;
114
117
return runNode ( [ [ data , null , null , "#" ] ] , ast ) ;
0 commit comments