Add lazy _.pluck and _.where. [closes #776]

This commit is contained in:
Filip Zawada
2014-11-14 02:28:53 +01:00
committed by John-David Dalton
parent 3b3ef411a4
commit a06207b814
2 changed files with 40 additions and 0 deletions

View File

@@ -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);

View File

@@ -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();
}
});
}());
/*--------------------------------------------------------------------------*/