Skip to content

Instantly share code, notes, and snippets.

@dinocarl
Last active July 17, 2020 21:14
Show Gist options
  • Save dinocarl/228052aff245fe082592c82fad859b01 to your computer and use it in GitHub Desktop.
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.
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