Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34.3k
stream: add iterator helper find#41849
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
Merged
nodejs-github-bot merged 2 commits into nodejs:master from Linkgoron:stream-find-operatorFeb 7, 2022
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -186,31 +186,9 @@ function asIndexedPairs(options = undefined){ | ||
| } | ||
| async function some(fn, options){ | ||
| if (options != null && typeof options !== 'object'){ | ||
| throw new ERR_INVALID_ARG_TYPE('options', ['Object']); | ||
| } | ||
| if (options?.signal != null){ | ||
| validateAbortSignal(options.signal, 'options.signal'); | ||
| } | ||
| // 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; | ||
| } | ||
| // eslint-disable-next-line no-unused-vars | ||
benjamingr marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| for await (const unused of filter.call(this, fn, options)){ | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| @@ -226,6 +204,13 @@ async function every(fn, options){ | ||
| }, options)); | ||
| } | ||
| async function find(fn, options){ | ||
| for await (const result of filter.call(this, fn, options)){ | ||
| return result; | ||
| } | ||
| return undefined; | ||
| } | ||
| async function forEach(fn, options){ | ||
| if (typeof fn !== 'function'){ | ||
| throw new ERR_INVALID_ARG_TYPE( | ||
| @@ -236,7 +221,7 @@ async function forEach(fn, options){ | ||
| return kEmpty; | ||
| } | ||
| // eslint-disable-next-line no-unused-vars | ||
| for await (const unused of this.map(forEachFn, options)); | ||
| for await (const unused of map.call(this, forEachFn, options)); | ||
| } | ||
| function filter(fn, options){ | ||
| @@ -250,7 +235,7 @@ function filter(fn, options){ | ||
| } | ||
| return kEmpty; | ||
| } | ||
| return this.map(filterFn, options); | ||
| return map.call(this, filterFn, options); | ||
benjamingr marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| // Specific to provide better error to reduce since the argument is only | ||
| @@ -329,7 +314,7 @@ async function toArray(options){ | ||
| } | ||
| function flatMap(fn, options){ | ||
| const values = this.map(fn, options); | ||
| const values = map.call(this, fn, options); | ||
| return async function* flatMap(){ | ||
| for await (const val of values){ | ||
| yield* val; | ||
| @@ -415,4 +400,5 @@ module.exports.promiseReturningOperators ={ | ||
| reduce, | ||
| toArray, | ||
| some, | ||
| find, | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Is it "chunk" or "item"? Let's choose one and use that throughout documentation
Uh oh!
There was an error while loading. Please reload this page.
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.
Do you want me to change if for every/some as well?
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.
Might as well! Fits in this PR because it already touches those.
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.
changed
itemtochunkin all places except in one unrelated place.