Skip to content

Instantly share code, notes, and snippets.

@wuct
Last active March 7, 2016 16:05
Show Gist options
  • Save wuct/19e85b98cc7fe12a7b0a to your computer and use it in GitHub Desktop.
Save wuct/19e85b98cc7fe12a7b0a to your computer and use it in GitHub Desktop.
A functional implementation of switch
// simple example
const isFruitOrVegetable = switch(
case('apple', 'isFruit'),
case('orange', 'isFruit'),
case('eggplant', 'isVegetable'),
default('isFruit')
);
isFruitOrVegetable('apple') // isFruit
// We can also pass regular expressions or functions to case.
// For example:
case(
/apple/,
() => 'isFruit'
)
case(
str => str.includes('eggplant'),
() => 'isVegetable'
)
@jackypan1989
Copy link

nice (thumb

@wuct
Copy link
Author

wuct commented Mar 7, 2016

I have opened a repo for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment