Skip to content

Instantly share code, notes, and snippets.

@buzzdecafe
Created February 13, 2014 02:52
Show Gist options
  • Save buzzdecafe/8968904 to your computer and use it in GitHub Desktop.
Save buzzdecafe/8968904 to your computer and use it in GitHub Desktop.
where: create a matching predicate from an object
function where(matchThis) {
var keys = Object.keys(matchThis);
return function(testObj) {
var match = ramda.all(function(key) {
return testObj[key] === matchThis[key];
}, keys);
return match ? testObj : false;
};
}
x1y2 = where({x: 1, y: 2});
x1y2({x: 1}) // => false
x1y2({x: 1, y: 2}); // => {x: 1, y: 2} or true?
x1y2({x: 1, y: 2, z: 3}); // => {x: 1, y: 2, z: 3}
function whereWith(rel, matchThis) {
var keys = Object.keys(matchThis);
return function(testObj) {
var match = ramda.all(function(key) {
return rel(testObj[key], matchThis[key]);
}, keys);
return match ? testObj : false;
};
}
xlt1 = whereWith(function(test, match) { return test < match; }, {x: 1});
xlt1({x: 1}); // false
xlt1({x: 0}); // Object {x: 0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment