where
takes a spec object and a test object and returns true if the test satisfies the spec.
Any property on the spec that is not a function is interpreted as an equality
relation. For example:
var spec = {x: 2};
where(spec, {w: 10, x: 2, y: 300}); // => true, x === 2
where(spec, {x: 1, y: 'moo', z: true}); // => false, x !== 2
If the spec has a property mapped to a function, then where
evaluates the function, passing in
the test object's value for the property in question, as well as the whole test object. For example:
var spec = {x: function(val, obj) { return val + obj.y > 10; };
where(spec, {x: 2, y: 7}); // => false
where(spec, {x: 3, y: 8}); // => true
where
is well suited to declarativley expressing constraints for other functions, e.g., filter
:
var xs = [{x: 2, y: 1}, {x: 10, y: 2},
{x: 8, y: 3}, {x: 10, y: 4}];
var fxs = filter(where({x: 10}), xs);
// fxs ==> [{x: 10, y: 2}, {x: 10, y: 4}]
R.where = _(function(spec, test) {
return all(function(key) {
var val = spec[key];
return (typeof val === 'function') ? val(test[key], test) : (test[key] === val);
}, keys(spec));
});