InputParser.js 610 B

123456789101112131415161718192021222324252627282930
  1. if(MIDAS == undefined) {
  2. var MIDAS = {};
  3. }
  4. MIDAS.InputParser = function() {
  5. var self = this;
  6. this.parse = function(string, commands) {
  7. var words = string.split(" ");
  8. var command = words[0];
  9. if (!isValidCommand(command, commands)) {
  10. return {"error": true, "errorMessage":string};
  11. }
  12. var data = words.splice(1);
  13. var result = {
  14. "command":command
  15. ,"data":data
  16. };
  17. return result;
  18. };
  19. function isValidCommand(command, commands) {
  20. for(var i = 0; i < commands.length; i++) {
  21. if(command == commands[i]) {
  22. return true;
  23. }
  24. }
  25. return false;
  26. };
  27. };