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

Conversation

matinFT
Copy link

@matinFT matinFT commented Feb 4, 2021

No description provided.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.03%) to 95.223% when pulling 1f35187 on matinFT:remove_method into 745e9b7 on jashkenas:master.

Copy link
Collaborator

@jgonggrijp jgonggrijp left a comment

Choose a reason for hiding this comment

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

Very good @matinFT. Thanks for moving forward! We're nearly there.

I'm not seeing an update in underscore.js and underscore-esm.js. It's good that you didn't edit those files manually, but I expected the commit hook to update them automatically given your addition in modules/index.js. Did you not run npm install, or is the commit hook not working for you for some reason?

I see three remaining steps. You can continue adding and pushing new commits to your remove_method branch, they will be included in the current PR automatically (so you don't have to open a new PR).

  • Get the commit hook to work so that underscore.js is automatically updated.
  • Process my comments below.
  • Add documentation for _.remove in the index.html.

Please let me know if you need any help. Good luck!

@@ -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...

@@ -0,0 +1,8 @@
// removes first element having the condition
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);

export default function remove(collection, predicate) {
var index = collection.length;
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);

if(predicate(collection[index])) 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.

Comment on lines +588 to +590
var result = [1, 2, 3, 4]
_.remove(result, function(obj) {return(obj == 2)})
assert.deepEqual(result, [1, 3, 4]);
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]);

@jgonggrijp
Copy link
Collaborator

Follow-up on #2906.

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.

3 participants