From 7bd986337a42acd6c73d59d04272f9c400e43c66 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 24 Nov 2015 16:49:59 -0800 Subject: [PATCH] Add lazy `invokeMap`. --- lodash.js | 9 +++++++++ test/test.js | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lodash.js b/lodash.js index 16598b7ff..cf7581a31 100644 --- a/lodash.js +++ b/lodash.js @@ -14051,6 +14051,15 @@ return this.reverse().find(predicate); }; + LazyWrapper.prototype.invokeMap = rest(function(path, args) { + if (typeof path == 'function') { + return new LazyWrapper(this); + } + return this.map(function(value) { + return baseInvokePath(value, path, args); + }); + }); + LazyWrapper.prototype.reject = function(predicate) { predicate = getIteratee(predicate, 3); return this.filter(function(value) { diff --git a/test/test.js b/test/test.js index 031610567..591d35654 100644 --- a/test/test.js +++ b/test/test.js @@ -7148,6 +7148,27 @@ assert.deepEqual(_.invokeMap([object], path), [1]); }); }); + + QUnit.test('should support shortcut fusion', function(assert) { + assert.expect(2); + + if (!isNpm) { + var count = 0, + method = function() { count++; return this.index; }; + + var array = lodashStable.times(LARGE_ARRAY_SIZE, function(index) { + return { 'index': index, 'method': method }; + }); + + var actual = _(array).invokeMap('method').take(1).value(); + + assert.strictEqual(count, 1); + assert.deepEqual(actual, [0]); + } + else { + skipTest(assert, 2); + } + }); }()); /*--------------------------------------------------------------------------*/