Skip to content

Commit

Permalink
Expose _.slice.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Jan 7, 2014
1 parent 4af8f46 commit 06e0c97
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 314 deletions.
87 changes: 53 additions & 34 deletions dist/lodash.compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,34 +487,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`.
*
Expand Down Expand Up @@ -1931,7 +1903,8 @@

// avoid non Object objects, `arguments` objects, and DOM elements
if (!(value && toString.call(value) == objectClass) ||
(ctor = value.constructor, isFunction(ctor) && !(ctor instanceof ctor)) ||
(!hasOwnProperty.call(value, 'constructor') &&
(ctor = value.constructor, isFunction(ctor) && !(ctor instanceof ctor))) ||
(!support.argsClass && isArguments(value)) ||
(!support.nodeClass && isNode(value))) {
return false;
Expand Down Expand Up @@ -2227,7 +2200,7 @@
return array ? array[0] : undefined;
}
}
return slice(array, 0, nativeMin(nativeMax(0, n), length));
return slice(array, 0, n > 0 ? n : 0);
}

/**
Expand Down Expand Up @@ -2387,7 +2360,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);
}

/**
Expand Down Expand Up @@ -2522,7 +2496,8 @@
return array ? array[length - 1] : undefined;
}
}
return slice(array, nativeMax(0, length - n));
n = length - n;
return slice(array, n > 0 ? n : 0);
}

/**
Expand Down Expand Up @@ -2768,12 +2743,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
Expand Down Expand Up @@ -7199,6 +7217,7 @@
lodash.remove = remove;
lodash.rest = rest;
lodash.shuffle = shuffle;
lodash.slice = slice;
lodash.sortBy = sortBy;
lodash.tap = tap;
lodash.throttle = throttle;
Expand Down Expand Up @@ -7372,7 +7391,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__);
Expand Down

0 comments on commit 06e0c97

Please sign in to comment.