Permalink
Browse files

Change .remove(), add .removeItem()

Just as .get() takes an index, .remove() now takes an item index too.
Also supports test expression to remove multiple items. Old
functionality moved to .removeItem().
  • Loading branch information...
1 parent 229215d commit 7615ceabd51af57a8d7f3e9eeb479e260ccd34cc @DarthJDG committed Oct 14, 2016
Showing with 30 additions and 5 deletions.
  1. +10 −1 mangler.js
  2. +20 −4 test/test.js
View
@@ -1141,7 +1141,16 @@ var Mangler = (function(global) {
return this;
},
- remove: function(item) {
+ remove: function(key) {
+ if(fn.isObject(key) || typeof key === 'function') {
+ fn.filter(this.items, { $not: key });
+ } else {
+ this.items.splice(key, 1);
+ }
+ return this;
+ },
+
+ removeItem: function(item) {
var i = this.items.indexOf(item);
if(i > -1) this.items.splice(i, 1);
return this;
View
@@ -1244,14 +1244,30 @@
});
QUnit.test('.remove', function(assert) {
+ assert.expect(6);
+
+ var m = Mangler(['A', 'B', 'C']);
+
+ assert.deepEqual(m.remove(1).items, ['A', 'C'], 'passed');
+ assert.deepEqual(m.remove(2).items, ['A', 'C'], 'passed');
+ assert.deepEqual(m.remove(0).items, ['C'], 'passed');
+ assert.deepEqual(m.remove(0).items, [], 'passed');
+
+ m = Mangler([1, 2, 3, 4, 5]);
+
+ assert.deepEqual(m.remove({ $gt: 1, $lt: 4 }).items, [1, 4, 5], 'passed');
+ assert.deepEqual(m.remove(function(v) { return v == 4 }).items, [1, 5], 'passed');
+ });
+
+ QUnit.test('.removeItem', function(assert) {
assert.expect(4);
var m = Mangler(['A', 'B', 'C']);
- assert.deepEqual(m.remove('B').items, ['A', 'C'], 'passed');
- assert.deepEqual(m.remove('D').items, ['A', 'C'], 'passed');
- assert.deepEqual(m.remove('A').items, ['C'], 'passed');
- assert.deepEqual(m.remove('C').items, [], 'passed');
+ assert.deepEqual(m.removeItem('B').items, ['A', 'C'], 'passed');
+ assert.deepEqual(m.removeItem('D').items, ['A', 'C'], 'passed');
+ assert.deepEqual(m.removeItem('A').items, ['C'], 'passed');
+ assert.deepEqual(m.removeItem('C').items, [], 'passed');
});
QUnit.test('.rename', function(assert) {

0 comments on commit 7615cea

Please sign in to comment.