Skip to content

Modified RFC6901 #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Retrofit val with scopes, as an example
  • Loading branch information
TotalTechGeek committed Dec 8, 2024
commit 75365ec135f48917b947341f2a43edf4d4e7662d
21 changes: 18 additions & 3 deletions defaultMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,24 @@ const defaultMethods = {
},
// Adding this to spec something out, not to merge it.
val: {
method: (args, context) => {
method: (args, context, above) => {
let result = context
for (let i = 0; i < args.length; i++) {
let start = 0
if (Array.isArray(args[0]) && args[0].length === 1) {
start++
const climb = +Math.abs(args[0][0])
let pos = 0
for (let i = 0; i < climb; i++) {
result = above[pos++]
if (i === above.length - 1 && Array.isArray(result)) {
above = result
result = result[0]
pos = 1
}
}
}

for (let i = start; i < args.length; i++) {
if (args[i] === null) continue
if (result === null || result === undefined) return null
result = result[args[i]]
Expand Down Expand Up @@ -553,7 +568,7 @@ function createArrayIterativeMethod (name, useTruthy = false) {
}

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

if (async) {
if (!isSyncDeep(mapper, buildState.engine, buildState)) {
Expand Down
21 changes: 21 additions & 0 deletions suites/val.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,26 @@
"rule": { "map": [[1,2,3], { "+": [{ "val": [null] }, 1] }] },
"data": null,
"result": [2,3,4]
},
"Testing out scopes",
{
"description": "Climb up to get adder",
"rule": { "map": [[1,2,3], { "+": [{ "val": null }, { "val": [[-2], "adder"] }] }] },
"data": { "adder": 10 },
"result": [11,12,13]
},
{
"description": "Climb up to get index",
"rule": { "map": [[1,2,3], { "+": [{ "val": null }, { "val": [[-1], "index"] }] }] },
"data": { "adder": 10 },
"result": [1,3,5]
},
{
"description": "Nested get adder",
"rule": {
"map": [["Test"], { "map": [[1,2,3], { "+": [{"val": null}, {"val": [[-4], "adder"]}] }]} ]
},
"data": { "adder": 10 },
"result": [[11,12,13]]
}
]
Loading