mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Add support for shortcut fusion to _.flow and _.flowRight.
This commit is contained in:
@@ -3375,25 +3375,39 @@
|
|||||||
*/
|
*/
|
||||||
function createFlow(fromRight) {
|
function createFlow(fromRight) {
|
||||||
return function() {
|
return function() {
|
||||||
var length = arguments.length,
|
var length = arguments.length;
|
||||||
index = length,
|
|
||||||
fromIndex = fromRight ? (length - 1) : 0;
|
|
||||||
|
|
||||||
if (!length) {
|
if (!length) {
|
||||||
return function() { return arguments[0]; };
|
return function() { return arguments[0]; };
|
||||||
}
|
}
|
||||||
var funcs = Array(length);
|
var index = fromRight ? length : -1,
|
||||||
while (index--) {
|
leftIndex = 0,
|
||||||
funcs[index] = arguments[index];
|
funcs = Array(length),
|
||||||
if (typeof funcs[index] != 'function') {
|
wrapper = new LodashWrapper([]);
|
||||||
|
|
||||||
|
while ((fromRight ? index-- : ++index < length)) {
|
||||||
|
var func = arguments[index];
|
||||||
|
if (typeof func != 'function') {
|
||||||
throw new TypeError(FUNC_ERROR_TEXT);
|
throw new TypeError(FUNC_ERROR_TEXT);
|
||||||
}
|
}
|
||||||
|
var data = func.name === 'wrapper' && getData(func);
|
||||||
|
if (data && data !== true && isLaziable(data[0])) {
|
||||||
|
wrapper = wrapper[data[0].name].apply(wrapper, data[3]);
|
||||||
|
} else if (func.length == 1 && isLaziable(func)) {
|
||||||
|
wrapper = wrapper[func.name]();
|
||||||
|
} else {
|
||||||
|
wrapper = wrapper.thru(func);
|
||||||
|
}
|
||||||
|
funcs[leftIndex++] = func;
|
||||||
}
|
}
|
||||||
return function() {
|
return function() {
|
||||||
var index = fromIndex,
|
var args = arguments;
|
||||||
result = funcs[index].apply(this, arguments);
|
if (args.length == 1 && isArray(args[0])) {
|
||||||
|
return wrapper.plant(args[0]).value();
|
||||||
|
}
|
||||||
|
var index = 0,
|
||||||
|
result = funcs[index].apply(this, args);
|
||||||
|
|
||||||
while ((fromRight ? index-- : ++index < length)) {
|
while (++index < length) {
|
||||||
result = funcs[index].call(this, result);
|
result = funcs[index].call(this, result);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
@@ -3558,11 +3572,10 @@
|
|||||||
if (!isCurryBound) {
|
if (!isCurryBound) {
|
||||||
bitmask &= ~(BIND_FLAG | BIND_KEY_FLAG);
|
bitmask &= ~(BIND_FLAG | BIND_KEY_FLAG);
|
||||||
}
|
}
|
||||||
var funcName = support.funcNames ? func.name : '',
|
var newData = [func, bitmask, thisArg, newPartials, newsHolders, newPartialsRight, newHoldersRight, newArgPos, ary, newArity],
|
||||||
newData = [func, bitmask, thisArg, newPartials, newsHolders, newPartialsRight, newHoldersRight, newArgPos, ary, newArity],
|
|
||||||
result = createHybridWrapper.apply(undefined, newData);
|
result = createHybridWrapper.apply(undefined, newData);
|
||||||
|
|
||||||
if (funcName && func === lodash[funcName] && LazyWrapper.prototype[funcName]) {
|
if (isLaziable(func)) {
|
||||||
setData(result, newData);
|
setData(result, newData);
|
||||||
}
|
}
|
||||||
result.placeholder = placeholder;
|
result.placeholder = placeholder;
|
||||||
@@ -4108,6 +4121,11 @@
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isLaziable(func) {
|
||||||
|
var funcName = support.funcNames ? func.name : '';
|
||||||
|
return (funcName && func === lodash[funcName] && funcName in LazyWrapper.prototype) || false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if `value` is a valid array-like length.
|
* Checks if `value` is a valid array-like length.
|
||||||
*
|
*
|
||||||
|
|||||||
28
test/test.js
28
test/test.js
@@ -2232,6 +2232,34 @@
|
|||||||
notStrictEqual(combined, _.identity);
|
notStrictEqual(combined, _.identity);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('`_.' + methodName + '` should support shortcut fusion', 3, function() {
|
||||||
|
if (!isNpm) {
|
||||||
|
var filterCount = 0,
|
||||||
|
mapCount = 0;
|
||||||
|
|
||||||
|
var map = _.curry(_.rearg(_.ary(_.map, 2), 1, 0), 2),
|
||||||
|
filter = _.curry(_.rearg(_.ary(_.filter, 2), 1, 0), 2),
|
||||||
|
take = _.curry(_.rearg(_.ary(_.take, 2), 1, 0), 2);
|
||||||
|
|
||||||
|
var partialMap = map(function(value) { mapCount++; return value * value; }),
|
||||||
|
partialFilter = filter(function(value) { filterCount++; return value % 2 == 0; }),
|
||||||
|
partialTake = take(2);
|
||||||
|
|
||||||
|
var combined = isFlow
|
||||||
|
? func(partialMap, partialFilter, _.compact, partialTake)
|
||||||
|
: func(partialTake, _.compact, partialFilter, partialMap);
|
||||||
|
|
||||||
|
var actual = combined(_.range(100));
|
||||||
|
|
||||||
|
deepEqual(actual, [4, 16]);
|
||||||
|
strictEqual(filterCount, 5, 'filterCount');
|
||||||
|
strictEqual(mapCount, 5, 'mapCount');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipTest(3);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
test('`_.' + methodName + '` should return a wrapped value when chaining', 1, function() {
|
test('`_.' + methodName + '` should return a wrapped value when chaining', 1, function() {
|
||||||
if (!isNpm) {
|
if (!isNpm) {
|
||||||
var wrapped = _(_.noop)[methodName]();
|
var wrapped = _(_.noop)[methodName]();
|
||||||
|
|||||||
Reference in New Issue
Block a user