mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 18:17:48 +00:00
Add createComposer to reduce the definition of _.flow and _.flowRight.
This commit is contained in:
@@ -3217,6 +3217,41 @@
|
|||||||
return new SetCache(values);
|
return new SetCache(values);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a function to compose other functions into a single function.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {boolean} [fromRight] Specify iterating from right to left.
|
||||||
|
* @returns {Function} Returns the new composer function.
|
||||||
|
*/
|
||||||
|
function createComposer(fromRight) {
|
||||||
|
return function() {
|
||||||
|
var length = arguments.length,
|
||||||
|
index = length,
|
||||||
|
fromIndex = fromRight ? length - 1 : 0;
|
||||||
|
|
||||||
|
if (!length) {
|
||||||
|
return function() { return arguments[0]; };
|
||||||
|
}
|
||||||
|
var funcs = Array(length);
|
||||||
|
while (index--) {
|
||||||
|
funcs[index] = arguments[index];
|
||||||
|
if (typeof funcs[index] != 'function') {
|
||||||
|
throw new TypeError(FUNC_ERROR_TEXT);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return function() {
|
||||||
|
var index = fromIndex,
|
||||||
|
result = funcs[index].apply(this, arguments);
|
||||||
|
|
||||||
|
while ((fromRight ? index-- : ++index < length)) {
|
||||||
|
result = funcs[index].call(this, result);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a function that produces compound words out of the words in a
|
* Creates a function that produces compound words out of the words in a
|
||||||
* given string.
|
* given string.
|
||||||
@@ -7588,26 +7623,7 @@
|
|||||||
* addSquare(1, 2);
|
* addSquare(1, 2);
|
||||||
* // => 9
|
* // => 9
|
||||||
*/
|
*/
|
||||||
function flow() {
|
var flow = createComposer();
|
||||||
var funcs = arguments,
|
|
||||||
length = funcs.length;
|
|
||||||
|
|
||||||
if (!length) {
|
|
||||||
return function() { return arguments[0]; };
|
|
||||||
}
|
|
||||||
if (!arrayEvery(funcs, baseIsFunction)) {
|
|
||||||
throw new TypeError(FUNC_ERROR_TEXT);
|
|
||||||
}
|
|
||||||
return function() {
|
|
||||||
var index = 0,
|
|
||||||
result = funcs[index].apply(this, arguments);
|
|
||||||
|
|
||||||
while (++index < length) {
|
|
||||||
result = funcs[index].call(this, result);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is like `_.flow` except that it creates a function that
|
* This method is like `_.flow` except that it creates a function that
|
||||||
@@ -7629,26 +7645,7 @@
|
|||||||
* addSquare(1, 2);
|
* addSquare(1, 2);
|
||||||
* // => 9
|
* // => 9
|
||||||
*/
|
*/
|
||||||
function flowRight() {
|
var flowRight = createComposer(true);
|
||||||
var funcs = arguments,
|
|
||||||
fromIndex = funcs.length - 1;
|
|
||||||
|
|
||||||
if (fromIndex < 0) {
|
|
||||||
return function() { return arguments[0]; };
|
|
||||||
}
|
|
||||||
if (!arrayEvery(funcs, baseIsFunction)) {
|
|
||||||
throw new TypeError(FUNC_ERROR_TEXT);
|
|
||||||
}
|
|
||||||
return function() {
|
|
||||||
var index = fromIndex,
|
|
||||||
result = funcs[index].apply(this, arguments);
|
|
||||||
|
|
||||||
while (index--) {
|
|
||||||
result = funcs[index].call(this, result);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a function that memoizes the result of `func`. If `resolver` is
|
* Creates a function that memoizes the result of `func`. If `resolver` is
|
||||||
|
|||||||
Reference in New Issue
Block a user