Skip to content

Instantly share code, notes, and snippets.

@carlosvillu
Created October 1, 2011 00:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosvillu/1255380 to your computer and use it in GitHub Desktop.
Save carlosvillu/1255380 to your computer and use it in GitHub Desktop.
Uso de la función call
jQuery(function(){
jQuery('input[type=submit]').click(function(evt){
evt.preventDefault();
var string = jQuery('input[type=text]').val();
var simpleParser = {},
advancedParser = {},
wonderfulParser = {};
PARSER.call(simpleParser);
PARSER.call(advancedParser, {g: function(argv){}});
PARSER.call(wonderfulParser, jQuery.extend(true, {}, advancedParser.rules, {v: function(argv){}}));
simpleParser.parser(string);
advancedParser.parser(string);
wonderfulParser.parser(string);
});
});
var PARSER = function(newRules){
var _self = this;
var defaultRules = {
l: function(argv){},
p: function(argv){},
d: function(argv){}
};
var rules = jQuery.extend(true, {}, defaultRules, newRules);
var REGEX = /(\w\s{1}[\d|\w|/|,]*)/;
this.parser = function(string){
if(REGEX.test(string)) {
var matches = string.match(REGEX);
for(var match in matches) {
_self.execute(matches[match]);
}
} else {
throw 'No found string to parser';
}
};
this.execute = function(command){
console.info(typeof command);
var command = command.split(' ');
if(typeof rules[command[0]] === 'function'){
rules[command[0]].call(command[1]||[]);
}else{
throw 'No rules defined for ' + command[0] + ' whith argv ' + command[1];
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment