-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
added remove method (issue #1856) #2906
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
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,7 @@ | ||
import findIndex from './findIndex.js' | ||
|
||
// removes first element having the condition | ||
export default function remove(collection, predicate){ | ||
collection.splice(findIndex(collection, predicate), 1); | ||
return collection; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -583,4 +583,11 @@ | |
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) { | ||
assert.deepEqual(_.remove([1, 2, 3, 4], [1, 3, 4]), function(obj) {return(obj == 2)}); | ||
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 line seems to have been written with the following intuition: if you have the elements 1, 2, 3, 4 and you remove the elements 1, 3, 4, you'll be left with only element 2. While this intuition is right, this is not what you should be testing, and the way you're trying to test it also doesn't work. You shouldn't be testing this, because the intent of The test also doesn't work, because There is also something missing. You should not only compare the result of |
||
assert.deepEqual(_.remove([{a: 1}, {a:3, b: 2}, 3], [{a: 1}, 3], function(obj) {return(obj.a == 3)})); | ||
assert.deepEqual(_.remove([{a: {a: 1}}, 2, 3], [2, 3], function(obj) {return(obj.a.a == 1)})); | ||
assert.deepEqual(_.remove([{a: 1, b: 2, c: 3}, {a:3, b: 2}, {c: 3}], [{a:3, b: 2}, {c: 3}], function(obj) {return(obj.b == 2 && obj.c == 3)})); | ||
Comment on lines
+589
to
+591
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. The next three tests have the same problems, but in addition, you mixed up the parentheses. You're now passing three arguments to 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. yeah sorry my bad. don't know how i came up with this! i messed up deepEqual and _. remove second arguments. i'll fix it and try with a new pr. |
||
}); | ||
}()); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 a start, but the intent of #1856 is to remove all elements that match the predicate. I think this will be easiest to do correctly if you go through
collection
backwards, i.e., remove the rightmost element first and the leftmost element last.