Skip to content

Commit

Permalink
Cleanup es-iterator-helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Dec 30, 2024
1 parent ece975b commit 63481d8
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ module.exports =
// Step 3.a.iii: Let iteratorRecord be ? GetIteratorDirect(iter).
innerIteratorRecord = getIteratorDirect(innerIterator)
}
const { iterator, next } = innerIteratorRecord
const { iterator, next: nextMethod } = innerIteratorRecord
try {
// Step 3.a.v.1: Let iteratorResult be ? IteratorStep(iteratorRecord).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 3.a.v.3: Yield value if not done.
if (!result.done) {
return { value: result.value, done: false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports =
throw new TypeErrorCtor('`Iterator.from` is not a constructor')
}
// Step 1: Let iteratorRecord be GetIteratorFlattenable(O, iterate-string-primitives).
const { iterator, next } = getIteratorFlattenable(O, true)
const { iterator, next: nextMethod } = getIteratorFlattenable(O, true)
// Step 2: Let hasInstance be OrdinaryHasInstance(%Iterator%, iteratorRecord.[[Iterator]]).
// Step 3: If hasInstance is true, then
if (iterator instanceof IteratorCtor) {
Expand All @@ -34,7 +34,7 @@ module.exports =
// Step 4: Let wrapper be OrdinaryObjectCreate(%WrapForValidIteratorPrototype%, << [[Iterated]] >>).
const wrapper = ObjectCreate(WrapForValidIteratorPrototype)
// Step 5: Set wrapper.[[Iterated]] to iteratorRecord.
setIterated(wrapper, { iterator, next })
setIterated(wrapper, { iterator, next: nextMethod })
// Step 6: Return wrapper.
return wrapper
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = function drop(limit) {
throw new RangeErrorCtor('`limit` must be a non-negative number')
}
// Step 7: Let iterated be GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(O)
const { iterator, next: nextMethod } = getIteratorDirect(O)
// Step 8.a: Let remaining be integerLimit.
let remaining = integerLimit
// Step 8: Let closure be a new Abstract Closure with no parameters that captures iterated and integerLimit.
Expand All @@ -52,14 +52,14 @@ module.exports = function drop(limit) {
remaining -= 1
}
// Step 8.b.ii: Let next be IteratorStep(iterated).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 8.b.iii: If next is done, return ReturnCompletion(undefined).
if (result.done) {
return result
}
}
// Step 8.c: Repeat, yield the remaining values.
return ReflectApply(next, iterator, [])
return ReflectApply(nextMethod, iterator, [])
}
})
// Step 10: Set result.[[UnderlyingIterator]] to iterated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ module.exports =
throw new TypeErrorCtor('`predicate` must be a function')
}
// Step 4: Let iterated be GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(O)
const { iterator, next: nextMethod } = getIteratorDirect(O)
// Step 5: Let counter be 0.
let index = 0
// Step 6: Repeat,
while (true) {
// Step 6.a: Let value be IteratorStepValue(iterated).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 6.b: If value is done, return true.
if (result.done) {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ module.exports = function filter(predicate) {
throw new TypeErrorCtor('`predicate` must be a function')
}
// Step 4: Let iterated be GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(O)
const { iterator, next: nextMethod } = getIteratorDirect(O)
let index = 0
// Step 5: Let closure be a new Abstract Closure with no parameters that captures iterated and predicate.
const wrapper = createIteratorFromClosure({
// Step 5.b: Repeat
next() {
while (true) {
// Step 5.b.i: Let value be IteratorStepValue(iterated).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 5.b.ii: If value is done, return ReturnCompletion(undefined).
if (result.done) {
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ module.exports =
throw new TypeErrorCtor('`predicate` must be a function')
}
// Step 4: Let iterated be GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(O)
const { iterator, next: nextMethod } = getIteratorDirect(O)
// Step 5: Let counter be 0.
let index = 0
// Step 6: Repeat,
while (true) {
// Step 6.a: Let value be IteratorStepValue(iterated).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 6.b: If value is done, return undefined.
if (result.done) {
return undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = function flatMap(mapper) {
throw new TypeErrorCtor('`mapper` must be a function')
}
// Step 4: GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(O)
const { iterator, next: nextMethod } = getIteratorDirect(O)
let innerNext = null
let innerIterator = null
let innerIteratorReturnCalled = false
Expand All @@ -40,7 +40,7 @@ module.exports = function flatMap(mapper) {
while (true) {
if (!innerIterator) {
// Step 5.b.i: Get the next outer value.
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
if (result.done) {
// Step 5.b.ii: ReturnCompletion(undefined).
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ module.exports =
throw new TypeErrorCtor('`procedure` must be a function')
}
// Step 4: Let iterated be GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(O)
const { iterator, next: nextMethod } = getIteratorDirect(O)
// Step 5: Let counter be 0.
let index = 0
// Step 6: Repeat,
while (true) {
// Step 6.a: Let value be IteratorStepValue(iterated).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 6.b: If value is done, return undefined.
if (result.done) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ module.exports = function map(mapper) {
throw new TypeErrorCtor('`mapper` must be a function')
}
// Step 4: Let iterated be GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(O)
const { iterator, next: nextMethod } = getIteratorDirect(O)
// Step 5: Let closure be a new Abstract Closure with no parameters that
// captures iterated and mapper.
let index = 0
const wrapper = createIteratorFromClosure({
// Step 5.b: Repeat
next() {
// Step 5.b.i: Let value be IteratorStepValue(iterated).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 5.b.ii: If value is done, return ReturnCompletion(undefined).
if (result.done) {
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports =
throw new TypeErrorCtor('`reducer` must be a function')
}
// Step 4: Let iterated be GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(O)
const { iterator, next: nextMethod } = getIteratorDirect(O)
let accumulator
let index = 0
// Step 5: If initialValue is not present,
Expand All @@ -42,7 +42,7 @@ module.exports =
accumulator = initialValue
} else {
// Step 5.a: Let accumulator be IteratorStepValue(iterated).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 5.b: If accumulator is done, throw a TypeError exception.
if (result.done) {
throw new TypeErrorCtor(
Expand All @@ -56,7 +56,7 @@ module.exports =
// Step 7: Repeat,
while (true) {
// Step 7.a: Let value be IteratorStepValue(iterated).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 7.b: If value is done, return accumulator.
if (result.done) {
return accumulator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ module.exports =
throw new TypeErrorCtor('`predicate` must be a function')
}
// Step 4: Let iterated be GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(O)
const { iterator, next: nextMethod } = getIteratorDirect(O)
// Step 5: Let counter be 0.
let index = 0
// Step 6: Repeat
while (true) {
// Step 6.a: Let value be IteratorStepValue(iterated).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 6.b: If value is done, return false.
if (result.done) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports =
throw new RangeErrorCtor('`limit` must be a non-negative number')
}
// Step 7: Let iterated be GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(O)
const { iterator, next: nextMethod } = getIteratorDirect(O)
// Step 8.a: Let remaining be integerLimit.
let remaining = integerLimit
// Step 8: Let closure be a new Abstract Closure with no parameters that captures iterated and integerLimit.
Expand All @@ -56,7 +56,7 @@ module.exports =
return { value: undefined, done: true }
}
// Step 8.b.iii: Let value be IteratorStepValue(iterated).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 8.b.iv: If value is done, return ReturnCompletion(undefined).
if (result.done) {
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ module.exports = function toArray() {
// Step 2: If O is not an Object, throw a TypeError exception.
ensureObject(O)
// Step 3: Let iterated be GetIteratorDirect(O).
const { iterator, next } = getIteratorDirect(O)
const { iterator, next: nextMethod } = getIteratorDirect(O)
// Step 4: Let items be a new empty List.
const items = []
// Step 5: Repeat.
while (true) {
// Step 5.a: Let value be IteratorStepValue(iterated).
const result = ReflectApply(next, iterator, [])
const result = ReflectApply(nextMethod, iterator, [])
// Step 5.b: If value is done, return CreateArrayFromList(items).
if (result.done) {
return items
Expand Down
Loading

0 comments on commit 63481d8

Please sign in to comment.