Skip to content

Commit

Permalink
merged _.match
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Feb 10, 2014
2 parents 6112bc3 + 131aeaf commit d07c409
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
8 changes: 0 additions & 8 deletions test/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,8 @@
result = _.where(list, {b: 2});
equal(result.length, 2);
equal(result[0].a, 1);

result = _.where(list, {a: 1}, true);
equal(result.b, 2, 'Only get the first object matched.')
result = _.where(list, {a: 1}, false);
equal(result.length, 3);

result = _.where(list, {});
equal(result.length, list.length);
result = _.where(list, {}, true);
equal(result, list[0]);
});

test('findWhere', function() {
Expand Down
28 changes: 18 additions & 10 deletions test/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,15 +578,23 @@
ok(returned == 6 && intercepted == 6, 'can use tapped objects in a chain');
});

test('has', function () {
var obj = {foo: 'bar', func: function () {} };
ok (_.has(obj, 'foo'), 'has() checks that the object has a property.');
ok (_.has(obj, 'baz') == false, "has() returns false if the object doesn't have the property.");
ok (_.has(obj, 'func'), 'has() works for functions too.');
obj.hasOwnProperty = null;
ok (_.has(obj, 'foo'), 'has() works even when the hasOwnProperty method is deleted.');
var child = {};
child.prototype = obj;
ok (_.has(child, 'foo') == false, 'has() does not check the prototype chain for a property.')
test("has", function () {
var obj = {foo: "bar", func: function () {} };
ok(_.has(obj, "foo"), "has() checks that the object has a property.");
ok(_.has(obj, "baz") == false, "has() returns false if the object doesn't have the property.");
ok(_.has(obj, "func"), "has() works for functions too.");
obj.hasOwnProperty = null;
ok(_.has(obj, "foo"), "has() works even when the hasOwnProperty method is deleted.");
var child = {};
child.prototype = obj;
ok(_.has(child, "foo") == false, "has() does not check the prototype chain for a property.")
});

test("match", function() {
var moe = {name: 'Moe Howard', hair: true},
curly = {name: 'Curly Howard', hair: false},
stooges = [moe, curly];
ok(_.find(stooges, _.match({hair: false})) === curly, "returns a predicate that can be used by finding functions.")
ok(_.find(stooges, _.match(moe)) === moe, "can be used to locate an object exists in a collection.")
})
})();
23 changes: 15 additions & 8 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,14 @@

// Convenience version of a common use case of `filter`: selecting only objects
// containing specific `key:value` pairs.
_.where = function(obj, attrs, first) {
return _[first ? 'find' : 'filter'](obj, function(value) {
for (var key in attrs) {
if (attrs[key] !== value[key]) return false;
}
return true;
});
_.where = function(obj, attrs) {
return _.filter(obj, _.match(attrs));
};

// Convenience version of a common use case of `find`: getting the first object
// containing specific `key:value` pairs.
_.findWhere = function(obj, attrs) {
return _.where(obj, attrs, true);
return _.find(obj, _.match(attrs));
};

// Return the maximum element or (element-based computation).
Expand Down Expand Up @@ -1106,6 +1101,18 @@
};
};

// Returns a predicate for checking whether an object has a given set of `key:value` pairs.
_.match = function(attrs) {
return function(obj) {
if (obj === attrs) return true; //avoid comparing an object to itself.
for (var key in attrs) {
if (attrs[key] !== obj[key])
return false;
}
return true;
}
};

// Run a function **n** times.
_.times = function(n, iterator, context) {
var accum = Array(Math.max(0, n));
Expand Down

0 comments on commit d07c409

Please sign in to comment.