-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Abstract iterator binding to createIterator and callbacks to createCallback #1582
Changes from all commits
bd97f6c
4b1096c
e52fd55
42d1abb
787290c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,10 @@ | |
|
||
var ifnull = _.map(null, function(){}); | ||
ok(_.isArray(ifnull) && ifnull.length === 0, 'handles a null properly'); | ||
|
||
// Passing a property name like _.pluck. | ||
var people = [{name : 'moe', age : 30}, {name : 'curly', age : 50}]; | ||
deepEqual(_.map(people, 'name'), ['moe', 'curly'], 'predicate string map to object properties'); | ||
}); | ||
|
||
test('reduce', function() { | ||
|
@@ -167,6 +171,13 @@ | |
var array = [1, 2, 3, 4]; | ||
strictEqual(_.find(array, function(n) { return n > 2; }), 3, 'should return first found `value`'); | ||
strictEqual(_.find(array, function() { return false; }), void 0, 'should return `undefined` if `value` is not found'); | ||
|
||
// Matching an object like _.findWhere. | ||
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}, {a: 2, b: 4}]; | ||
deepEqual(_.find(list, {a: 1}), {a: 1, b: 2}, 'can be used as findWhere'); | ||
deepEqual(_.find(list, {b: 4}), {a: 1, b: 4}); | ||
ok(!_.find(list, {c: 1}), 'undefined when not found'); | ||
ok(!_.find([], {c: 1}), 'undefined when searching empty list'); | ||
}); | ||
|
||
test('detect', function() { | ||
|
@@ -177,9 +188,22 @@ | |
test('select', function() { | ||
var evens = _.select([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); | ||
deepEqual(evens, [2, 4, 6], 'selected each even number'); | ||
}); | ||
|
||
test('filter', function() { | ||
var evenArray = [1, 2, 3, 4, 5, 6]; | ||
var evenObject = {one: 1, two: 2, three: 3}; | ||
var isEven = function(num){ return num % 2 === 0; }; | ||
|
||
deepEqual(_.filter(evenArray, isEven), [2, 4, 6]); | ||
deepEqual(_.filter(evenObject, isEven), [2]); | ||
deepEqual(_.filter([{}, evenObject, []], 'two'), [evenObject], 'predicate string map to object properties'); | ||
|
||
evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; }); | ||
deepEqual(evens, [2, 4, 6], 'aliased as "filter"'); | ||
// Can be used like _.where. | ||
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]; | ||
deepEqual(_.filter(list, {a: 1}), [{a: 1, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]); | ||
deepEqual(_.filter(list, {b: 2}), [{a: 1, b: 2}, {a: 2, b: 2}]); | ||
deepEqual(_.filter(list, {}), list, 'Empty object accepts all items'); | ||
}); | ||
|
||
test('reject', function() { | ||
|
@@ -193,6 +217,15 @@ | |
return num % 2 != 0; | ||
}, context); | ||
deepEqual(evens, [2, 4, 6], 'rejected each odd number'); | ||
|
||
deepEqual(_.reject([odds, {one: 1, two: 2, three: 3}], 'two'), [odds], 'predicate string map to object properties'); | ||
|
||
// Can be used like _.where. | ||
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]; | ||
deepEqual(_.reject(list, {a: 1}), [{a: 2, b: 2}]); | ||
deepEqual(_.reject(list, {b: 2}), [{a: 1, b: 3}, {a: 1, b: 4}]); | ||
deepEqual(_.reject(list, {}), [], 'Returns empty list given empty object'); | ||
deepEqual(_.reject(list, []), [], 'Returns empty list given empty array'); | ||
}); | ||
|
||
test('all', function() { | ||
|
@@ -205,6 +238,14 @@ | |
ok(_.all([0], _.identity) === false, 'cast to boolean - false'); | ||
ok(_.every([true, true, true], _.identity), 'aliased as "every"'); | ||
ok(!_.all([undefined, undefined, undefined], _.identity), 'works with arrays of undefined'); | ||
|
||
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]; | ||
ok(!_.all(list, {a: 1, b: 2}), 'Can be called with object'); | ||
ok(_.all(list, 'a'), 'String mapped to object property'); | ||
|
||
list = [{a: 1, b: 2}, {a: 2, b: 2, c: true}]; | ||
ok(_.all(list, {b: 2}), 'Can be called with object'); | ||
ok(!_.all(list, 'c'), 'String mapped to object property'); | ||
}); | ||
|
||
test('any', function() { | ||
|
@@ -218,6 +259,14 @@ | |
ok(_.any([1], _.identity) === true, 'cast to boolean - true'); | ||
ok(_.any([0], _.identity) === false, 'cast to boolean - false'); | ||
ok(_.some([false, false, true]), 'aliased as "some"'); | ||
|
||
var list = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 1, b: 3}, {a: 1, b: 4}]; | ||
ok(!_.any(list, {a: 5, b: 2}), 'Can be called with object'); | ||
ok(_.any(list, 'a'), 'String mapped to object property'); | ||
|
||
list = [{a: 1, b: 2}, {a: 2, b: 2, c: true}]; | ||
ok(_.any(list, {b: 2}), 'Can be called with object'); | ||
ok(!_.any(list, 'd'), 'String mapped to object property'); | ||
}); | ||
|
||
test('include', function() { | ||
|
@@ -257,8 +306,10 @@ | |
}); | ||
|
||
test('pluck', function() { | ||
var people = [{name : 'moe', age : 30}, {name : 'curly', age : 50}]; | ||
var people = [{name: 'moe', age: 30}, {name: 'curly', age: 50}]; | ||
deepEqual(_.pluck(people, 'name'), ['moe', 'curly'], 'pulls names out of objects'); | ||
//compat: most flexible handling of edge cases | ||
deepEqual(_.pluck([{'[object Object]': 1}], {}), [1]); | ||
}); | ||
|
||
test('where', function() { | ||
|
@@ -271,6 +322,10 @@ | |
equal(result[0].a, 1); | ||
result = _.where(list, {}); | ||
equal(result.length, list.length); | ||
|
||
function test() {} | ||
test.map = _.map; | ||
deepEqual(_.where([_, {a: 1, b: 2}, _], test), [_, _], 'checks properties given function'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this test as functions are just another object as far as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe not, but its a gotcha I had in a previous commit (4512921) which is why I added a test for it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough! :) |
||
}); | ||
|
||
test('findWhere', function() { | ||
|
@@ -280,11 +335,22 @@ | |
result = _.findWhere(list, {b: 4}); | ||
deepEqual(result, {a: 1, b: 4}); | ||
|
||
result = _.findWhere(list, {c: 1}) | ||
result = _.findWhere(list, {c: 1}); | ||
ok(_.isUndefined(result), 'undefined when not found'); | ||
|
||
result = _.findWhere([], {c: 1}); | ||
ok(_.isUndefined(result), 'undefined when searching empty list'); | ||
|
||
function test() {} | ||
test.map = _.map; | ||
equal(_.findWhere([_, {a: 1, b: 2}, _], test), _, 'checks properties given function'); | ||
|
||
function TestClass() { | ||
this.y = 5; | ||
this.x = 'foo'; | ||
} | ||
var expect = {c: 1, x: 'foo', y: 5}; | ||
deepEqual(_.findWhere([{y: 5, b: 6}, expect], new TestClass()), expect, 'uses class instance properties'); | ||
}); | ||
|
||
test('max', function() { | ||
|
@@ -306,6 +372,8 @@ | |
var b = {x: -Infinity}; | ||
var iterator = function(o){ return o.x; }; | ||
equal(_.max([a, b], iterator), a, 'Respects iterator return value of -Infinity'); | ||
|
||
deepEqual(_.max([{'a': 1}, {'a': 0, 'b': 3}, {'a': 4}, {'a': 2}], 'a'), {'a': 4}, 'String keys use property iterator'); | ||
}); | ||
|
||
test('min', function() { | ||
|
@@ -331,6 +399,8 @@ | |
var b = {x: Infinity}; | ||
var iterator = function(o){ return o.x; }; | ||
equal(_.min([a, b], iterator), a, 'Respects iterator return value of Infinity'); | ||
|
||
deepEqual(_.min([{'a': 1}, {'a': 0, 'b': 3}, {'a': 4}, {'a': 2}], 'a'), {'a': 0, 'b': 3}, 'String keys use property iterator'); | ||
}); | ||
|
||
test('sortBy', function() { | ||
|
@@ -368,6 +438,8 @@ | |
|
||
deepEqual(actual, collection, 'sortBy should be stable'); | ||
|
||
deepEqual(_.sortBy(collection, 'x'), collection, 'sortBy accepts property string'); | ||
|
||
var list = ['q', 'w', 'e', 'r', 't', 'y']; | ||
deepEqual(_.sortBy(list), ['e', 'q', 'r', 't', 'w', 'y'], 'uses _.identity if iterator is not specified'); | ||
}); | ||
|
@@ -405,8 +477,8 @@ | |
[1, 3], | ||
[2, 3] | ||
]; | ||
deepEqual(_.groupBy(matrix, 0), {1: [[1, 2], [1, 3]], 2: [[2, 3]]}) | ||
deepEqual(_.groupBy(matrix, 1), {2: [[1, 2]], 3: [[1, 3], [2, 3]]}) | ||
deepEqual(_.groupBy(matrix, 0), {1: [[1, 2], [1, 3]], 2: [[2, 3]]}); | ||
deepEqual(_.groupBy(matrix, 1), {2: [[1, 2]], 3: [[1, 3], [2, 3]]}); | ||
}); | ||
|
||
test('indexBy', function() { | ||
|
@@ -549,6 +621,8 @@ | |
// Context | ||
var predicate = function(x){ return x === this.x }; | ||
deepEqual(_.partition([1, 2, 3], predicate, {x: 2}), [[2], [1, 3]], 'partition takes a context argument'); | ||
|
||
deepEqual(_.partition([{a: 1}, {b: 2}, {a: 1, b: 2}], {a: 1}), [[{a: 1}, {a: 1, b: 2}], [{b: 2}]], 'predicate can be object'); | ||
}); | ||
|
||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_.filter
is equivalent to_.select
. Perhaps we can just combine these?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bunch of the other alias methods have their own tests, personally I think it makes sense to leave it there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would they have their own tests though? Everything that applies to one always applies to the other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats true, want me to remove the tests for detect as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, let's do that in a separate pull. Thanks @megawac!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good, whoever makes the pr should add a test for
_.foldl
and drop includeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For aliases a simple strictEqual check would do:
No need to test functionality of the alias beyond that.