Created
February 13, 2014 02:52
-
-
Save buzzdecafe/8968904 to your computer and use it in GitHub Desktop.
where: create a matching predicate from an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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