-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
added remove method (issue #1856) #2907
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -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 commentThe reason will be displayed to describe this comment to others. Learn more. ... which is to pass the predicate through our internal
Suggested change
|
||||||||
while(index--){ | ||||||||
if(predicate(collection[index])) collection.splice(index, 1) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||
} | ||||||||
return collection; | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a linebreak at the end of the file. |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Suggested change
|
||||||||||||||
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}]); | ||||||||||||||
}); | ||||||||||||||
}()); |
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 required for my next suggestion...