From ff26080c5c378fd7af282e5b72c500832c1ddbbf Mon Sep 17 00:00:00 2001 From: Dan Allison Date: Sat, 1 Mar 2014 08:56:44 -0800 Subject: [PATCH] fix `_.removeAt` with repeated indexes --- lodash.js | 12 ++++++++++-- test/test.js | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lodash.js b/lodash.js index 673d90749..f4ee952ae 100644 --- a/lodash.js +++ b/lodash.js @@ -2927,9 +2927,17 @@ } else { removals.sort(baseCompareAscending); } - var result = Array(length); + var result = Array(length), + adjust = -1, + removal, prev; while(++index < length) { - result[index] = splice.call(array, removals[index] - index, 1)[0]; + removal = removals[index]; + if (removal === prev) { + result[index] = result[index - 1]; + continue; + } + prev = removal; + result[index] = splice.call(array, removal - ++adjust, 1)[0]; } return result; } diff --git a/test/test.js b/test/test.js index 1212c6759..69e153d3e 100644 --- a/test/test.js +++ b/test/test.js @@ -6820,6 +6820,14 @@ deepEqual(actual, [1, 2, 4, 5]); }); + test('should work with repeated indexes', 2, function() { + var array = [1, 2, 3, 4, 5]; + var actual = _.removeAt(array, [0, 0, 1, 2, 2, 2]); + + deepEqual(array, [4, 5]); + deepEqual(actual, [1, 1, 2, 3, 3, 3]); + }); + test('should return `undefined` for nonexistent keys', 2, function() { var array = ['a', 'b', 'c']; var actual = _.removeAt(array, [0, 2, 4]);