Created
February 5, 2013 11:18
-
-
Save yeikos/4713825 to your computer and use it in GitHub Desktop.
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
str2argv( 'name(password), long(8, 16), error(Password incorrect)' ); | |
// { "name": ["password"], "long": [8, 16], "error": ["Password incorrect"] } | |
str2argv( 'one(hello), two(8.698), three( false , "9.6" , 5,), one(world)' ); | |
// { "one": ["world"], "two": [8.698], "three": [false, "9.6", 5, null] } | |
str2argv( 'numbers(5 , 8)' )['numbers']; // [5, 8] | |
str2argv( 'nullnumber(,6)' )['nullnumber']; // [null, 6] | |
str2argv( 'nullnumber( ,6)' )['nullnumber']; // [null, 6] | |
str2argv( 'null( )' )['null']; // [null] | |
str2argv( 'emptyarray()' )['emptyarray']; // [] | |
str2argv( 'string(hello world)' )['string']; // ["hello world"] | |
str2argv( 'literalnumber("8")' )['literalnumber']; // ["8"] | |
str2argv( 'number(8)' )['number']; // [8] | |
str2argv( 'boolean(true, false)' )['boolean']; // { boolean: [true, false] } | |
str2argv( 'booleanliteral("true", \'false\')' )['booleanliteral']; // ["true", "false"] |
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
function str2argv(i) { | |
var o = {}, e = /([_\-\w]+)\(([^\)]*)\)/gi, m, x, y, z, w; | |
while (m = e.exec(i)) { | |
if (x = m[2]) { | |
for(y in (x = x.split(/\s*,\s*/))) { | |
x[y] = /^\s*$/.test(z = x[y].replace(/^\s+/, '').replace(/\s+$/, '')) ? null : // null | |
(z == 'true') ? true : (z == 'false') ? false : // boolean | |
((w = /^'(.*)'$/.exec(z)) || (w = /^"(.*)"$/.exec(z))) ? w[1] : // literal string | |
(!isNaN(z)) ? parseFloat(z) : // number | |
z; // string | |
} | |
} else { | |
x = []; | |
} | |
o[m[1]] = x; | |
} | |
return o; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment