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});
where(spec, {x: 1, y: 'moo', z: true});
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});
where(spec, {x: 3, y: 8});
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);
R.where = function(spec, test) {
function isFn(key) {return typeof spec[key] === 'function';}
var specKeys = keys(spec);
var fnKeys = filter(isFn, specKeys);
var objKeys = reject(isFn, specKeys);
var process = function(test) {
if (!test) { return false; }
var i = -1, key;
while (++i < fnKeys.length) {
key = fnKeys[i];
if (!spec[key](test[key], test)) {
return false;
}
}
i = -1;
while (++i < objKeys.length) {
key = objKeys[i];