Allow argCount to be omitted an not default to 3, and cleanup createBound.

Former-commit-id: cbafeaa441aae1ef28268bc2614b1a6e038acfe1
This commit is contained in:
John-David Dalton
2013-07-22 19:11:44 -07:00
parent 2c772d0f4c
commit 17e32017a7
8 changed files with 272 additions and 277 deletions

View File

@@ -173,11 +173,11 @@
/** Native method shortcuts */
var ceil = Math.ceil,
concat = arrayRef.concat,
floor = Math.floor,
hasOwnProperty = objectProto.hasOwnProperty,
push = arrayRef.push,
toString = objectProto.toString;
toString = objectProto.toString,
unshift = arrayRef.unshift;
/* Native method shortcuts for methods with the same name as other `lodash` methods */
var nativeBind = reNative.test(nativeBind = toString.bind) && nativeBind,
@@ -553,16 +553,12 @@
if (!isFunc && !isBindKey) {
throw new TypeError;
}
var key = thisArg;
// juggle arguments for `_.bindKey` behavior
if (isBindKey) {
thisArg = func;
}
// use `Function#bind` if it exists and is fast
// (in V8 `Function#bind` is slower except when partially applied)
if (!isPartial && !isAlt && !partialRightArgs.length && (support.fastBind || (nativeBind && partialArgs.length))) {
var bound = nativeBind.call.apply(nativeBind, concat.call(arrayRef, func, thisArg, partialArgs));
args = [func, thisArg];
push.apply(args, partialArgs);
var bound = nativeBind.call.apply(nativeBind, args);
}
else {
bound = function() {
@@ -575,7 +571,7 @@
func = thisArg[key];
}
if (partialArgs.length || partialRightArgs.length) {
args = concat.apply(partialArgs, args);
unshift.apply(args, partialArgs);
push.apply(args, partialRightArgs);
}
if (this instanceof bound) {
@@ -590,6 +586,10 @@
return func.apply(thisBinding, args);
};
}
if (isBindKey) {
var key = thisArg;
thisArg = func;
}
return bound;
}
@@ -1578,7 +1578,7 @@
*/
function countBy(collection, callback, thisArg) {
var result = {};
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
forEach(collection, function(value, key, collection) {
key = String(callback(value, key, collection));
@@ -1630,7 +1630,7 @@
*/
function every(collection, callback, thisArg) {
var result = true;
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
var index = -1,
length = collection ? collection.length : 0;
@@ -1691,7 +1691,7 @@
*/
function filter(collection, callback, thisArg) {
var result = [];
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
var index = -1,
length = collection ? collection.length : 0;
@@ -1757,7 +1757,7 @@
* // => { 'name': 'banana', 'organic': true, 'type': 'fruit' }
*/
function find(collection, callback, thisArg) {
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
var index = -1,
length = collection ? collection.length : 0;
@@ -1834,7 +1834,7 @@
var index = -1,
length = collection ? collection.length : 0;
callback = callback && typeof thisArg == 'undefined' ? callback : createCallback(callback, thisArg);
callback = callback && typeof thisArg == 'undefined' ? callback : createCallback(callback, thisArg, 3);
if (typeof length == 'number') {
while (++index < length) {
if (callback(collection[index], index, collection) === indicatorObject) {
@@ -1882,7 +1882,7 @@
*/
function groupBy(collection, callback, thisArg) {
var result = {};
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
forEach(collection, function(value, key, collection) {
key = String(callback(value, key, collection));
@@ -1969,7 +1969,7 @@
var index = -1,
length = collection ? collection.length : 0;
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
if (typeof length == 'number') {
var result = Array(length);
while (++index < length) {
@@ -2038,7 +2038,7 @@
}
}
} else {
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
forEach(collection, function(value, index, collection) {
var current = callback(value, index, collection);
@@ -2105,7 +2105,7 @@
}
}
} else {
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
forEach(collection, function(value, index, collection) {
var current = callback(value, index, collection);
@@ -2284,7 +2284,7 @@
* // => [{ 'name': 'carrot', 'organic': true, 'type': 'vegetable' }]
*/
function reject(collection, callback, thisArg) {
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
return filter(collection, function(value, index, collection) {
return !callback(value, index, collection);
});
@@ -2386,7 +2386,7 @@
*/
function some(collection, callback, thisArg) {
var result;
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
var index = -1,
length = collection ? collection.length : 0;
@@ -2445,7 +2445,7 @@
length = collection ? collection.length : 0,
result = Array(typeof length == 'number' ? length : 0);
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
forEach(collection, function(value, key, collection) {
result[++index] = {
'criteria': callback(value, key, collection),
@@ -2641,7 +2641,7 @@
if (typeof callback != 'number' && callback != null) {
var index = -1;
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
while (++index < length && callback(array[index], index, array)) {
n++;
}
@@ -2800,7 +2800,7 @@
if (typeof callback != 'number' && callback != null) {
var index = length;
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
while (index-- && callback(array[index], index, array)) {
n++;
}
@@ -2912,7 +2912,7 @@
if (typeof callback != 'number' && callback != null) {
var index = length;
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
while (index-- && callback(array[index], index, array)) {
n++;
}
@@ -3072,7 +3072,7 @@
index = -1,
length = array ? array.length : 0;
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
while (++index < length && callback(array[index], index, array)) {
n++;
}
@@ -3216,7 +3216,7 @@
isSorted = false;
}
if (callback != null) {
callback = createCallback(callback, thisArg);
callback = createCallback(callback, thisArg, 3);
}
return baseUniq(array, isSorted, callback);
}
@@ -3437,7 +3437,7 @@
* @category Functions
* @param {Mixed} [func=identity] The value to convert to a callback.
* @param {Mixed} [thisArg] The `this` binding of the created callback.
* @param {Number} [argCount=3] The number of arguments the callback accepts.
* @param {Number} [argCount] The number of arguments the callback accepts.
* @returns {Function} Returns a callback function.
* @example
*
@@ -3498,23 +3498,22 @@
if (typeof thisArg == 'undefined') {
return func;
}
if (argCount === 1) {
return function(value) {
switch (argCount) {
case 1: return function(value) {
return func.call(thisArg, value);
};
}
if (argCount === 2) {
return function(a, b) {
case 2: return function(a, b) {
return func.call(thisArg, a, b);
};
}
if (argCount === 4) {
return function(accumulator, value, index, collection) {
case 3: return function(value, index, collection) {
return func.call(thisArg, value, index, collection);
};
case 4: return function(accumulator, value, index, collection) {
return func.call(thisArg, accumulator, value, index, collection);
};
}
return function(value, index, collection) {
return func.call(thisArg, value, index, collection);
return function() {
return func.apply(thisArg, arguments);
};
}