Ensure "Arrays" and "Objects" methods work with arguments objects and arrays respectively.

Former-commit-id: aebb7a0004d804b7fd43d73e24d1da28c67f4059
This commit is contained in:
John-David Dalton
2013-04-07 15:10:03 -07:00
parent 93df901b71
commit 43037c0ff9
3 changed files with 100 additions and 32 deletions

View File

@@ -580,16 +580,15 @@
* @private
* @param {Array} array The array to search.
* @param {Mixed} value The value to search for.
* @param {Number} fromIndex The index to search from.
* @returns {Boolean} Returns `true`, if `value` is found, else `false`.
*/
function cachedContains(array, fromIndex) {
function cachedContains(array) {
var length = array.length,
isLarge = (length - fromIndex) >= largeArraySize;
isLarge = length >= largeArraySize;
if (isLarge) {
var cache = {},
index = fromIndex - 1;
index = -1;
while (++index < length) {
var key = keyPrefix + array[index];
@@ -601,7 +600,7 @@
var key = keyPrefix + value;
return cache[key] && indexOf(cache[key], value) > -1;
}
return indexOf(array, value, fromIndex) > -1;
return indexOf(array, value) > -1;
}
}
@@ -2111,12 +2110,12 @@
if (isFunc) {
callback = lodash.createCallback(callback, thisArg);
} else {
var props = concat.apply(arrayRef, arguments);
var props = concat.apply(arrayRef, nativeSlice.call(arguments, 1));
}
forIn(object, function(value, key, object) {
if (isFunc
? !callback(value, key, object)
: indexOf(props, key, 1) < 0
: indexOf(props, key) < 0
) {
result[key] = value;
}
@@ -2179,8 +2178,8 @@
function pick(object, callback, thisArg) {
var result = {};
if (typeof callback != 'function') {
var index = 0,
props = concat.apply(arrayRef, arguments),
var index = -1,
props = concat.apply(arrayRef, nativeSlice.call(arguments, 1)),
length = isObject(object) ? props.length : 0;
while (++index < length) {
@@ -3292,8 +3291,8 @@
function difference(array) {
var index = -1,
length = array ? array.length : 0,
flattened = concat.apply(arrayRef, arguments),
contains = cachedContains(flattened, length),
flattened = concat.apply(arrayRef, nativeSlice.call(arguments, 1)),
contains = cachedContains(flattened),
result = [];
while (++index < length) {
@@ -3643,7 +3642,7 @@
}
var argsIndex = argsLength;
while (--argsIndex) {
if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(args[argsIndex], 0)))(value)) {
if (!(cache[argsIndex] || (cache[argsIndex] = cachedContains(args[argsIndex])))(value)) {
continue outer;
}
}
@@ -3967,8 +3966,11 @@
* _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
* // => [1, 2, 3, 101, 10]
*/
function union() {
return uniq(concat.apply(arrayRef, arguments));
function union(array) {
return uniq(isArray(array)
? concat.apply(arrayRef, arguments)
: concat.apply(array ? nativeSlice.call(array) : arrayRef, nativeSlice.call(arguments, 1))
);
}
/**
@@ -4258,8 +4260,8 @@
* // => alerts 'clicked docs', when the button is clicked
*/
function bindAll(object) {
var funcs = concat.apply(arrayRef, arguments),
index = funcs.length > 1 ? 0 : (funcs = functions(object), -1),
var funcs = arguments.length > 1 ? concat.apply(arrayRef, nativeSlice.call(arguments, 1)) : functions(object),
index = -1,
length = funcs.length;
while (++index < length) {