Skip to content

Instantly share code, notes, and snippets.

@aoberoi
Last active May 4, 2018 01:00
Show Gist options
  • Save aoberoi/01fbdab36ba6c2742081aec7216841ea to your computer and use it in GitHub Desktop.
Save aoberoi/01fbdab36ba6c2742081aec7216841ea to your computer and use it in GitHub Desktop.
// define dispatch table OR import it from somewhere else
// const commands = require('./commands/live/somewhere');
const commands = {
first: (foo, bar) => { /* ... */ },
second: (baz) => { /* ... */ },
help: (commandName) => { /* ... */ }
};
function usingDispatch(commandName, ...args) {
const command = commands[commandName] || commands.help;
command(args);
doTheRest();
}
function doFirst(foo, bar) { /* ... */ }
function doSecond(baz) { /* ... */ }
function doHelp(commandName) { /* ... */ }
function usingSwitch(commandName, ...args) {
switch (commandName) {
case 'first':
doFirst(args);
break;
case 'second':
doSecond(args);
break;
default:
doHelp(args[0]);
break;
}
doTheRest(); // ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment