Last active
July 17, 2020 21:14
-
-
Save dinocarl/228052aff245fe082592c82fad859b01 to your computer and use it in GitHub Desktop.
Uses a fallback value, then an array of strings that serve as an ordered list of preferred props from most to least to return from an object. The first valid prop gets returned.
This file contains hidden or 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
const validVal = compose( | |
not, | |
anyPass([isEmpty, isNil]) | |
); | |
const validProp = curry((propName, obj) => validVal(prop(propName, obj))); | |
const propsOr = curry((fallback, propsList, obj) => compose( | |
cond, | |
append([T, always(fallback)]), | |
map((str) => [validProp(str), prop(str)]), | |
)(propsList)(obj)); | |
const data = { | |
mostPreferred: 'X', | |
preferred2: 'Y', | |
}; | |
propsOr(':(', ['mostPreferred', 'preferred2', 'leastPreferred'], data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment