-
Notifications
You must be signed in to change notification settings - Fork 30.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This continues on the iterator-helpers work by adding `.some` and `.every` to readable streams. Co-Authored-By: Robert Nagy <ronagy@icloud.com>
- Loading branch information
1 parent
5a407d6
commit 6ad7973
Showing
3 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'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 | ||
(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 | ||
(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()); | ||
} | ||
|
||
{ | ||
// Short circuiting - some | ||
const stream = Readable.from(async function*() { | ||
yield 1; | ||
yield 2; | ||
throw new Error(); | ||
}()); | ||
(async () => { | ||
assert.strictEqual(await stream.some((x) => x === 2), true); | ||
assert.strictEqual(stream.destroyed, true); | ||
})().then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Short circuiting - every | ||
const stream = Readable.from(async function*() { | ||
yield 1; | ||
yield 2; | ||
throw new Error(); | ||
}()); | ||
(async () => { | ||
assert.strictEqual(await stream.every((x) => x !== 2), false); | ||
})().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()); | ||
} |