Skip to content

Pass index and original array in .filter #10

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export const move = (array, fromIndex, toIndex) => {

export const filter = (array, fn) => {
let changed = false;
const newArray = array.filter(row => {
const shouldKeep = fn(row);
const newArray = array.filter((row, index, originalArray) => {
const shouldKeep = fn(row, index, originalArray);
changed = !shouldKeep || changed;
return shouldKeep;
});
Expand Down
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ test('filter()', t => {
t.deepEqual(actual, expected);
});

test('filter() unique values', t => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this describes a use case for using the index and originalArray properties, but not the actual functionality.

I think the test would be better (more descriptive) if the title at least was more generic. Might also make sense to create a simpler test.

Another idea would be to add a .unique method to immutable-array-methods!

const input = [1, 1, 2, 3];
const actual = filter(input, (value, index, originalArray) => originalArray.indexOf(value) === index);
const expected = [1, 2, 3];
t.deepEqual(actual, expected);
});

test('default import', t => {
t.is(arrayMethods.push, push);
t.is(arrayMethods.unshift, unshift);
Expand Down