mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 02:17:48 +00:00
Add lazy _.pluck and _.where. [closes #776]
This commit is contained in:
committed by
John-David Dalton
parent
3b3ef411a4
commit
a06207b814
10
lodash.js
10
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) {
|
LazyWrapper.prototype.dropWhile = function(iteratee, thisArg) {
|
||||||
iteratee = getCallback(iteratee, thisArg, 3);
|
iteratee = getCallback(iteratee, thisArg, 3);
|
||||||
|
|
||||||
|
|||||||
30
test/test.js
30
test/test.js
@@ -9262,6 +9262,19 @@
|
|||||||
deepEqual(_.pluck(objects, 'a'), [1, undefined, undefined, 4]);
|
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() {
|
test('should coerce `key` to a string', 1, function() {
|
||||||
function fn() {}
|
function fn() {}
|
||||||
fn.toString = _.constant('fn');
|
fn.toString = _.constant('fn');
|
||||||
@@ -12473,6 +12486,23 @@
|
|||||||
|
|
||||||
deepEqual(actual, expected);
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user