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

@@ -192,34 +192,6 @@
return '\\' + stringEscapes[match];
}
/**
* 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;
}
/**
* Used by `unescape` to convert HTML entities to characters.
*
@@ -1082,7 +1054,7 @@
return array ? array[0] : undefined;
}
}
return slice(array, 0, nativeMin(nativeMax(0, n), length));
return slice(array, 0, n > 0 ? n : 0);
}
/**
@@ -1233,7 +1205,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);
}
/**
@@ -1351,7 +1324,8 @@
return array ? array[length - 1] : undefined;
}
}
return slice(array, nativeMax(0, length - n));
n = length - n;
return slice(array, n > 0 ? n : 0);
}
/**
@@ -1512,12 +1486,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