fix/compat array find array like length - #1958
Open
kojesung wants to merge 11 commits into
Open
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…mpat-array-find-arrayLike-length
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
findused theif (!Array.isArray(source))statement to treat anything that wasn't an array as an object and process it accordingly. However, this branch handling, when an object has a readable property calledlength, uses the property namelengthas a key to perform the search. But this differs from lodash's intended behavior. When lodash encounters alengthproperty — such as thelengththat exists on real arrays — it does not use it as a key and ignores it instead.To follow this behavior of lodash, instead of
Array.isArray, I applied the implementation approach of.map, which uses theisArrayLikefunction to extract keys excluding the length property when it exists, and treats only the extracted keys as the iteration target. Through this, I implemented it so that, just like lodash's implementation, when a length property exists it is not used as a key and is ignored.In this process, I also removed the structure where the array branch delegated to native code via
.slice(fromIndex).find(doesMatch);as a side effect, this also fixed theindex/collectionarguments passed to the predicate when usingfromIndexso that they are now correctly passed based on the original array (rather than the sliced array).I added a test case that can verify this work.
As the implementation approach changed, the shape of the error message that the
'should throw error when boolean predicate is used'test was verifying ended up changing. Because the entity that throws the error changed, I modified the assertion message so the test would pass. For reference, actual lodash does not throw an error for a boolean predicate and instead returnsundefined;this behavior itself (throwing an error) was judged to be the domain of theiterateefunction, so it was not included in the scope of this fix.