From a06207b81416f907c79bf0d0ade5d926e8a86bcb Mon Sep 17 00:00:00 2001 From: Filip Zawada Date: Fri, 14 Nov 2014 02:28:53 +0100 Subject: [PATCH] Add lazy _.pluck and _.where. [closes #776] --- lodash.js | 10 ++++++++++ test/test.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lodash.js b/lodash.js index ff009c477..f010fc5e9 100644 --- a/lodash.js +++ b/lodash.js @@ -10190,6 +10190,16 @@ }; }); + // add `LazyWrapper` methods for `_.pluck` and `_.where` + arrayEach(['pluck', 'where'], function (methodName, index) { + var operationName = index ? 'filter' : 'map', + getCallback = index ? matches : property; + + LazyWrapper.prototype[methodName] = function (arg) { + return this[operationName](getCallback(arg)); + }; + }); + LazyWrapper.prototype.dropWhile = function(iteratee, thisArg) { iteratee = getCallback(iteratee, thisArg, 3); diff --git a/test/test.js b/test/test.js index 07c4cc0c4..9445462b2 100644 --- a/test/test.js +++ b/test/test.js @@ -9262,6 +9262,19 @@ deepEqual(_.pluck(objects, 'a'), [1, undefined, undefined, 4]); }); + test('should work in a lazy chain sequence', 1, function () { + if (!isNpm) { + var array = [{prop: 1}, null, {prop: 3}, {prop: 4}]; + + var wrapped = _(array).filter(Boolean).pluck("prop"); + + deepEqual(wrapped.value(), [1, 3, 4]); + } + else { + skipTest(); + } + }); + test('should coerce `key` to a string', 1, function() { function fn() {} fn.toString = _.constant('fn'); @@ -12473,6 +12486,23 @@ deepEqual(actual, expected); }); + + test('should work in a lazy chain sequence', 1, function () { + if (!isNpm) { + var array = [ + {'a': 1}, + {'a': 3}, + {'a': 1, 'b': 2} + ]; + + var wrapped = _(array).where({'a': 1}); + + deepEqual(wrapped.value(), [array[0], array[2]]); + } + else { + skipTest(); + } + }); }()); /*--------------------------------------------------------------------------*/