Skip to content

Instantly share code, notes, and snippets.

@pvpshoot
Created March 20, 2017 07:45
Show Gist options
  • Save pvpshoot/6526bd3279e837c0cf43a4c42b4fd067 to your computer and use it in GitHub Desktop.
Save pvpshoot/6526bd3279e837c0cf43a4c42b4fd067 to your computer and use it in GitHub Desktop.
fp-switch, pattern matching
const patternMatch = (o, cond, or) => {
if (typeof o[cond] !== 'function') {
return or || null;
}
return o[cond]();
}
const TEST = {
lol(){ return: 'lol' },
kek(){ return: 'kek' },
cheburek(){ return: 'cheburek' },
}
const DEFAULT = 'something default';
console.log(patternMatch(TEST, 'kek', DEFAULT));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment