Skip to content

Commit d45204a

Browse files
committed
Add access to iterator values
1 parent 24884f6 commit d45204a

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

async_iterators.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ export async function filter (arr, iter) {
66
const result = []
77
let index = 0
88
for (const item of arr) {
9-
if (await iter(item, index++)) result.push(item)
9+
if (await iter(item, index++, arr)) result.push(item)
1010
}
1111
return result
1212
}
1313

1414
export async function some (arr, iter) {
1515
let index = 0
1616
for (const item of arr) {
17-
if (await iter(item, index++)) return true
17+
if (await iter(item, index++, arr)) return true
1818
}
1919
return false
2020
}
2121

2222
export async function every (arr, iter) {
2323
let index = 0
2424
for (const item of arr) {
25-
if (!(await iter(item, index++))) return false
25+
if (!(await iter(item, index++, arr))) return false
2626
}
2727
return true
2828
}
@@ -31,7 +31,7 @@ export async function map (arr, iter) {
3131
const result = []
3232
let index = 0
3333
for (const item of arr) {
34-
result.push(await iter(item, index++))
34+
result.push(await iter(item, index++, arr))
3535
}
3636
return result
3737
}

defaultMethods.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ function createArrayIterativeMethod (name, useTruthy = false) {
571571

572572
return selector[name]((i, index) => {
573573
const result = engine.run(mapper, i, {
574-
above: [{ item: selector, index }, context, above]
574+
above: [{ iterator: selector, index }, context, above]
575575
})
576576
return useTruthy ? engine.truthy(result) : result
577577
})
@@ -585,7 +585,7 @@ function createArrayIterativeMethod (name, useTruthy = false) {
585585
})) || []
586586
return asyncIterators[name](selector, (i, index) => {
587587
const result = engine.run(mapper, i, {
588-
above: [{ item: selector, index }, context, above]
588+
above: [{ iterator: selector, index }, context, above]
589589
})
590590
return useTruthy ? engine.truthy(result) : result
591591
})
@@ -603,16 +603,16 @@ function createArrayIterativeMethod (name, useTruthy = false) {
603603
}
604604

605605
const method = build(mapper, mapState)
606-
const aboveArray = method.aboveDetected ? buildState.compile`[{ item: null }, context, above]` : buildState.compile`null`
606+
const aboveArray = method.aboveDetected ? buildState.compile`[{ iterator: z, index: x }, context, above]` : buildState.compile`null`
607607

608608
if (async) {
609609
if (!isSyncDeep(mapper, buildState.engine, buildState)) {
610610
buildState.detectAsync = true
611-
return buildState.compile`await asyncIterators[${name}](${selector} || [], async (i, x) => ${method}(i, x, ${aboveArray}))`
611+
return buildState.compile`await asyncIterators[${name}](${selector} || [], async (i, x, z) => ${method}(i, x, ${aboveArray}))`
612612
}
613613
}
614614

615-
return buildState.compile`(${selector} || [])[${name}]((i, x) => ${method}(i, x, ${aboveArray}))`
615+
return buildState.compile`(${selector} || [])[${name}]((i, x, z) => ${method}(i, x, ${aboveArray}))`
616616
},
617617
traverse: false
618618
}

general.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,9 @@ describe('Various Test Cases', () => {
259259
})
260260

261261
// This is not fully supported right now.
262-
// it('is able to access the source array in the iterators', async () => {
263-
// for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { map: [[0, 1, 2, 3], { '+': [{ var: '' }, { var: '../item.2' }] }] }, {}, [2, 3, 4, 5])
264-
// })
262+
it('is able to access the source array in the iterators', async () => {
263+
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { map: [[0, 1, 2, 3], { '+': [{ var: '' }, { var: '../iterator.2' }] }] }, {}, [2, 3, 4, 5])
264+
})
265265

266266
it('should be able to handle various instances of "in" with good, bad and invalid data', async () => {
267267
const rule = {

0 commit comments

Comments
 (0)