From a14fbff53d583c58e6a8f797105ec96a275f22c8 Mon Sep 17 00:00:00 2001 From: Alejandro Carrau Date: Fri, 3 Jul 2015 00:21:03 -0300 Subject: [PATCH 1/7] 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/7] 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/7] 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; } From 12fd38976f6fd89c822285bf9451c8fbd3000186 Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Fri, 18 Dec 2020 16:43:14 +0100 Subject: [PATCH 4/7] Remove some trailing whitespace (#199) --- docs/underscore.array.builders.js.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/underscore.array.builders.js.md b/docs/underscore.array.builders.js.md index d5850b7..1be73d6 100644 --- a/docs/underscore.array.builders.js.md +++ b/docs/underscore.array.builders.js.md @@ -1,4 +1,4 @@ -### array.builders +### array.builders > Functions to build arrays. View Annotated Source @@ -198,7 +198,7 @@ That is, the array only contains every number from `5` down to `1` because when The `_.keepIndexed` function takes an array and a function and returns a new array filled with the *non-null* return results of the given function on the elements or keys in the given array: ```javascript -_.keepIndexed([1,2,3], function(k) { +_.keepIndexed([1,2,3], function(k) { return i === 1 || i === 2; }); @@ -208,8 +208,8 @@ _.keepIndexed([1,2,3], function(k) { If you return either `null` or `undefined` then the result is dropped from the resulting array: ```javascript -_.keepIndexed(['a','b','c'], function(k, v) { - if (k === 1) return v; +_.keepIndexed(['a','b','c'], function(k, v) { + if (k === 1) return v; }); //=> ['b'] @@ -291,7 +291,7 @@ _.splitAt([1,2,3,4,5], 20000); //=> [[1,2,3,4,5],[]] _.splitAt([1,2,3,4,5], -1000); -//=> [[],[1,2,3,4,5]] +//=> [[],[1,2,3,4,5]] _.splitAt([], 0); //=> [[],[]] From fe7a2edb42954f62b3c79e1941f62a9113597946 Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Fri, 18 Dec 2020 16:43:28 +0100 Subject: [PATCH 5/7] Add documentation for _.insert (#199) --- docs/underscore.array.builders.js.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/underscore.array.builders.js.md b/docs/underscore.array.builders.js.md index 1be73d6..4214038 100644 --- a/docs/underscore.array.builders.js.md +++ b/docs/underscore.array.builders.js.md @@ -152,6 +152,20 @@ _.cycle(5, [1,2,3]); -------------------------------------------------------------------------------- +#### insert + +Given an array, an index and a value, insert the value at this index in the array. The value that was previously at this index shifts one position up, together with any values that may come after it. `_.insert` modifies the array in place and also returns it. Useful shorthand for `Array.prototype.splice.call(array, index, 0, value)`. + +```javascript +var array = [1, 2, 3]; +var result = _.insert(array, 1, 4); +//=> [1, 4, 2, 3] +result === array; +//=> true +``` + +-------------------------------------------------------------------------------- + #### interpose The `_.interpose` function takes an array and an element and returns a new array with the given element inserted betwixt every element in the original array: From 65cf10cd58912a8c4d1e5a9847db707b205565e6 Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Fri, 18 Dec 2020 16:51:13 +0100 Subject: [PATCH 6/7] Relax the array restriction on _.insert (#199) --- docs/underscore.array.builders.js.md | 2 +- underscore.array.builders.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/underscore.array.builders.js.md b/docs/underscore.array.builders.js.md index 4214038..0788d36 100644 --- a/docs/underscore.array.builders.js.md +++ b/docs/underscore.array.builders.js.md @@ -154,7 +154,7 @@ _.cycle(5, [1,2,3]); #### insert -Given an array, an index and a value, insert the value at this index in the array. The value that was previously at this index shifts one position up, together with any values that may come after it. `_.insert` modifies the array in place and also returns it. Useful shorthand for `Array.prototype.splice.call(array, index, 0, value)`. +Given an array (or array-like object), an index and a value, insert the value at this index in the array. The value that was previously at this index shifts one position up, together with any values that may come after it. `_.insert` modifies the array in place and also returns it. Useful shorthand for `Array.prototype.splice.call(array, index, 0, value)`. ```javascript var array = [1, 2, 3]; diff --git a/underscore.array.builders.js b/underscore.array.builders.js index 46aae9f..92976e4 100644 --- a/underscore.array.builders.js +++ b/underscore.array.builders.js @@ -198,11 +198,11 @@ },[]); },_.map(arguments[0],function(i){return [i];})); }, - + // 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'); + if (!_.isNumber(array.length)) throw new TypeError('Expected an array-like object as the first argument'); splice.call(array, index, 0, item); return array; } From e28b607c7aa16d6f466c6682274136e84ec0f3af Mon Sep 17 00:00:00 2001 From: Julian Gonggrijp Date: Fri, 18 Dec 2020 16:51:26 +0100 Subject: [PATCH 7/7] Drop some more trailing whitespace (#199) --- underscore.array.builders.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/underscore.array.builders.js b/underscore.array.builders.js index 92976e4..2be04f9 100644 --- a/underscore.array.builders.js +++ b/underscore.array.builders.js @@ -14,7 +14,7 @@ // Helpers // ------- - + // Create quick reference variables for speed access to core prototypes. var slice = Array.prototype.slice; var splice = Array.prototype.splice; @@ -26,7 +26,7 @@ _.mixin({ // Concatenates one or more arrays given as arguments. If given objects and - // scalars as arguments `cat` will plop them down in place in the result + // scalars as arguments `cat` will plop them down in place in the result // array. If given an `arguments` object, `cat` will treat it like an array // and concatenate it likewise. cat: function() { @@ -89,7 +89,7 @@ if (sz === 0) return array; if (sz === 1) return array; - return slice.call(_.mapcat(array, function(elem) { + return slice.call(_.mapcat(array, function(elem) { return _.cons(elem, [inter]); }), 0, -1); }, @@ -167,7 +167,7 @@ return ret; }, - // Runs its given function on the index of the elements rather than + // Runs its given function on the index of the elements rather than // the elements themselves, keeping all of the truthy values in the end. keepIndexed: function(array, pred) { return _.filter(_.map(_.range(_.size(array)), function(i) {