Get Programming with JavaScript - Listing 18.08
Listing 18.08 - Converting a command string into a command object
function parseCommand (commandString) {
var commandWords = commandString.split(" ");
var command = {
type: commandWords.shift()
};
if (command.type === "go" || command.type === "use") {
command.direction = commandWords.pop();
}
command.item = commandWords.join(" ");
return command;
}