From a14fbff53d583c58e6a8f797105ec96a275f22c8 Mon Sep 17 00:00:00 2001 From: Alejandro Carrau Date: Fri, 3 Jul 2015 00:21:03 -0300 Subject: [PATCH 1/3] Add insert function to array.builders --- test/array.builders.js | 5 +++++ underscore.array.builders.js | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/test/array.builders.js b/test/array.builders.js index 892e6a9..cb7604b 100644 --- a/test/array.builders.js +++ b/test/array.builders.js @@ -208,4 +208,9 @@ $(document).ready(function() { deepEqual(_.combinations(["a",["b"]],[[1]]),[["a",[1]],[["b"],[1]]],'initial arrays can contain array elements which are then preserved'); }); + test('insert', function(){ + deepEqual(_.insert([], 0, 1), [1],'empty array will will insert item'); + deepEqual(_.insert([1,3], 1, 2), [1,2,3],'array will insert item'); + deepEqual(_.insert([1,3], 2, 2), [1,3,2],'exceeding index will insert item at the end'); + }); }); diff --git a/underscore.array.builders.js b/underscore.array.builders.js index 426657f..1e4596d 100644 --- a/underscore.array.builders.js +++ b/underscore.array.builders.js @@ -196,6 +196,13 @@ })); },[]); },_.map(arguments[0],function(i){return [i];})); + }, + + // Inserts an item in an array at the specific index + insert: function(array, index, item){ + var copy = array.slice(0); + copy.splice(index, 0, item); + return copy; } }); From a36c4d4b2c11332568e6b72ee3561ae8d851d586 Mon Sep 17 00:00:00 2001 From: Alejandro Carrau Date: Sat, 11 Jul 2015 18:32:30 -0300 Subject: [PATCH 2/3] Mutate original array passed to insert function and return it --- test/array.builders.js | 7 ++++--- underscore.array.builders.js | 9 +++++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/test/array.builders.js b/test/array.builders.js index cb7604b..51cfb98 100644 --- a/test/array.builders.js +++ b/test/array.builders.js @@ -209,8 +209,9 @@ $(document).ready(function() { }); test('insert', function(){ - deepEqual(_.insert([], 0, 1), [1],'empty array will will insert item'); - deepEqual(_.insert([1,3], 1, 2), [1,2,3],'array will insert item'); - deepEqual(_.insert([1,3], 2, 2), [1,3,2],'exceeding index will insert item at the end'); + deepEqual(_.insert([], 0, 1), [1],'inserts item in empty array'); + deepEqual(_.insert([2], 0, 1), [1,2],'inserst item at the corret index'); + deepEqual(_.insert([1,2], 2, 3), [1,2,3],'inserts item at the end of array if exceeding index'); + deepEqual(_.insert([1,3], -1, 2), [1,2,3],'inserst item at the correct index if negative index'); }); }); diff --git a/underscore.array.builders.js b/underscore.array.builders.js index 1e4596d..93f8c52 100644 --- a/underscore.array.builders.js +++ b/underscore.array.builders.js @@ -17,6 +17,7 @@ // Create quick reference variables for speed access to core prototypes. var slice = Array.prototype.slice; + var splice = Array.prototype.splice; var existy = function(x) { return x != null; }; @@ -198,11 +199,11 @@ },_.map(arguments[0],function(i){return [i];})); }, - // Inserts an item in an array at the specific index + // Inserts an item in an array at the specific index mutating the original + // array and returning it. insert: function(array, index, item){ - var copy = array.slice(0); - copy.splice(index, 0, item); - return copy; + splice.call(array, index, 0, item); + return array; } }); From ac922d12715442fc0e9c2ad87bd8c70ec6de8eed Mon Sep 17 00:00:00 2001 From: Alejandro Carrau Date: Sun, 12 Jul 2015 19:26:23 -0300 Subject: [PATCH 3/3] Verify only array is passed to insert function Throw TypeError if insert receives something other than an array --- test/array.builders.js | 3 +++ underscore.array.builders.js | 1 + 2 files changed, 4 insertions(+) diff --git a/test/array.builders.js b/test/array.builders.js index 51cfb98..02e8f41 100644 --- a/test/array.builders.js +++ b/test/array.builders.js @@ -209,6 +209,9 @@ $(document).ready(function() { }); test('insert', function(){ + var throwingFn = function() { _.insert({}, 0, 1); }; + throws(throwingFn, TypeError, 'throws a TypeError when passing an object literal'); + deepEqual(_.insert([], 0, 1), [1],'inserts item in empty array'); deepEqual(_.insert([2], 0, 1), [1,2],'inserst item at the corret index'); deepEqual(_.insert([1,2], 2, 3), [1,2,3],'inserts item at the end of array if exceeding index'); diff --git a/underscore.array.builders.js b/underscore.array.builders.js index 93f8c52..46aae9f 100644 --- a/underscore.array.builders.js +++ b/underscore.array.builders.js @@ -202,6 +202,7 @@ // Inserts an item in an array at the specific index mutating the original // array and returning it. insert: function(array, index, item){ + if (!_.isArray(array)) throw new TypeError('Expected an array as the first argument'); splice.call(array, index, 0, item); return array; }