Skip to content

Commit ac922d1

Browse files
committed
Verify only array is passed to insert function
Throw TypeError if insert receives something other than an array
1 parent a36c4d4 commit ac922d1

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

test/array.builders.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ $(document).ready(function() {
209209
});
210210

211211
test('insert', function(){
212+
var throwingFn = function() { _.insert({}, 0, 1); };
213+
throws(throwingFn, TypeError, 'throws a TypeError when passing an object literal');
214+
212215
deepEqual(_.insert([], 0, 1), [1],'inserts item in empty array');
213216
deepEqual(_.insert([2], 0, 1), [1,2],'inserst item at the corret index');
214217
deepEqual(_.insert([1,2], 2, 3), [1,2,3],'inserts item at the end of array if exceeding index');

underscore.array.builders.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
// Inserts an item in an array at the specific index mutating the original
203203
// array and returning it.
204204
insert: function(array, index, item){
205+
if (!_.isArray(array)) throw new TypeError('Expected an array as the first argument');
205206
splice.call(array, index, 0, item);
206207
return array;
207208
}

0 commit comments

Comments
 (0)