Created
October 20, 2010 07:34
-
-
Save alexbosworth/635964 to your computer and use it in GitHub Desktop.
in progress GET object 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
// SCRIPT IN PROGRESS! BEWARE SUPER HACKY/UGLY CODE BELOW | |
// takes an associative array of args "a[b][c]" = d into a : { b : { c : d } } | |
// use: var getArgs = parseGetArgs(url.parse(request.url, true).query); | |
function parseGetArgs(args) { | |
if (!args) return null; | |
var result = {}; | |
for (var arg in args) { | |
var parts = arg.split(/[\[\]]/); | |
for (var i = 0, part, last = result, ii = parts.length; i < ii; i++) { | |
part = parts[i]; | |
if (!part.length) continue; | |
if (i == ii - 2) { | |
last[part] = args[arg]; | |
continue; | |
} | |
if (!last[part]) // this is a new key | |
last[part] = (i == ii - 1) ? args[arg] : {}; | |
last = last[part]; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment