Skip to content

Commit 95adac6

Browse files
committed
Improve val testing
1 parent 0df7807 commit 95adac6

File tree

7 files changed

+724
-131
lines changed

7 files changed

+724
-131
lines changed

defaultMethods.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,17 @@ const defaultMethods = {
269269
},
270270
// Adding this to spec something out, not to merge it quite yet
271271
val: {
272-
method: (args, context, above) => {
272+
method: (args, context, above, engine) => {
273273
if (Array.isArray(args) && args.length === 1) args = args[0]
274+
// A unary optimization
274275
if (!Array.isArray(args)) {
275-
if (args === null || args === undefined) return context
276276
const result = context[args]
277277
if (typeof result === 'undefined') return null
278278
return result
279279
}
280280
let result = context
281281
let start = 0
282+
// This block handles scope traversal
282283
if (Array.isArray(args[0]) && args[0].length === 1) {
283284
start++
284285
const climb = +Math.abs(args[0][0])
@@ -292,12 +293,13 @@ const defaultMethods = {
292293
}
293294
}
294295
}
296+
// This block handles traversing the path
295297
for (let i = start; i < args.length; i++) {
296-
if (args[i] === null) continue
297298
if (result === null || result === undefined) return null
298299
result = result[args[i]]
299300
}
300301
if (typeof result === 'undefined') return null
302+
if (typeof result === 'function' && !engine.allowFunctions) return null
301303
return result
302304
},
303305
optimizeUnary: true,
@@ -310,9 +312,14 @@ const defaultMethods = {
310312
},
311313
compile: (data, buildState) => {
312314
function wrapNull (data) {
313-
if (!chainingSupported) return buildState.compile`(((a) => a === null || a === undefined ? null : a)(${data}))`
314-
return buildState.compile`(${data} ?? null)`
315+
if (!chainingSupported) return buildState.compile`(methods.preventFunctions(((a) => a === null || a === undefined ? null : a)(${data})))`
316+
return buildState.compile`(methods.preventFunctions(${data} ?? null))`
315317
}
318+
319+
if (!buildState.engine.allowFunctions) buildState.methods.preventFunctions = a => typeof a === 'function' ? null : a
320+
else buildState.methods.preventFunctions = a => a
321+
322+
if (typeof data === 'object' && !Array.isArray(data)) return false
316323
if (Array.isArray(data) && Array.isArray(data[0])) {
317324
// A very, very specific optimization.
318325
if (buildState.iteratorCompile && Math.abs(data[0][0] || 0) === 1 && data[1] === 'index') return buildState.compile`index`
@@ -445,7 +452,7 @@ const defaultMethods = {
445452
buildState.methods.push(mapper)
446453
if (async) {
447454
if (!isSync(mapper) || selector.includes('await')) {
448-
buildState.detectAsync = true
455+
buildState.asyncDetected = true
449456
if (typeof defaultValue !== 'undefined') {
450457
return `await asyncIterators.reduce(${selector} || [], (a,b) => methods[${
451458
buildState.methods.length - 1
@@ -672,8 +679,8 @@ function createArrayIterativeMethod (name, useTruthy = false) {
672679
const aboveArray = method.aboveDetected ? buildState.compile`[{ iterator: z, index: x }, context, above]` : buildState.compile`null`
673680

674681
if (async) {
675-
if (!isSyncDeep(mapper, buildState.engine, buildState)) {
676-
buildState.detectAsync = true
682+
if (!isSync(method)) {
683+
buildState.asyncDetected = true
677684
return buildState.compile`await asyncIterators[${name}](${selector} || [], async (i, x, z) => ${method}(i, x, ${aboveArray}))`
678685
}
679686
}

suites/additional.json

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[
55
1,
66
{
7-
"var": "x"
7+
"val": "x"
88
},
99
3
1010
],
@@ -21,11 +21,11 @@
2121
{
2222
"if": [
2323
{
24-
"var": "x"
24+
"val": "x"
2525
},
2626
[
2727
{
28-
"var": "y"
28+
"val": "y"
2929
}
3030
],
3131
99
@@ -43,20 +43,20 @@
4343
{
4444
"reduce": [
4545
{
46-
"var": "integers"
46+
"val": "integers"
4747
},
4848
{
4949
"+": [
5050
{
51-
"var": "current"
51+
"val": "current"
5252
},
5353
{
54-
"var": "accumulator"
54+
"val": "accumulator"
5555
}
5656
]
5757
},
5858
{
59-
"var": "start_with"
59+
"val": "start_with"
6060
}
6161
]
6262
},

suites/chained.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
{
1616
"description": "Max with Logic Chaining",
17-
"rule": { "max": { "var": "data" } },
17+
"rule": { "max": { "val": "data" } },
1818
"data": { "data": [1, 2, 3] },
1919
"result": 3
2020
},
@@ -26,7 +26,7 @@
2626
},
2727
{
2828
"description": "Cat with Logic Chaining (Simple)",
29-
"rule": { "cat": { "var": "text" } },
29+
"rule": { "cat": { "val": "text" } },
3030
"data": { "text": ["Hello ", "World", "!"] },
3131
"result": "Hello World!"
3232
},
@@ -35,10 +35,10 @@
3535
"max": {
3636
"map": [{
3737
"filter": [
38-
{ "var": "people" },
39-
{ "===": [{ "var": "department" }, "Engineering"] }
38+
{ "val": "people" },
39+
{ "===": [{ "val": "department" }, "Engineering"] }
4040
]},
41-
{ "var": "salary" }
41+
{ "val": "salary" }
4242
]
4343
}
4444
},

suites/scopes.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"description": "Map can add each number to index",
55
"rule": {
66
"map": [
7-
{ "var": "numbers" },
8-
{ "+": [{ "var": "../index" }, { "var": "" }]}
7+
{ "val": "numbers" },
8+
{ "+": [{ "val": [[1], "index"] }, { "val": [] }]}
99
]
1010
},
1111
"data": { "numbers": [1,2,3] },
@@ -15,8 +15,8 @@
1515
"description": "Map can add each number to value from context",
1616
"rule": {
1717
"map": [
18-
{ "var": "numbers" },
19-
{ "+": [{ "var": "../../value" }, { "var": "" }]}
18+
{ "val": "numbers" },
19+
{ "+": [{ "val": [[2], "value"] }, { "val": [] }]}
2020
]
2121
},
2222
"data": { "numbers": [1,2,3], "value": 10 },
@@ -26,8 +26,8 @@
2626
"description": "Filter can use parent context to filter",
2727
"rule": {
2828
"filter": [
29-
{ "var": "people" },
30-
{ "===": [{ "var": "department" }, { "var": "../../department" }] }
29+
{ "val": "people" },
30+
{ "===": [{ "val": "department" }, { "val": [[2], "department"] }] }
3131
]
3232
},
3333
"data": {
@@ -57,8 +57,8 @@
5757
"description": "Access an escaped key from above",
5858
"rule": {
5959
"map": [
60-
{ "var": "arr" },
61-
{ "+": [{ "var": "../../\\.\\./" }, { "var": "../../..\\/" }]}
60+
{ "val": "arr" },
61+
{ "+": [{ "val": [[2], "", "", "/"] }, { "val": [[2], "../"] }]}
6262
]
6363
},
6464
"data": { "arr": [1,2,3], "../": 10, "": { "": { "/": 7 }} },

0 commit comments

Comments
 (0)