Created
December 22, 2011 23:10
-
-
Save hannahherbig/1512257 to your computer and use it in GitHub Desktop.
irc parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = parse = (str) -> | |
origin = null | |
command = null | |
args = [] | |
# pull out the origin if there is one | |
if str[0] is ':' | |
i = str.indexOf ' ' | |
origin = str.slice 1, i | |
str = str.slice i + 1 | |
# pull out the command | |
i = str.indexOf ' ' | |
command = str.slice 0, i | |
str = str.slice i + 1 | |
# get the args | |
i = str.indexOf ' :' | |
unless i is -1 | |
args = (str.slice 0, i).split /\s+/ | |
args.push str.slice i + 2 | |
else | |
args = str.split /\s+/ | |
args.origin = origin | |
args.command = command | |
args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// same as above just compiled like `coffee -bcp parse.coffee` | |
var parse; | |
module.exports = parse = function(str) { | |
var args, command, i, origin; | |
origin = null; | |
command = null; | |
args = []; | |
if (str[0] === ':') { | |
i = str.indexOf(' '); | |
origin = str.slice(1, i); | |
str = str.slice(i + 1); | |
} | |
i = str.indexOf(' '); | |
command = str.slice(0, i); | |
str = str.slice(i + 1); | |
i = str.indexOf(' :'); | |
if (i !== -1) { | |
args = (str.slice(0, i)).split(/\s+/); | |
args.push(str.slice(i + 2)); | |
} else { | |
args = str.split(/\s+/); | |
} | |
args.origin = origin; | |
args.command = command; | |
return args; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment