-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 theindex.html
.
Please let me know if you need any help. Good luck!
@@ -0,0 +1,8 @@ | |||
// removes first element having the condition |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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; |
There was a problem hiding this comment.
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).
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) |
There was a problem hiding this comment.
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.
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; | ||
} |
There was a problem hiding this comment.
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.
var result = [1, 2, 3, 4] | ||
_.remove(result, function(obj) {return(obj == 2)}) | ||
assert.deepEqual(result, [1, 3, 4]); |
There was a problem hiding this comment.
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:
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]); |
Follow-up on #2906. |
No description provided.