diff --git a/src/iter/iterable2.test.ts b/src/iter/iterable2.test.ts index a15680cf..f343b07b 100644 --- a/src/iter/iterable2.test.ts +++ b/src/iter/iterable2.test.ts @@ -5,6 +5,10 @@ test('iterable2', () => { expect(_rangeIt(3).toArray()).toEqual([0, 1, 2]) expect(_rangeIt(1, 4).find(v => v % 2 === 0)).toBe(2) + expect(_rangeIt(1, 4).some(v => v % 2 === 0)).toBe(true) + expect(_rangeIt(1, 4).some(v => v % 2 === -1)).toBe(false) + expect(_rangeIt(1, 4).every(v => v % 2 === 0)).toBe(false) + expect(_rangeIt(1, 4).every(v => v > 0)).toBe(true) expect( _rangeIt(1, 4) diff --git a/src/iter/iterable2.ts b/src/iter/iterable2.ts index e1512a80..31255478 100644 --- a/src/iter/iterable2.ts +++ b/src/iter/iterable2.ts @@ -1,4 +1,4 @@ -import { AbortableMapper, AbortablePredicate, END, Predicate, SKIP } from '../types' +import { AbortableMapper, AbortablePredicate, END, SKIP } from '../types' /** * Iterable2 is a wrapper around Iterable that implements "Iterator Helpers proposal": @@ -34,10 +34,26 @@ export class Iterable2 implements Iterable { } } - find(cb: Predicate): T | undefined { + some(cb: AbortablePredicate): boolean { + // eslint-disable-next-line unicorn/prefer-array-some + return !!this.find(cb) + } + + every(cb: AbortablePredicate): boolean { + let i = 0 + for (const v of this.it) { + const r = cb(v, i++) + if (r === END || !r) return false + } + return true + } + + find(cb: AbortablePredicate): T | undefined { let i = 0 for (const v of this.it) { - if (cb(v, i++)) return v + const r = cb(v, i++) + if (r === END) return + if (r) return v } }