From 37cd5dc97a50bc5e955863ce4e80dd99442dfbde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Am=C3=A9rico?= Date: Fri, 16 Aug 2019 19:52:06 -0300 Subject: [PATCH] Coerce drop and dropRight n param using toInteger (#4412) * Coerce dropRight n param using toInteger * Coerce drop n param using toInteger --- drop.js | 3 ++- dropRight.js | 4 +++- test/{drop.js => drop.test.js} | 7 ------- test/{dropRight.js => dropRight.test.js} | 7 ------- 4 files changed, 5 insertions(+), 16 deletions(-) rename test/{drop.js => drop.test.js} (89%) rename test/{dropRight.js => dropRight.test.js} (89%) diff --git a/drop.js b/drop.js index 4df218589..438f1e201 100644 --- a/drop.js +++ b/drop.js @@ -1,4 +1,5 @@ import slice from './slice.js' +import toInteger from './toInteger.js' /** * Creates a slice of `array` with `n` elements dropped from the beginning. @@ -25,7 +26,7 @@ import slice from './slice.js' function drop(array, n=1) { const length = array == null ? 0 : array.length return length - ? slice(array, n < 0 ? 0 : n, length) + ? slice(array, n < 0 ? 0 : toInteger(n), length) : [] } diff --git a/dropRight.js b/dropRight.js index 774dd09c6..2f023aeea 100644 --- a/dropRight.js +++ b/dropRight.js @@ -1,4 +1,5 @@ import slice from './slice.js' +import toInteger from './toInteger.js' /** * Creates a slice of `array` with `n` elements dropped from the end. @@ -24,7 +25,8 @@ import slice from './slice.js' */ function dropRight(array, n=1) { const length = array == null ? 0 : array.length - return length ? slice(array, 0, n < 0 ? 0 : -n) : [] + n = length - toInteger(n) + return length ? slice(array, 0, n < 0 ? 0 : n) : [] } export default dropRight diff --git a/test/drop.js b/test/drop.test.js similarity index 89% rename from test/drop.js rename to test/drop.test.js index 13aadd8f9..5011c70c0 100644 --- a/test/drop.js +++ b/test/drop.test.js @@ -38,13 +38,6 @@ describe('drop', function() { assert.deepStrictEqual(drop(array, 1.6), [2, 3]); }); - it('should work as an iteratee for methods like `_.map`', function() { - var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], - actual = lodashStable.map(array, drop); - - assert.deepStrictEqual(actual, [[2, 3], [5, 6], [8, 9]]); - }); - it('should work in a lazy sequence', function() { var array = lodashStable.range(1, LARGE_ARRAY_SIZE + 1), predicate = function(value) { values.push(value); return isEven(value); }, diff --git a/test/dropRight.js b/test/dropRight.test.js similarity index 89% rename from test/dropRight.js rename to test/dropRight.test.js index 5966621ab..040ecc6cf 100644 --- a/test/dropRight.js +++ b/test/dropRight.test.js @@ -38,13 +38,6 @@ describe('dropRight', function() { assert.deepStrictEqual(dropRight(array, 1.6), [1, 2]); }); - it('should work as an iteratee for methods like `_.map`', function() { - var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], - actual = lodashStable.map(array, dropRight); - - assert.deepStrictEqual(actual, [[1, 2], [4, 5], [7, 8]]); - }); - it('should work in a lazy sequence', function() { var array = lodashStable.range(1, LARGE_ARRAY_SIZE + 1), predicate = function(value) { values.push(value); return isEven(value); },