Expose _.slice.

This commit is contained in:
John-David Dalton
2014-01-06 22:26:50 -08:00
parent 16dfdbe314
commit d309eb8fa1
8 changed files with 447 additions and 314 deletions

View File

@@ -486,34 +486,6 @@
}
}
/**
* Slices the `collection` from the `start` index up to, but not including,
* the `end` index.
*
* Note: This function is used instead of `Array#slice` to support node lists
* in IE < 9 and to ensure dense arrays are returned.
*
* @private
* @param {Array|Object|string} collection The collection to slice.
* @param {number} start The start index.
* @param {number} end The end index.
* @returns {Array} Returns the new array.
*/
function slice(array, start, end) {
start || (start = 0);
if (typeof end == 'undefined') {
end = array ? array.length : 0;
}
var index = -1,
length = end - start || 0,
result = Array(length < 0 ? 0 : length);
while (++index < length) {
result[index] = array[start + index];
}
return result;
}
/**
* Gets the index of the first non-whitespace character of `string`.
*
@@ -2245,7 +2217,7 @@
return array ? array[0] : undefined;
}
}
return slice(array, 0, nativeMin(nativeMax(0, n), length));
return slice(array, 0, n > 0 ? n : 0);
}
/**
@@ -2405,7 +2377,8 @@
} else {
n = (callback == null || thisArg) ? 1 : callback || n;
}
return slice(array, 0, nativeMin(nativeMax(0, length - n), length));
n = length - n;
return slice(array, 0, n > 0 ? n : 0);
}
/**
@@ -2540,7 +2513,8 @@
return array ? array[length - 1] : undefined;
}
}
return slice(array, nativeMax(0, length - n));
n = length - n;
return slice(array, n > 0 ? n : 0);
}
/**
@@ -2786,12 +2760,55 @@
while (++index < length && callback(array[index], index, array)) {
n++;
}
} else if (callback == null || thisArg) {
n = 1;
} else {
n = (callback == null || thisArg) ? 1 : nativeMax(0, callback);
n = callback > 0 ? callback : 0;
}
return slice(array, n);
}
/**
* Slices `array` from the `start` index up to, but not including, the `end` index.
*
* Note: This function is used instead of `Array#slice` to support node lists
* in IE < 9 and to ensure dense arrays are returned.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to slice.
* @param {number} [start=0] The start index.
* @param {number} [end=array.length] The end index.
* @returns {Array} Returns the new array.
*/
function slice(array, start, end) {
var index = -1,
length = array ? array.length : 0;
if (typeof start == 'undefined') {
start = 0;
} else if (start < 0) {
start = nativeMax(length + start, 0);
} else if (start > length) {
start = length;
}
if (typeof end == 'undefined') {
end = length;
} else if (end < 0) {
end = nativeMax(length + end, 0);
} else if (end > length) {
end = length;
}
length = end - start || 0;
var result = Array(length);
while (++index < length) {
result[index] = array[start + index];
}
return result;
}
/**
* Uses a binary search to determine the smallest index at which a value
* should be inserted into a given sorted array in order to maintain the sort
@@ -7219,6 +7236,7 @@
lodash.remove = remove;
lodash.rest = rest;
lodash.shuffle = shuffle;
lodash.slice = slice;
lodash.sortBy = sortBy;
lodash.tap = tap;
lodash.throttle = throttle;
@@ -7392,7 +7410,7 @@
});
// add `Array` functions that return new wrapped values
baseEach(['concat', 'slice', 'splice'], function(methodName) {
baseEach(['concat', 'splice'], function(methodName) {
var func = arrayRef[methodName];
lodash.prototype[methodName] = function() {
return new lodashWrapper(func.apply(this.__wrapped__, arguments), this.__chain__);