From d93aa183f3fdbefe73c8b06ae3ecc6bae6416c56 Mon Sep 17 00:00:00 2001 From: jdalton Date: Mon, 29 Jun 2015 21:07:46 -0700 Subject: [PATCH] Ensure `_.flow` and `_.flowRight` works with `_.first`. [closes #1308] --- lodash.src.js | 14 +++++++------- test/test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index 6aa8d435d..85641617d 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -1205,7 +1205,7 @@ takeCount = nativeMin(length, this.__takeCount__); if (!isArr || arrLength < LARGE_ARRAY_SIZE || (arrLength == length && takeCount == length)) { - return baseWrapperValue(isRight ? array.reverse() : array, this.__actions__); + return baseWrapperValue((isRight && isArr) ? array.reverse() : array, this.__actions__); } var result = []; @@ -3433,7 +3433,7 @@ throw new TypeError(FUNC_ERROR_TEXT); } if (!wrapper && LodashWrapper.prototype.thru && getFuncName(func) == 'wrapper') { - wrapper = new LodashWrapper([]); + wrapper = new LodashWrapper([], true); } } index = wrapper ? -1 : length; @@ -12428,7 +12428,9 @@ isLazy = useLazy = false; } var interceptor = function(value) { - return lodashFunc.apply(undefined, arrayPush([value], args)); + return (retUnwrapped && chainAll) + ? lodashFunc(value, 1)[0] + : lodashFunc.apply(undefined, arrayPush([value], args)); }; var action = { 'func': thru, 'args': [interceptor], 'thisArg': undefined }, @@ -12442,12 +12444,10 @@ } return lodashFunc.call(undefined, this.value())[0]; } - if (useLazy) { + if (!retUnwrapped && useLazy) { value = onlyLazy ? value : new LazyWrapper(this); var result = func.apply(value, args); - if (!retUnwrapped) { - result.__actions__.push(action); - } + result.__actions__.push(action); return new LodashWrapper(result, chainAll); } return this.thru(interceptor); diff --git a/test/test.js b/test/test.js index 1a1685cf3..327c85809 100644 --- a/test/test.js +++ b/test/test.js @@ -2248,6 +2248,16 @@ notStrictEqual(combined, _.identity); }); + test('`_.' + methodName + '` should work with a curried function and `_.first`', 1, function() { + var curried = _.curry(_.identity); + + var combined = isFlow + ? func(_.first, curried) + : func(curried, _.first); + + strictEqual(combined([1]), 1); + }); + test('`_.' + methodName + '` should support shortcut fusion', 12, function() { var filterCount, mapCount; @@ -4796,6 +4806,16 @@ } }); + test('should not execute immediately when explicitly chaining', 1, function() { + if (!isNpm) { + var wrapped = _(array).chain().first(); + strictEqual(wrapped.__wrapped__, array); + } + else { + skipTest(); + } + }); + test('should work in a lazy chain sequence', 1, function() { if (!isNpm) { var array = _.range(1, LARGE_ARRAY_SIZE + 1); @@ -9261,6 +9281,16 @@ } }); + test('should not execute immediately when explicitly chaining', 1, function() { + if (!isNpm) { + var wrapped = _(array).chain().last(); + strictEqual(wrapped.__wrapped__, array); + } + else { + skipTest(); + } + }); + test('should work in a lazy chain sequence', 1, function() { if (!isNpm) { var array = _.range(1, LARGE_ARRAY_SIZE + 1),