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

added remove method (issue #1856) #2907

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions modules/index.js
Expand Up @@ -191,6 +191,8 @@ export { default as zip } from './zip.js';
export { default as object } from './object.js';
export { default as range } from './range.js';
export { default as chunk } from './chunk.js';
export { default as remove } from './remove.js';


// OOP
// ---
Expand Down
8 changes: 8 additions & 0 deletions modules/remove.js
@@ -0,0 +1,8 @@
// removes first element having the condition
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// removes first element having the condition
import cb from './_cb.js';
// removes first element having the condition

This is required for my next suggestion...

export default function remove(collection, predicate) {
var index = collection.length;
Copy link
Collaborator

Choose a reason for hiding this comment

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

... which is to pass the predicate through our internal cb function. This enables the iteratee shorthands, so users can for example do _.remove(anArray, { a: 1 }) (which would be equivalent to your testcase where you're passing function(obj) { return obj.a == 1; } as the second argument).

Suggested change
var index = collection.length;
var index = collection.length;
predicate = cb(predicate);

while(index--){
if(predicate(collection[index])) collection.splice(index, 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Following the conventions of other Underscore functions, it would be nice to also pass the index and the whole collection to the predicate.

Suggested change
if(predicate(collection[index])) collection.splice(index, 1)
if (predicate(collection[index], index, collection)) collection.splice(index, 1);

}
return collection;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add a linebreak at the end of the file.

15 changes: 15 additions & 0 deletions test/arrays.js
Expand Up @@ -583,4 +583,19 @@
assert.deepEqual(_.chunk([10, 20, 30, 40, 50, 60, 70], 2), [[10, 20], [30, 40], [50, 60], [70]], 'chunk into parts of less then current array length elements');
assert.deepEqual(_.chunk([10, 20, 30, 40, 50, 60, 70], 3), [[10, 20, 30], [40, 50, 60], [70]], 'chunk into parts of less then current array length elements');
});

QUnit.test('remove', function(assert) {
var result = [1, 2, 3, 4]
_.remove(result, function(obj) {return(obj == 2)})
assert.deepEqual(result, [1, 3, 4]);
Comment on lines +588 to +590
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is good. I now understand that your tests in the previous PR where intended differently than I thought.

There is just one thing missing: these tests aren't verifying yet that result is also returned from _.remove. You can fix this as follows:

Suggested change
var result = [1, 2, 3, 4]
_.remove(result, function(obj) {return(obj == 2)})
assert.deepEqual(result, [1, 3, 4]);
var result = [1, 2, 3, 4];
assert.strictEqual(_.remove(result, function(obj) {return(obj == 2)}), result);
assert.deepEqual(result, [1, 3, 4]);

result = [{a: 1}, {a:3, b: 2}, 3]
_.remove(result, function(obj) {return(obj.a == 3)})
assert.deepEqual(result, [{a: 1}, 3]);
result = [{a: 1, b: 2}, {a: 1}, 3]
_.remove(result, function(obj) {return(obj.a == 1)})
assert.deepEqual(result, [3]);
result = [{a: 1, b: 2, c: 3}, {a:1, c: 3}, {c: 3}]
_.remove(result, function(obj) {return(obj.a == 1 && obj.c == 3)})
assert.deepEqual(result, [{c: 3}]);
});
}());