Remove overloadWrapper and ensure Arrays methods work with arguments objects as secondary arguments.

Former-commit-id: 0b5f213947496622e79daea74e525e60ca53e8eb
This commit is contained in:
John-David Dalton
2013-07-13 18:22:31 -07:00
parent f1b1fee6e4
commit 8694364aa3
7 changed files with 390 additions and 384 deletions

127
lodash.js
View File

@@ -234,7 +234,7 @@
}
/**
* Used by `sortBy` to compare transformed `collection` values, stable sorting
* Used by `sortBy` to compare transformed `collection` elements, stable sorting
* them in ascending order.
*
* @private
@@ -974,24 +974,22 @@
*
* @private
* @param {Array} array The array to flatten.
* @param {Boolean} [isShallow=false] A flag to indicate only flattening a single level.
* @param {Function} [callback] The function called per iteration.
* @param {Boolean} [isShallow=false] A flag to restrict flattening to a single level.
* @param {Boolean} [isArgArrays=false] A flag to restrict flattening to arrays and `arguments` objects.
* @param {Number} [fromIndex=0] The index to start from.
* @returns {Array} Returns a new flattened array.
*/
function basicFlatten(array, isShallow, callback) {
var index = -1,
function basicFlatten(array, isShallow, isArgArrays, fromIndex) {
var index = (fromIndex || 0) - 1,
length = array ? array.length : 0,
result = [];
while (++index < length) {
var value = array[index];
if (callback) {
value = callback(value, index, array);
}
// recursively flatten arrays (susceptible to call stack limits)
if (value && typeof value == 'object' && (isArray(value) || isArguments(value))) {
push.apply(result, isShallow ? value : basicFlatten(value));
} else {
} else if (!isArgArrays) {
result.push(value);
}
}
@@ -1175,7 +1173,7 @@
}
// fallback for browsers without `Object.create`
if (!nativeCreate) {
var createObject = function(prototype) {
createObject = function(prototype) {
if (isObject(prototype)) {
noop.prototype = prototype;
var result = new noop;
@@ -1204,34 +1202,11 @@
* @private
* @returns {Function} Returns the "indexOf" function.
*/
function getIndexOf(array, value, fromIndex) {
function getIndexOf() {
var result = (result = lodash.indexOf) === indexOf ? basicIndexOf : result;
return result;
}
/**
* Creates a function that juggles arguments, allowing argument overloading
* for `_.flatten` and `_.uniq`, before passing them to the given `func`.
*
* @private
* @param {Function} func The function to wrap.
* @returns {Function} Returns the new function.
*/
function overloadWrapper(func) {
return function(array, flag, callback, thisArg) {
// juggle arguments
if (typeof flag != 'boolean' && flag != null) {
thisArg = callback;
callback = !(thisArg && thisArg[flag] === array) ? flag : undefined;
flag = false;
}
if (callback != null) {
callback = lodash.createCallback(callback, thisArg);
}
return func(array, flag, callback, thisArg);
};
}
/**
* A fallback implementation of `isPlainObject` which checks if a given `value`
* is an object created by the `Object` constructor, assuming objects created
@@ -1742,8 +1717,8 @@
var forOwn = createIterator(eachIteratorOptions, forOwnIteratorOptions);
/**
* Creates a sorted array of all enumerable properties, own and inherited,
* of `object` that have function values.
* Creates a sorted array of property names of all enumerable properties,
* own and inherited, of `object` that have function values.
*
* @static
* @memberOf _
@@ -2516,7 +2491,7 @@
if (isFunc) {
callback = lodash.createCallback(callback, thisArg);
} else {
var props = basicFlatten(nativeSlice.call(arguments, 1));
var props = basicFlatten(arguments, true, false, 1);
}
forIn(object, function(value, key, object) {
if (isFunc
@@ -2586,7 +2561,7 @@
var result = {};
if (typeof callback != 'function') {
var index = -1,
props = basicFlatten(nativeSlice.call(arguments, 1)),
props = basicFlatten(arguments, true, false, 1),
length = isObject(object) ? props.length : 0;
while (++index < length) {
@@ -2707,7 +2682,7 @@
*/
function at(collection) {
var index = -1,
props = basicFlatten(nativeSlice.call(arguments, 1)),
props = basicFlatten(arguments, true, false, 1),
length = props.length,
result = Array(length);
@@ -3712,7 +3687,7 @@
* @memberOf _
* @category Arrays
* @param {Array} array The array to compact.
* @returns {Array} Returns a new filtered array.
* @returns {Array} Returns a new array of filtered values.
* @example
*
* _.compact([0, 1, false, 2, '', 3]);
@@ -3733,17 +3708,15 @@
}
/**
* Creates an arrat with all occurrences of the passed values removed using
* using strict equality for comparisons, i.e. `===`. Values to exclude may
* be specified as individual arguments or as arrays.
* Creates an array excluding all values of the passed-in arrays using
* strict equality for comparisons, i.e. `===`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to process.
* @param {Array} [array1, array2, ...] The values to exclude, specified as
* individual values or arrays of values.
* @returns {Array} Returns a new filtered array.
* @param {Array} [array1, array2, ...] The arrays of values to exclude.
* @returns {Array} Returns a new array of filtered values.
* @example
*
* _.difference([1, 2, 3, 4, 5], [5, 2, 10]);
@@ -3753,7 +3726,7 @@
var index = -1,
indexOf = getIndexOf(),
length = array ? array.length : 0,
seen = basicFlatten(nativeSlice.call(arguments, 1)),
seen = basicFlatten(arguments, true, true, 1),
result = [];
var isLarge = length >= largeArraySize && indexOf === basicIndexOf;
@@ -3908,7 +3881,7 @@
* @memberOf _
* @category Arrays
* @param {Array} array The array to flatten.
* @param {Boolean} [isShallow=false] A flag to indicate only flattening a single level.
* @param {Boolean} [isShallow=false] A flag to restrict flattening to a single level.
* @param {Function|Object|String} [callback=identity] The function called per
* iteration. If a property name or object is passed, it will be used to create
* a "_.pluck" or "_.where" style callback, respectively.
@@ -3931,7 +3904,18 @@
* _.flatten(stooges, 'quotes');
* // => ['Oh, a wise guy, eh?', 'Poifect!', 'Spread out!', 'You knucklehead!']
*/
var flatten = overloadWrapper(basicFlatten);
function flatten(array, isShallow, callback, thisArg) {
// juggle arguments
if (typeof isShallow != 'boolean' && isShallow != null) {
thisArg = callback;
callback = !(thisArg && thisArg[isShallow] === array) ? isShallow : undefined;
isShallow = false;
}
if (callback != null) {
array = map(array, callback, thisArg);
}
return basicFlatten(array, isShallow);
}
/**
* Gets the index at which the first occurrence of `value` is found using
@@ -4044,15 +4028,14 @@
}
/**
* Computes the intersection of all the passed-in arrays using strict equality
* for comparisons, i.e. `===`.
* Creates an array of unique values present in all passed-in arrays using
* strict equality for comparisons, i.e. `===`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} [array1, array2, ...] Arrays to process.
* @returns {Array} Returns a new array of unique elements that are present
* in **all** of the arrays.
* @param {Array} [array1, array2, ...] The arrays to inspect.
* @returns {Array} Returns an array of composite values.
* @example
*
* _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]);
@@ -4403,22 +4386,21 @@
}
/**
* Computes the union of the passed-in arrays using strict equality for
* comparisons, i.e. `===`.
* Creates an array of unique values, in order, of the passed-in arrays
* using strict equality for comparisons, i.e. `===`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} [array1, array2, ...] Arrays to process.
* @returns {Array} Returns a new array of unique values, in order, that are
* present in one or more of the arrays.
* @param {Array} [array1, array2, ...] The arrays to inspect.
* @returns {Array} Returns an array of composite values.
* @example
*
* _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
* // => [1, 2, 3, 101, 10]
*/
function union(array) {
return basicUniq(basicFlatten(compact(arguments), true));
return basicUniq(basicFlatten(arguments, true, true));
}
/**
@@ -4464,18 +4446,29 @@
* _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
* // => [{ 'x': 1 }, { 'x': 2 }]
*/
var uniq = overloadWrapper(basicUniq);
function uniq(array, isSorted, callback, thisArg) {
// juggle arguments
if (typeof isSorted != 'boolean' && isSorted != null) {
thisArg = callback;
callback = !(thisArg && thisArg[isSorted] === array) ? isSorted : undefined;
isSorted = false;
}
if (callback != null) {
callback = lodash.createCallback(callback, thisArg);
}
return basicUniq(array, isSorted, callback);
}
/**
* Creates an array excluding all occurrences of the passed values using
* strict equality for comparisons, i.e. `===`.
* Creates an array excluding all passed values using strict equality for
* comparisons, i.e. `===`.
*
* @static
* @memberOf _
* @category Arrays
* @param {Array} array The array to filter.
* @param {Mixed} [value1, value2, ...] Values to exclude.
* @returns {Array} Returns a new filtered array.
* @param {Mixed} [value1, value2, ...] The values to exclude.
* @returns {Array} Returns a new array of filtered values.
* @example
*
* _.without([1, 2, 1, 0, 3, 1, 4], 0, 1);
@@ -4617,7 +4610,7 @@
* @category Functions
* @param {Object} object The object to bind and assign the bound methods to.
* @param {String} [methodName1, methodName2, ...] The object method names to
* bind, specified as individual values or arrays of values.
* bind, specified as individual method names or arrays of method names.
* @returns {Object} Returns `object`.
* @example
*
@@ -4631,7 +4624,7 @@
* // => alerts 'clicked docs', when the button is clicked
*/
function bindAll(object) {
var funcs = arguments.length > 1 ? basicFlatten(nativeSlice.call(arguments, 1)) : functions(object),
var funcs = arguments.length > 1 ? basicFlatten(arguments, true, false, 1) : functions(object),
index = -1,
length = funcs.length;