|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import type { RealTestFunc, TestOptions } from "./index" |
| 17 | +import type { TestOptions, TestWithPathFunc } from "./index" |
18 | 18 |
|
19 | 19 | const fillHashtags = (count: number): string => "#".repeat(count) |
20 | 20 |
|
21 | 21 | /** |
22 | 22 | * Handles `$any`, `$all`, and `$inarray`. Works with nested loops! |
23 | | - * @param realTest The realTest function. |
| 23 | + * @param testWithPath The testWithPath function. |
24 | 24 | * @param input The state machine. |
25 | 25 | * @param variables The variables. |
26 | 26 | * @param op The operation being performed. |
27 | 27 | * @param options The test options. |
28 | 28 | * @internal |
29 | 29 | */ |
30 | 30 | export function handleArrayLogic<Variables>( |
31 | | - realTest: RealTestFunc, |
| 31 | + testWithPath: TestWithPathFunc, |
32 | 32 | input: any, |
33 | 33 | variables: Variables, |
34 | 34 | op: string, |
35 | | - options: TestOptions |
| 35 | + options: TestOptions, |
36 | 36 | ): boolean { |
37 | 37 | const inValue = input[op]["in"] |
38 | 38 | const depth = (options._currentLoopDepth || 0) + 1 |
39 | 39 |
|
40 | 40 | if (inValue.includes("#")) { |
41 | | - throw new TypeError("Nested array nodes cannot use current iteration (`$.#`) as an `in` value", { |
42 | | - cause: options._path |
43 | | - }) |
| 41 | + throw new TypeError( |
| 42 | + "Nested array nodes cannot use current iteration (`$.#`) as an `in` value", |
| 43 | + { |
| 44 | + cause: options._path, |
| 45 | + }, |
| 46 | + ) |
44 | 47 | } |
45 | 48 |
|
46 | 49 | // find the array |
47 | | - const array = realTest(inValue, variables, { |
48 | | - ...options, |
49 | | - _currentLoopDepth: depth, |
50 | | - _path: `${options._path}.${op}.in` |
51 | | - }) as unknown as unknown[] |
| 50 | + const array = testWithPath( |
| 51 | + inValue, |
| 52 | + variables, |
| 53 | + { |
| 54 | + ...options, |
| 55 | + _currentLoopDepth: depth, |
| 56 | + }, |
| 57 | + `${op}.in`, |
| 58 | + ) as unknown as unknown[] |
52 | 59 |
|
53 | 60 | const itemConditions = input[op]["?"] |
54 | 61 |
|
55 | 62 | for (const item of array) { |
56 | | - const test = realTest(itemConditions, variables, { |
57 | | - ...options, |
58 | | - _currentLoopDepth: depth, |
59 | | - _path: `${options._path}.${op}.?`, |
60 | | - findNamedChild(reference, variables) { |
61 | | - // NOTE: if we have a multi-layered loop, this should one-by-one fall back until the targeted loop is hit |
62 | | - const hashtags = fillHashtags(depth) |
| 63 | + const test = testWithPath( |
| 64 | + itemConditions, |
| 65 | + variables, |
| 66 | + { |
| 67 | + ...options, |
| 68 | + _currentLoopDepth: depth, |
| 69 | + findNamedChild(reference, variables) { |
| 70 | + // NOTE: if we have a multi-layered loop, this should one-by-one fall back until the targeted loop is hit |
| 71 | + const hashtags = fillHashtags(depth) |
63 | 72 |
|
64 | | - // a little future-proofing, as sometimes the $ is there, and other times it isn't. |
65 | | - // we strip it out somewhere, but it shouldn't matter too much. |
66 | | - if ( |
67 | | - reference === `$.${hashtags}` || |
68 | | - reference === `.${hashtags}` |
69 | | - ) { |
70 | | - return item |
71 | | - } |
| 73 | + // a little future-proofing, as sometimes the $ is there, and other times it isn't. |
| 74 | + // we strip it out somewhere, but it shouldn't matter too much. |
| 75 | + if ( |
| 76 | + reference === `$.${hashtags}` || |
| 77 | + reference === `.${hashtags}` |
| 78 | + ) { |
| 79 | + return item |
| 80 | + } |
72 | 81 |
|
73 | | - // handle properties of an object |
74 | | - if (typeof item === "object") { |
75 | | - const newReference = `$${reference.substring( |
76 | | - reference.indexOf("#.") + 1 |
77 | | - )}` |
78 | | - const found = options.findNamedChild(newReference, item) |
79 | | - if (found !== newReference) return found |
80 | | - } |
| 82 | + // handle properties of an object |
| 83 | + if (typeof item === "object") { |
| 84 | + const newReference = `$${reference.substring( |
| 85 | + reference.indexOf("#.") + 1, |
| 86 | + )}` |
| 87 | + const found = options.findNamedChild(newReference, item) |
| 88 | + if (found !== newReference) return found |
| 89 | + } |
81 | 90 |
|
82 | | - return options.findNamedChild(reference, variables) |
83 | | - } |
84 | | - }) |
| 91 | + return options.findNamedChild(reference, variables) |
| 92 | + }, |
| 93 | + }, |
| 94 | + `${op}.?`, |
| 95 | + ) |
85 | 96 |
|
86 | 97 | if (test && (op === "$inarray" || op === "$any")) { |
87 | 98 | return true |
|
0 commit comments