Skip to content

Commit 62b9e81

Browse files
committed
Separate out the val proposal
1 parent 91d1820 commit 62b9e81

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

defaultMethods.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,35 @@ const defaultMethods = {
203203
}
204204
}
205205
},
206+
// Adding this to spec something out, not to merge it quite yet
207+
val: {
208+
method: (args, context, above) => {
209+
let result = context
210+
let start = 0
211+
if (Array.isArray(args[0]) && args[0].length === 1) {
212+
start++
213+
const climb = +Math.abs(args[0][0])
214+
let pos = 0
215+
for (let i = 0; i < climb; i++) {
216+
result = above[pos++]
217+
if (i === above.length - 1 && Array.isArray(result)) {
218+
above = result
219+
result = result[0]
220+
pos = 1
221+
}
222+
}
223+
}
224+
225+
for (let i = start; i < args.length; i++) {
226+
if (args[i] === null) continue
227+
if (result === null || result === undefined) return null
228+
result = result[args[i]]
229+
}
230+
if (typeof result === 'undefined') return null
231+
return result
232+
},
233+
deterministic: false
234+
},
206235
var: (key, context, above, engine) => {
207236
let b
208237
if (Array.isArray(key)) {
@@ -539,7 +568,7 @@ function createArrayIterativeMethod (name, useTruthy = false) {
539568
}
540569

541570
const method = build(mapper, mapState)
542-
const aboveArray = method.aboveDetected ? buildState.compile`[{ item: null }, context, above]` : buildState.compile`null`
571+
const aboveArray = method.aboveDetected ? buildState.compile`[{ item: null, index: x }, context, above]` : buildState.compile`null`
543572

544573
if (async) {
545574
if (!isSyncDeep(mapper, buildState.engine, buildState)) {

suites/val.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[
2+
"Test Specification for val",
3+
{
4+
"description": "Fetches a value from an empty key",
5+
"rule": { "val": "" },
6+
"data": { "" : 1 },
7+
"result": 1
8+
},
9+
{
10+
"description": "Fetches a value from a nested empty key",
11+
"rule": { "val": ["", ""] },
12+
"data": { "" : { "": 2 } },
13+
"result": 2
14+
},
15+
{
16+
"description": "Fetches a value from a doubly nested empty key",
17+
"rule": { "val": ["", "", ""] },
18+
"data": { "" : { "": { "": 3 } } },
19+
"result": 3
20+
},
21+
{
22+
"description": "Fetches a value from a key that is purely a dot",
23+
"rule": { "val": "." },
24+
"data": { "." : 20 },
25+
"result": 20
26+
},
27+
{
28+
"description": "Fetches the entire context",
29+
"rule": { "val": null },
30+
"data": { "": 21 },
31+
"result": { "": 21 }
32+
},
33+
{
34+
"description": "Fetches the entire context for a nested key",
35+
"rule": { "val": "" },
36+
"data": { "": { "": 22 } },
37+
"result": { "": 22 }
38+
},
39+
{
40+
"description": "Using val in a map (remember that null gets the current context, not empty string)",
41+
"rule": { "map": [[1,2,3], { "+": [{ "val": null }, 1] }] },
42+
"data": null,
43+
"result": [2,3,4]
44+
},
45+
{
46+
"description": "Using val in a map (and remember [null] and null are the same)",
47+
"rule": { "map": [[1,2,3], { "+": [{ "val": [null] }, 1] }] },
48+
"data": null,
49+
"result": [2,3,4]
50+
},
51+
"Testing out scopes",
52+
{
53+
"description": "Climb up to get adder",
54+
"rule": { "map": [[1,2,3], { "+": [{ "val": null }, { "val": [[-2], "adder"] }] }] },
55+
"data": { "adder": 10 },
56+
"result": [11,12,13]
57+
},
58+
{
59+
"description": "Climb up to get index",
60+
"rule": { "map": [[1,2,3], { "+": [{ "val": null }, { "val": [[-1], "index"] }] }] },
61+
"data": { "adder": 10 },
62+
"result": [1,3,5]
63+
},
64+
{
65+
"description": "Nested get adder",
66+
"rule": {
67+
"map": [["Test"], { "map": [[1,2,3], { "+": [{"val": null}, {"val": [[-4], "adder"]}] }]} ]
68+
},
69+
"data": { "adder": 10 },
70+
"result": [[11,12,13]]
71+
}
72+
]

0 commit comments

Comments
 (0)