Skip to content

Instantly share code, notes, and snippets.

@Ankirama
Last active November 23, 2016 10:25
Show Gist options
  • Save Ankirama/949391b7d583949f4533d6df94a4c8fd to your computer and use it in GitHub Desktop.
Save Ankirama/949391b7d583949f4533d6df94a4c8fd to your computer and use it in GitHub Desktop.
function command1(params) {
if (params.length > 0) {
console.log('command1 --> ', params);
}
return 0;
}
function command2(params) {
if (params.length > 0) {
console.log('command2 --> ', params);
}
return 0;
}
function command3(params) {
if (params.length > 0) {
console.log('command3 --> ', params);
}
return 0;
}
let dictionnary = {
"command1": command1,
"command2": command2,
"command3": command3
};
function useCommand(cmd, params) {
if (dictionnary.hasOwnProperty(cmd)) {
return dictionnary[cmd](params);
} else {
console.log("Command not found, you must specify a valid one...");
return 1;
}
}
// Tests
useCommand('toto', []); // message + retourne 1
useCommand('command1', []); // rien + retourne 0
useCommand('command2', [1, 2, 3]); // affiche [1, 2, 3] + retourne 0
useCommand('command4', [1, 2, 3]); // message + retourne 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment