Skip to content

Commit

Permalink
added remove method + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matinFT committed Feb 4, 2021
1 parent 311b04e commit 1f35187
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions modules/index.js
Original file line number Diff line number Diff line change
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
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;
while(index--){
if(predicate(collection[index])) collection.splice(index, 1)
}
return collection;
}
15 changes: 15 additions & 0 deletions test/arrays.js
Original file line number Diff line number Diff line change
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]);
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}]);
});
}());

0 comments on commit 1f35187

Please sign in to comment.