Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
stream: add iterator helper some every#41573
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -10,6 +10,7 @@ const { | ||
| AbortError, | ||
| } = require('internal/errors'); | ||
| const { validateInteger } = require('internal/validators'); | ||
| const { kWeakHandler } = require('internal/event_target'); | ||
| const { | ||
| ArrayPrototypePush, | ||
| @@ -47,6 +48,10 @@ async function * map(fn, options) { | ||
| const signalOpt = { signal }; | ||
| const abort = () => ac.abort(); | ||
| if (options?.signal?.aborted) { | ||
| abort(); | ||
| } | ||
| options?.signal?.addEventListener('abort', abort); | ||
| let next; | ||
| @@ -150,6 +155,40 @@ async function * map(fn, options) { | ||
| } | ||
| } | ||
| async function some(fn, options) { | ||
benjamingr marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| // https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.some | ||
| // Note that some does short circuit but also closes the iterator if it does | ||
| const ac = new AbortController(); | ||
| if (options?.signal) { | ||
| if (options.signal.aborted) { | ||
| ac.abort(); | ||
| } | ||
| options.signal.addEventListener('abort', () => ac.abort(), { | ||
| [kWeakHandler]: this, | ||
| once: true, | ||
| }); | ||
| } | ||
| const mapped = this.map(fn, { ...options, signal: ac.signal }); | ||
| for await (const result of mapped) { | ||
| if (result) { | ||
| ac.abort(); | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
mcollina marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| async function every(fn, options) { | ||
| if (typeof fn !== 'function') { | ||
| throw new ERR_INVALID_ARG_TYPE( | ||
| 'fn', ['Function', 'AsyncFunction'], fn); | ||
| } | ||
| // https://en.wikipedia.org/wiki/De_Morgan%27s_laws | ||
| return !(await some.call(this, async (x) => { | ||
| return !(await fn(x)); | ||
| }, options)); | ||
| } | ||
| async function forEach(fn, options) { | ||
| if (typeof fn !== 'function') { | ||
| throw new ERR_INVALID_ARG_TYPE( | ||
| @@ -196,6 +235,8 @@ module.exports.streamReturningOperators = { | ||
| }; | ||
| module.exports.promiseReturningOperators = { | ||
| every, | ||
| forEach, | ||
| toArray, | ||
| some, | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| 'use strict'; | ||
| const common = require('../common'); | ||
| const { | ||
| Readable, | ||
| } = require('stream'); | ||
| const assert = require('assert'); | ||
| function oneTo5() { | ||
| return Readable.from([1, 2, 3, 4, 5]); | ||
| } | ||
| function oneTo5Async() { | ||
| return oneTo5().map(async (x) => { | ||
| await Promise.resolve(); | ||
| return x; | ||
| }); | ||
| } | ||
| { | ||
| // Some and every work with a synchronous stream and predicate | ||
benjamingr marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| (async () => { | ||
| assert.strictEqual(await oneTo5().some((x) => x > 3), true); | ||
| assert.strictEqual(await oneTo5().every((x) => x > 3), false); | ||
| assert.strictEqual(await oneTo5().some((x) => x > 6), false); | ||
| assert.strictEqual(await oneTo5().every((x) => x < 6), true); | ||
| assert.strictEqual(await Readable.from([]).some((x) => true), false); | ||
| assert.strictEqual(await Readable.from([]).every((x) => true), true); | ||
| })().then(common.mustCall()); | ||
| } | ||
| { | ||
| // Some and every work with an asynchronous stream and synchronous predicate | ||
benjamingr marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| (async () => { | ||
| assert.strictEqual(await oneTo5Async().some((x) => x > 3), true); | ||
| assert.strictEqual(await oneTo5Async().every((x) => x > 3), false); | ||
| assert.strictEqual(await oneTo5Async().some((x) => x > 6), false); | ||
| assert.strictEqual(await oneTo5Async().every((x) => x < 6), true); | ||
| })().then(common.mustCall()); | ||
| } | ||
| { | ||
| // Some and every work on asynchronous streams with an asynchronous predicate | ||
| (async () => { | ||
| assert.strictEqual(await oneTo5().some(async (x) => x > 3), true); | ||
| assert.strictEqual(await oneTo5().every(async (x) => x > 3), false); | ||
| assert.strictEqual(await oneTo5().some(async (x) => x > 6), false); | ||
| assert.strictEqual(await oneTo5().every(async (x) => x < 6), true); | ||
| })().then(common.mustCall()); | ||
| } | ||
| { | ||
| // Some and every short circuit | ||
| (async () => { | ||
| await oneTo5().some(common.mustCall((x) => x > 2, 3)); | ||
| await oneTo5().every(common.mustCall((x) => x < 3, 3)); | ||
| // When short circuit isn't possible the whole stream is iterated | ||
| await oneTo5().some(common.mustCall((x) => x > 6, 5)); | ||
| // The stream is destroyed afterwards | ||
| const stream = oneTo5(); | ||
| await stream.some(common.mustCall((x) => x > 2, 3)); | ||
| assert.strictEqual(stream.destroyed, true); | ||
| })().then(common.mustCall()); | ||
| } | ||
| { | ||
| // Support for AbortSignal | ||
| const ac = new AbortController(); | ||
| assert.rejects(Readable.from([1, 2, 3]).some( | ||
| () => new Promise(() => {}), | ||
| { signal: ac.signal } | ||
| ), { | ||
| name: 'AbortError', | ||
| }).then(common.mustCall()); | ||
| ac.abort(); | ||
| } | ||
| { | ||
| // Support for pre-aborted AbortSignal | ||
| assert.rejects(Readable.from([1, 2, 3]).some( | ||
| () => new Promise(() => {}), | ||
| { signal: AbortSignal.abort() } | ||
| ), { | ||
| name: 'AbortError', | ||
| }).then(common.mustCall()); | ||
| } | ||
| { | ||
| // Error cases | ||
| assert.rejects(async () => { | ||
| await Readable.from([1]).every(1); | ||
| }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); | ||
| assert.rejects(async () => { | ||
| await Readable.from([1]).every((x) => x, { | ||
| concurrency: 'Foo' | ||
| }); | ||
| }, /ERR_OUT_OF_RANGE/).then(common.mustCall()); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.