Skip to content

Instantly share code, notes, and snippets.

@frontsideair
Created May 1, 2016 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frontsideair/957627f793741a85239e140c18f27b2f to your computer and use it in GitHub Desktop.
Save frontsideair/957627f793741a85239e140c18f27b2f to your computer and use it in GitHub Desktop.
const Case = cases => (
pred => (
(typeof cases[pred] !== 'undefined') ? cases[pred]() : cases['_']()
)
)
const count = Case({
0: () => 'zero',
1: () => 'one',
_: () => 'many'
})
count(0) // 'zero'
count(1) // 'one'
count(5) // 'many'
const cond = Case({
true: () => 'yes!',
false: () => 'no :('
})
cond(true) // 'yes!'
cond(false) // 'no :('
Case({
1: () => 'a single item',
_: () => 'many items'
})(2) // 'many items'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment