Skip to content
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

makes parity with native filter function #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kalpeshsingh
Copy link

What was the issue?
The newFilter method allows undefined values to be part of the filtered array but the native implementation removes that.

How to replicate it?
Add numbers[7] = 9 and change condition to filter odd numbers and you will find out that it doesn't filter empty/undefined values in the array.

numbers array would be -

const numbers = [1, 2, 3, 4, undefined, undefined, undefined, 9]

What is the reason?
When we call callback.call(context, this[index], index, this) for undefined value then it produces true and it pushes the value to result array.

undefined => undefined % 2 !== 0 // produces `true`

How to prevent it?
We need to put a guard condition to only allow values that are actually part of the array and not produced artificially. The proposed condition makes sure that it matches with native .filter output.

The `newFilter` method allows undefined to be part of the filtered array but the native implementation removes that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant