Remove baseSlice.

This commit is contained in:
John-David Dalton
2017-04-17 21:03:39 -07:00
parent a023f92049
commit e5e8f35c06
11 changed files with 37 additions and 54 deletions

View File

@@ -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

View File

@@ -1,4 +1,4 @@
import baseSlice from './baseSlice.js' import slice from '../slice.js'
/** /**
* The base implementation of methods like `dropWhile` and `takeWhile`. * The base implementation of methods like `dropWhile` and `takeWhile`.
@@ -18,8 +18,8 @@ function baseWhile(array, predicate, isDrop, fromRight) {
predicate(array[index], index, array)) {} predicate(array[index], index, array)) {}
return isDrop return isDrop
? baseSlice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length)) ? slice(array, (fromRight ? 0 : index), (fromRight ? index + 1 : length))
: baseSlice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index)) : slice(array, (fromRight ? index + 1 : 0), (fromRight ? length : index))
} }
export default baseWhile export default baseWhile

View File

@@ -1,4 +1,4 @@
import baseSlice from './baseSlice.js' import slice from '../slice.js'
/** /**
* Casts `array` to a slice if it's needed. * Casts `array` to a slice if it's needed.
@@ -12,7 +12,7 @@ import baseSlice from './baseSlice.js'
function castSlice(array, start, end) { function castSlice(array, start, end) {
const { length } = array const { length } = array
end = end === undefined ? length : end 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 export default castSlice

View File

@@ -1,5 +1,5 @@
import baseGet from './baseGet.js' import baseGet from './baseGet.js'
import baseSlice from './baseSlice.js' import slice from '../slice.js'
/** /**
* Gets the parent value at `path` of `object`. * Gets the parent value at `path` of `object`.
@@ -10,7 +10,7 @@ import baseSlice from './baseSlice.js'
* @returns {*} Returns the parent value. * @returns {*} Returns the parent value.
*/ */
function parent(object, path) { 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 export default parent

View File

@@ -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`. * 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)) const result = new Array(Math.ceil(length / size))
while (index < length) { while (index < length) {
result[resIndex++] = baseSlice(array, index, (index += size)) result[resIndex++] = slice(array, index, (index += size))
} }
return result return result
} }

View File

@@ -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. * 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) { function drop(array, n=1) {
const length = array == null ? 0 : array.length const length = array == null ? 0 : array.length
return length return length
? baseSlice(array, n < 0 ? 0 : n, length) ? slice(array, n < 0 ? 0 : n, length)
: [] : []
} }

View File

@@ -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. * Creates a slice of `array` with `n` elements dropped from the end.
@@ -28,7 +28,7 @@ function dropRight(array, n=1) {
return [] return []
} }
n = length - n n = length - n
return baseSlice(array, 0, n < 0 ? 0 : n) return slice(array, 0, n < 0 ? 0 : n)
} }
export default dropRight export default dropRight

View File

@@ -1,4 +1,4 @@
import baseSlice from './.internal/baseSlice.js' import slice from './slice.js'
/** /**
* Gets all but the last element of `array`. * Gets all but the last element of `array`.
@@ -14,7 +14,7 @@ import baseSlice from './.internal/baseSlice.js'
*/ */
function initial(array) { function initial(array) {
const length = array == null ? 0 : array.length const length = array == null ? 0 : array.length
return length ? baseSlice(array, 0, -1) : [] return length ? slice(array, 0, -1) : []
} }
export default initial export default initial

View File

@@ -1,5 +1,3 @@
import baseSlice from './.internal/baseSlice.js'
/** /**
* Creates a slice of `array` from `start` up to, but not including, `end`. * 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`. * @returns {Array} Returns the slice of `array`.
*/ */
function slice(array, start, end) { function slice(array, start, end) {
const length = array == null ? 0 : array.length let length = array == null ? 0 : array.length
if (!length) { if (!length) {
return [] return []
} }
start = start == null ? 0 : start start = start == null ? 0 : start
end = end === undefined ? length : end 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 export default slice

View File

@@ -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. * 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)) { if (!(array != null && array.length)) {
return [] return []
} }
return baseSlice(array, 0, n < 0 ? 0 : n) return slice(array, 0, n < 0 ? 0 : n)
} }
export default take export default take

View File

@@ -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. * Creates a slice of `array` with `n` elements taken from the end.
@@ -28,7 +28,7 @@ function takeRight(array, n=1) {
return [] return []
} }
n = length - n n = length - n
return baseSlice(array, n < 0 ? 0 : n, length) return slice(array, n < 0 ? 0 : n, length)
} }
export default takeRight export default takeRight