Skip to content

Commit a14fbff

Browse files
author
Alejandro Carrau
committed
Add insert function to array.builders
1 parent 1492ffb commit a14fbff

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

test/array.builders.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,9 @@ $(document).ready(function() {
208208
deepEqual(_.combinations(["a",["b"]],[[1]]),[["a",[1]],[["b"],[1]]],'initial arrays can contain array elements which are then preserved');
209209
});
210210

211+
test('insert', function(){
212+
deepEqual(_.insert([], 0, 1), [1],'empty array will will insert item');
213+
deepEqual(_.insert([1,3], 1, 2), [1,2,3],'array will insert item');
214+
deepEqual(_.insert([1,3], 2, 2), [1,3,2],'exceeding index will insert item at the end');
215+
});
211216
});

underscore.array.builders.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,13 @@
196196
}));
197197
},[]);
198198
},_.map(arguments[0],function(i){return [i];}));
199+
},
200+
201+
// Inserts an item in an array at the specific index
202+
insert: function(array, index, item){
203+
var copy = array.slice(0);
204+
copy.splice(index, 0, item);
205+
return copy;
199206
}
200207

201208
});

0 commit comments

Comments
 (0)