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
stream: add reduce #41669
Merged
nodejs-github-bot
merged 3 commits into
nodejs:master
from
benjamingr:stream-iterator-helper-reduce
Jan 30, 2022
Merged
stream: add reduce #41669
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,130 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { | ||
Readable, | ||
} = require('stream'); | ||
const assert = require('assert'); | ||
|
||
function sum(p, c) { | ||
return p + c; | ||
} | ||
|
||
{ | ||
// Does the same thing as `(await stream.toArray()).reduce(...)` | ||
(async () => { | ||
const tests = [ | ||
[[], sum, 0], | ||
[[1], sum, 0], | ||
[[1, 2, 3, 4, 5], sum, 0], | ||
[[...Array(100).keys()], sum, 0], | ||
benjamingr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[['a', 'b', 'c'], sum, ''], | ||
[[1, 2], sum], | ||
[[1, 2, 3], (x, y) => y], | ||
]; | ||
for (const [values, fn, initial] of tests) { | ||
const streamReduce = await Readable.from(values) | ||
.reduce(fn, initial); | ||
const arrayReduce = values.reduce(fn, initial); | ||
assert.deepStrictEqual(streamReduce, arrayReduce); | ||
} | ||
// Does the same thing as `(await stream.toArray()).reduce(...)` with an | ||
// asynchronous reducer | ||
for (const [values, fn, initial] of tests) { | ||
const streamReduce = await Readable.from(values) | ||
.map(async (x) => x) | ||
.reduce(fn, initial); | ||
const arrayReduce = values.reduce(fn, initial); | ||
assert.deepStrictEqual(streamReduce, arrayReduce); | ||
} | ||
})().then(common.mustCall()); | ||
} | ||
{ | ||
// Works with an async reducer, with or without initial value | ||
(async () => { | ||
const six = await Readable.from([1, 2, 3]).reduce(async (p, c) => p + c, 0); | ||
assert.strictEqual(six, 6); | ||
})().then(common.mustCall()); | ||
(async () => { | ||
const six = await Readable.from([1, 2, 3]).reduce(async (p, c) => p + c); | ||
assert.strictEqual(six, 6); | ||
})().then(common.mustCall()); | ||
} | ||
{ | ||
// Works lazily | ||
assert.rejects(Readable.from([1, 2, 3, 4, 5, 6]) | ||
.map(common.mustCall((x) => { | ||
return x; | ||
}, 3)) // Two consumed and one buffered by `map` due to default concurrency | ||
.reduce(async (p, c) => { | ||
if (p === 1) { | ||
throw new Error('boom'); | ||
} | ||
return c; | ||
}, 0) | ||
, /boom/).then(common.mustCall()); | ||
} | ||
|
||
{ | ||
// Support for AbortSignal | ||
const ac = new AbortController(); | ||
assert.rejects(async () => { | ||
await Readable.from([1, 2, 3]).reduce(async (p, c) => { | ||
if (c === 3) { | ||
await new Promise(() => {}); // Explicitly do not pass signal here | ||
} | ||
return Promise.resolve(); | ||
}, 0, { signal: ac.signal }); | ||
}, { | ||
name: 'AbortError', | ||
}).then(common.mustCall()); | ||
ac.abort(); | ||
} | ||
|
||
|
||
{ | ||
// Support for AbortSignal - pre aborted | ||
const stream = Readable.from([1, 2, 3]); | ||
assert.rejects(async () => { | ||
await stream.reduce(async (p, c) => { | ||
if (c === 3) { | ||
await new Promise(() => {}); // Explicitly do not pass signal here | ||
} | ||
return Promise.resolve(); | ||
}, 0, { signal: AbortSignal.abort() }); | ||
}, { | ||
name: 'AbortError', | ||
}).then(common.mustCall(() => { | ||
assert.strictEqual(stream.destroyed, true); | ||
})); | ||
} | ||
|
||
{ | ||
// Support for AbortSignal - deep | ||
const stream = Readable.from([1, 2, 3]); | ||
assert.rejects(async () => { | ||
await stream.reduce(async (p, c, { signal }) => { | ||
signal.addEventListener('abort', common.mustCall(), { once: true }); | ||
if (c === 3) { | ||
await new Promise(() => {}); // Explicitly do not pass signal here | ||
} | ||
return Promise.resolve(); | ||
}, 0, { signal: AbortSignal.abort() }); | ||
}, { | ||
name: 'AbortError', | ||
}).then(common.mustCall(() => { | ||
assert.strictEqual(stream.destroyed, true); | ||
})); | ||
} | ||
|
||
{ | ||
// Error cases | ||
assert.rejects(() => Readable.from([]).reduce(1), /TypeError/); | ||
assert.rejects(() => Readable.from([]).reduce('5'), /TypeError/); | ||
} | ||
|
||
{ | ||
// Test result is a Promise | ||
const result = Readable.from([1, 2, 3, 4, 5]).reduce(sum, 0); | ||
assert.ok(result instanceof Promise); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To align with what the code currently supports. What if you want to
reduce()
with options but without an initial value?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How would we differentiate an initial value not existing? An object containing a signal can be a valid initialValue for reduce
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An ugly solution would be
reduce(fn, undefined, options)
and changing the spec from "initialValue is not present" to "initialValue is not undefined".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that would also be a hard sell because it diverges from how Array.prototype.reduce behaves:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@devsnek thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whatever is decided, there is something to fix here because one of the opening brackets isn't closed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benjamingr ping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, didn't realize this was blocking the review, fixed.