mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 18:17:48 +00:00
Add _.curryRight.
This commit is contained in:
147
lodash.js
147
lodash.js
@@ -15,9 +15,10 @@
|
|||||||
var BIND_FLAG = 1,
|
var BIND_FLAG = 1,
|
||||||
BIND_KEY_FLAG = 2,
|
BIND_KEY_FLAG = 2,
|
||||||
CURRY_FLAG = 4,
|
CURRY_FLAG = 4,
|
||||||
CURRY_BOUND_FLAG = 8,
|
CURRY_RIGHT_FLAG = 8,
|
||||||
PARTIAL_FLAG = 16,
|
CURRY_BOUND_FLAG = 16,
|
||||||
PARTIAL_RIGHT_FLAG = 32;
|
PARTIAL_FLAG = 32,
|
||||||
|
PARTIAL_RIGHT_FLAG = 64;
|
||||||
|
|
||||||
/** Used as the semantic version number */
|
/** Used as the semantic version number */
|
||||||
var version = '3.0.0-pre';
|
var version = '3.0.0-pre';
|
||||||
@@ -1161,7 +1162,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.bind` that creates the bound function and
|
* The base implementation of `_.bind` which creates the bound function and
|
||||||
* sets its metadata.
|
* sets its metadata.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
@@ -1391,7 +1392,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `createWrapper` that creates the wrapper and
|
* The base implementation of `createWrapper` which creates the wrapper and
|
||||||
* sets its metadata.
|
* sets its metadata.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
@@ -1411,6 +1412,7 @@
|
|||||||
var isBind = bitmask & BIND_FLAG,
|
var isBind = bitmask & BIND_FLAG,
|
||||||
isBindKey = bitmask & BIND_KEY_FLAG,
|
isBindKey = bitmask & BIND_KEY_FLAG,
|
||||||
isCurry = bitmask & CURRY_FLAG,
|
isCurry = bitmask & CURRY_FLAG,
|
||||||
|
isCurryRight = bitmask & CURRY_RIGHT_FLAG,
|
||||||
isCurryBound = bitmask & CURRY_BOUND_FLAG,
|
isCurryBound = bitmask & CURRY_BOUND_FLAG,
|
||||||
key = func;
|
key = func;
|
||||||
|
|
||||||
@@ -1428,18 +1430,21 @@
|
|||||||
if (partialRightArgs) {
|
if (partialRightArgs) {
|
||||||
args = composeArgsRight(partialRightArgs, partialRightHolders, args);
|
args = composeArgsRight(partialRightArgs, partialRightHolders, args);
|
||||||
}
|
}
|
||||||
if (isCurry) {
|
if (isCurry || isCurryRight) {
|
||||||
var newPartialHolders = getHolders(args);
|
var newPartialHolders = getHolders(args);
|
||||||
length -= newPartialHolders.length;
|
length -= newPartialHolders.length;
|
||||||
|
|
||||||
if (length < arity) {
|
if (length < arity) {
|
||||||
bitmask |= PARTIAL_FLAG;
|
bitmask |= (isCurry ? PARTIAL_FLAG : PARTIAL_RIGHT_FLAG);
|
||||||
bitmask &= ~PARTIAL_RIGHT_FLAG
|
bitmask &= ~(isCurry ? PARTIAL_RIGHT_FLAG : PARTIAL_FLAG);
|
||||||
|
|
||||||
if (!isCurryBound) {
|
if (!isCurryBound) {
|
||||||
bitmask &= ~(BIND_FLAG | BIND_KEY_FLAG);
|
bitmask &= ~(BIND_FLAG | BIND_KEY_FLAG);
|
||||||
}
|
}
|
||||||
var newArity = nativeMax(arity - length, 0);
|
var newData = [func, bitmask, nativeMax(arity - length, 0), thisArg];
|
||||||
return baseCreateWrapper([func, bitmask, newArity, thisArg, args, null, newPartialHolders]);
|
newData[isCurry ? 4 : 5] = args;
|
||||||
|
newData[isCurry ? 6 : 7] = newPartialHolders;
|
||||||
|
return baseCreateWrapper(newData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var thisBinding = isBind ? thisArg : this;
|
var thisBinding = isBind ? thisArg : this;
|
||||||
@@ -1458,7 +1463,24 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.difference` that accepts a single array
|
* The base implementation of `_.curry` and `_.curryRight` which handles
|
||||||
|
* resolving the default arity of `func`.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Function} func The function to curry.
|
||||||
|
* @param {number} bitmask The bitmask of flags to compose.
|
||||||
|
* @param {number} [arity=func.length] The arity of `func`.
|
||||||
|
* @returns {Function} Returns the new curried function.
|
||||||
|
*/
|
||||||
|
function baseCurry(func, bitmask, arity) {
|
||||||
|
if (typeof arity != 'number') {
|
||||||
|
arity = +arity || (func ? func.length : 0);
|
||||||
|
}
|
||||||
|
return createWrapper(func, bitmask, arity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The base implementation of `_.difference` which accepts a single array
|
||||||
* of values to exclude.
|
* of values to exclude.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
@@ -1714,7 +1736,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.functions` that creates an array of function
|
* The base implementation of `_.functions` which creates an array of function
|
||||||
* property names from those returned by `keysFunc`.
|
* property names from those returned by `keysFunc`.
|
||||||
*
|
*
|
||||||
* @private
|
* @private
|
||||||
@@ -2019,6 +2041,26 @@
|
|||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The base implementation of `_.partial` and `_.partialRight` which handles
|
||||||
|
* resolving the arity of `func`.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {Function} func The function to partially apply arguments to.
|
||||||
|
* @param {number} bitmask The bitmask of flags to compose.
|
||||||
|
* @param {Array} args The array of arguments to be partially applied.
|
||||||
|
* @param {*} [thisArg] The `this` binding of `func`.
|
||||||
|
* @returns {Function} Returns the new partially applied function.
|
||||||
|
*/
|
||||||
|
function basePartial(func, bitmask, args, thisArg) {
|
||||||
|
if (func) {
|
||||||
|
var arity = func[expando] ? func[expando][2] : func.length;
|
||||||
|
arity -= args.length;
|
||||||
|
}
|
||||||
|
var isPartial = bitmask & PARTIAL_FLAG;
|
||||||
|
return createWrapper(func, bitmask, arity, thisArg, isPartial && args, !isPartial && args);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base implementation of `_.pick` without support for `this` binding
|
* The base implementation of `_.pick` without support for `this` binding
|
||||||
* and individual property name arguments.
|
* and individual property name arguments.
|
||||||
@@ -2382,9 +2424,10 @@
|
|||||||
* 1 - `_.bind`
|
* 1 - `_.bind`
|
||||||
* 2 - `_.bindKey`
|
* 2 - `_.bindKey`
|
||||||
* 4 - `_.curry`
|
* 4 - `_.curry`
|
||||||
* 8 - `_.curry` (bound)
|
* 8 - `_.curryRight`
|
||||||
* 16 - `_.partial`
|
* 16 - `_.curry` or `_.curryRight` of a bound function
|
||||||
* 32 - `_.partialRight`
|
* 32 - `_.partial`
|
||||||
|
* 64 - `_.partialRight`
|
||||||
* @param {number} [arity] The arity of `func`.
|
* @param {number} [arity] The arity of `func`.
|
||||||
* @param {*} [thisArg] The `this` binding of `func`.
|
* @param {*} [thisArg] The `this` binding of `func`.
|
||||||
* @param {Array} [partialArgs] An array of arguments to prepend to those
|
* @param {Array} [partialArgs] An array of arguments to prepend to those
|
||||||
@@ -5221,16 +5264,9 @@
|
|||||||
* // => 'hi fred'
|
* // => 'hi fred'
|
||||||
*/
|
*/
|
||||||
function bind(func, thisArg) {
|
function bind(func, thisArg) {
|
||||||
if (arguments.length < 3) {
|
return arguments.length < 3
|
||||||
return createWrapper(func, BIND_FLAG, null, thisArg);
|
? createWrapper(func, BIND_FLAG, null, thisArg)
|
||||||
}
|
: basePartial(func, BIND_FLAG | PARTIAL_FLAG, slice(arguments, 2), thisArg);
|
||||||
if (func) {
|
|
||||||
var arity = func[expando] ? func[expando][2] : func.length,
|
|
||||||
partialArgs = slice(arguments, 2);
|
|
||||||
|
|
||||||
arity -= partialArgs.length;
|
|
||||||
}
|
|
||||||
return createWrapper(func, BIND_FLAG | PARTIAL_FLAG, arity, thisArg, partialArgs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -5398,23 +5434,51 @@
|
|||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* var curried = _.curry(function(a, b, c) {
|
* var curried = _.curry(function(a, b, c) {
|
||||||
* console.log(a + b + c);
|
* console.log([a, b, c]);
|
||||||
* });
|
* });
|
||||||
*
|
*
|
||||||
* curried(1)(2)(3);
|
* curried(1)(2)(3);
|
||||||
* // => 6
|
* // => [1, 2, 3]
|
||||||
*
|
*
|
||||||
* curried(1, 2)(3);
|
* curried(1, 2)(3);
|
||||||
* // => 6
|
* // => [1, 2, 3]
|
||||||
*
|
*
|
||||||
* curried(1, 2, 3);
|
* curried(1, 2, 3);
|
||||||
* // => 6
|
* // => [1, 2, 3]
|
||||||
*/
|
*/
|
||||||
function curry(func, arity) {
|
function curry(func, arity) {
|
||||||
if (typeof arity != 'number') {
|
return baseCurry(func, CURRY_FLAG, arity);
|
||||||
arity = +arity || (func ? func.length : 0);
|
}
|
||||||
}
|
|
||||||
return createWrapper(func, CURRY_FLAG, arity);
|
/**
|
||||||
|
* This method is like `_.curry` except that arguments are applied from right
|
||||||
|
* to left.
|
||||||
|
*
|
||||||
|
* Note: This method does not set the `length` property of curried functions.
|
||||||
|
*
|
||||||
|
* @static
|
||||||
|
* @memberOf _
|
||||||
|
* @category Function
|
||||||
|
* @param {Function} func The function to curry.
|
||||||
|
* @param {number} [arity=func.length] The arity of `func`.
|
||||||
|
* @returns {Function} Returns the new curried function.
|
||||||
|
* @example
|
||||||
|
*
|
||||||
|
* var curried = _.curryRight(function(a, b, c) {
|
||||||
|
* console.log([a, b, c]);
|
||||||
|
* });
|
||||||
|
*
|
||||||
|
* curried(3)(2)(1);
|
||||||
|
* // => [1, 2, 3]
|
||||||
|
*
|
||||||
|
* curried(3, 2)(1);
|
||||||
|
* // => [1, 2, 3]
|
||||||
|
*
|
||||||
|
* curried(3, 2, 1);
|
||||||
|
* // => [1, 2, 3]
|
||||||
|
*/
|
||||||
|
function curryRight(func, arity) {
|
||||||
|
return baseCurry(func, CURRY_RIGHT_FLAG, arity);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -5777,13 +5841,7 @@
|
|||||||
* // => 'hello fred'
|
* // => 'hello fred'
|
||||||
*/
|
*/
|
||||||
function partial(func) {
|
function partial(func) {
|
||||||
if (func) {
|
return basePartial(func, PARTIAL_FLAG, slice(arguments, 1));
|
||||||
var arity = func[expando] ? func[expando][2] : func.length,
|
|
||||||
partialArgs = slice(arguments, 1);
|
|
||||||
|
|
||||||
arity -= partialArgs.length;
|
|
||||||
}
|
|
||||||
return createWrapper(func, PARTIAL_FLAG, arity, null, partialArgs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -5818,13 +5876,7 @@
|
|||||||
* // => { 'a': { 'b': { 'c': 1, 'd': 2 } } }
|
* // => { 'a': { 'b': { 'c': 1, 'd': 2 } } }
|
||||||
*/
|
*/
|
||||||
function partialRight(func) {
|
function partialRight(func) {
|
||||||
if (func) {
|
return basePartial(func, PARTIAL_RIGHT_FLAG, slice(arguments, 1));
|
||||||
var arity = func[expando] ? func[expando][2] : func.length,
|
|
||||||
partialRightArgs = slice(arguments, 1);
|
|
||||||
|
|
||||||
arity -= partialRightArgs.length;
|
|
||||||
}
|
|
||||||
return createWrapper(func, PARTIAL_RIGHT_FLAG, arity, null, null, partialRightArgs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -8612,6 +8664,7 @@
|
|||||||
lodash.countBy = countBy;
|
lodash.countBy = countBy;
|
||||||
lodash.create = create;
|
lodash.create = create;
|
||||||
lodash.curry = curry;
|
lodash.curry = curry;
|
||||||
|
lodash.curryRight = curryRight;
|
||||||
lodash.debounce = debounce;
|
lodash.debounce = debounce;
|
||||||
lodash.defaults = defaults;
|
lodash.defaults = defaults;
|
||||||
lodash.defer = defer;
|
lodash.defer = defer;
|
||||||
|
|||||||
@@ -11142,6 +11142,7 @@
|
|||||||
'bind',
|
'bind',
|
||||||
'compose',
|
'compose',
|
||||||
'curry',
|
'curry',
|
||||||
|
'curryRight',
|
||||||
'debounce',
|
'debounce',
|
||||||
'defer',
|
'defer',
|
||||||
'delay',
|
'delay',
|
||||||
@@ -11223,7 +11224,7 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should throw a TypeError for falsey arguments', 15, function() {
|
test('should throw a TypeError for falsey arguments', 16, function() {
|
||||||
_.each(rejectFalsey, function(methodName) {
|
_.each(rejectFalsey, function(methodName) {
|
||||||
var expected = _.map(falsey, _.constant(true)),
|
var expected = _.map(falsey, _.constant(true)),
|
||||||
func = _[methodName];
|
func = _[methodName];
|
||||||
|
|||||||
Reference in New Issue
Block a user