From e5e8f35c066c71a04ba584f65acc017d032c0174 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 17 Apr 2017 21:03:39 -0700 Subject: [PATCH] Remove `baseSlice`. --- .internal/baseSlice.js | 31 ------------------------------- .internal/baseWhile.js | 6 +++--- .internal/castSlice.js | 4 ++-- .internal/parent.js | 4 ++-- chunk.js | 4 ++-- drop.js | 4 ++-- dropRight.js | 4 ++-- initial.js | 4 ++-- slice.js | 22 ++++++++++++++++++---- take.js | 4 ++-- takeRight.js | 4 ++-- 11 files changed, 37 insertions(+), 54 deletions(-) delete mode 100644 .internal/baseSlice.js diff --git a/.internal/baseSlice.js b/.internal/baseSlice.js deleted file mode 100644 index 452364c1b..000000000 --- a/.internal/baseSlice.js +++ /dev/null @@ -1,31 +0,0 @@ -/** - * The base implementation of `slice`. - * - * @private - * @param {Array} array The array to slice. - * @param {number} [start=0] The start position. - * @param {number} [end=array.length] The end position. - * @returns {Array} Returns the slice of `array`. - */ -function baseSlice(array, start, end) { - let index = -1 - let { length } = array - - if (start < 0) { - start = -start > length ? 0 : (length + start) - } - end = end > length ? length : end - if (end < 0) { - end += length - } - length = start > end ? 0 : ((end - start) >>> 0) - start >>>= 0 - - const result = new Array(length) - while (++index < length) { - result[index] = array[index + start] - } - return result -} - -export default baseSlice diff --git a/.internal/baseWhile.js b/.internal/baseWhile.js index 83728b2aa..a024ffb81 100644 --- a/.internal/baseWhile.js +++ b/.internal/baseWhile.js @@ -1,4 +1,4 @@ -import baseSlice from './baseSlice.js' +import slice from '../slice.js' /** * The base implementation of methods like `dropWhile` and `takeWhile`. @@ -18,8 +18,8 @@ function baseWhile(array, predicate, isDrop, fromRight) { predicate(array[index], index, array)) {} return isDrop - ? baseSlice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length)) - : baseSlice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index)) + ? slice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length)) + : slice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index)) } export default baseWhile diff --git a/.internal/castSlice.js b/.internal/castSlice.js index 88e513e8f..56a0699a5 100644 --- a/.internal/castSlice.js +++ b/.internal/castSlice.js @@ -1,4 +1,4 @@ -import baseSlice from './baseSlice.js' +import slice from '../slice.js' /** * Casts `array` to a slice if it's needed. @@ -12,7 +12,7 @@ import baseSlice from './baseSlice.js' function castSlice(array, start, end) { const { length } = array end = end === undefined ? length : end - return (!start && end >= length) ? array : baseSlice(array, start, end) + return (!start && end >= length) ? array : slice(array, start, end) } export default castSlice diff --git a/.internal/parent.js b/.internal/parent.js index 04e5a01da..12180de6c 100644 --- a/.internal/parent.js +++ b/.internal/parent.js @@ -1,5 +1,5 @@ import baseGet from './baseGet.js' -import baseSlice from './baseSlice.js' +import slice from '../slice.js' /** * Gets the parent value at `path` of `object`. @@ -10,7 +10,7 @@ import baseSlice from './baseSlice.js' * @returns {*} Returns the parent value. */ function parent(object, path) { - return path.length < 2 ? object : baseGet(object, baseSlice(path, 0, -1)) + return path.length < 2 ? object : baseGet(object, slice(path, 0, -1)) } export default parent diff --git a/chunk.js b/chunk.js index 5e4b50a02..f06b0e93b 100644 --- a/chunk.js +++ b/chunk.js @@ -1,4 +1,4 @@ -import baseSlice from './.internal/baseSlice.js' +import slice from './slice.js' /** * Creates an array of elements split into groups the length of `size`. @@ -29,7 +29,7 @@ function chunk(array, size) { const result = new Array(Math.ceil(length / size)) while (index < length) { - result[resIndex++] = baseSlice(array, index, (index += size)) + result[resIndex++] = slice(array, index, (index += size)) } return result } diff --git a/drop.js b/drop.js index 0ebeb09cc..4df218589 100644 --- a/drop.js +++ b/drop.js @@ -1,4 +1,4 @@ -import baseSlice from './.internal/baseSlice.js' +import slice from './slice.js' /** * Creates a slice of `array` with `n` elements dropped from the beginning. @@ -25,7 +25,7 @@ import baseSlice from './.internal/baseSlice.js' function drop(array, n=1) { const length = array == null ? 0 : array.length return length - ? baseSlice(array, n < 0 ? 0 : n, length) + ? slice(array, n < 0 ? 0 : n, length) : [] } diff --git a/dropRight.js b/dropRight.js index 3198d97e4..7e95c771c 100644 --- a/dropRight.js +++ b/dropRight.js @@ -1,4 +1,4 @@ -import baseSlice from './.internal/baseSlice.js' +import slice from './slice.js' /** * Creates a slice of `array` with `n` elements dropped from the end. @@ -28,7 +28,7 @@ function dropRight(array, n=1) { return [] } n = length - n - return baseSlice(array, 0, n < 0 ? 0 : n) + return slice(array, 0, n < 0 ? 0 : n) } export default dropRight diff --git a/initial.js b/initial.js index 96c3ce6ce..1a4b1d837 100644 --- a/initial.js +++ b/initial.js @@ -1,4 +1,4 @@ -import baseSlice from './.internal/baseSlice.js' +import slice from './slice.js' /** * Gets all but the last element of `array`. @@ -14,7 +14,7 @@ import baseSlice from './.internal/baseSlice.js' */ function initial(array) { const length = array == null ? 0 : array.length - return length ? baseSlice(array, 0, -1) : [] + return length ? slice(array, 0, -1) : [] } export default initial diff --git a/slice.js b/slice.js index cc89999d4..217c8a360 100644 --- a/slice.js +++ b/slice.js @@ -1,5 +1,3 @@ -import baseSlice from './.internal/baseSlice.js' - /** * Creates a slice of `array` from `start` up to, but not including, `end`. * @@ -15,13 +13,29 @@ import baseSlice from './.internal/baseSlice.js' * @returns {Array} Returns the slice of `array`. */ function slice(array, start, end) { - const length = array == null ? 0 : array.length + let length = array == null ? 0 : array.length if (!length) { return [] } start = start == null ? 0 : start end = end === undefined ? length : end - return baseSlice(array, start, end) + + if (start < 0) { + start = -start > length ? 0 : (length + start) + } + end = end > length ? length : end + if (end < 0) { + end += length + } + length = start > end ? 0 : ((end - start) >>> 0) + start >>>= 0 + + let index = -1 + const result = new Array(length) + while (++index < length) { + result[index] = array[index + start] + } + return result } export default slice diff --git a/take.js b/take.js index 0579de172..890118b9b 100644 --- a/take.js +++ b/take.js @@ -1,4 +1,4 @@ -import baseSlice from './.internal/baseSlice.js' +import slice from './slice.js' /** * Creates a slice of `array` with `n` elements taken from the beginning. @@ -26,7 +26,7 @@ function take(array, n=1) { if (!(array != null && array.length)) { return [] } - return baseSlice(array, 0, n < 0 ? 0 : n) + return slice(array, 0, n < 0 ? 0 : n) } export default take diff --git a/takeRight.js b/takeRight.js index fb09b1b01..3ad2a57a7 100644 --- a/takeRight.js +++ b/takeRight.js @@ -1,4 +1,4 @@ -import baseSlice from './.internal/baseSlice.js' +import slice from './slice.js' /** * Creates a slice of `array` with `n` elements taken from the end. @@ -28,7 +28,7 @@ function takeRight(array, n=1) { return [] } n = length - n - return baseSlice(array, n < 0 ? 0 : n, length) + return slice(array, n < 0 ? 0 : n, length) } export default takeRight