Created
September 14, 2012 03:29
-
-
Save tobsn/3719623 to your computer and use it in GitHub Desktop.
node.js parse array form from query string into array like php does
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
url.query = function( url ) { | |
var parsed = this.parse( url, true, false ), | |
query = Object.keys( parsed.query ), | |
result = {}; | |
if( query.length > 0 ) { | |
query.forEach(function(key){ | |
if( key.match( /([^\[]+)\[([^\]]+)\]/g ) ) { | |
key.replace( /([^\[]+)\[([^\]]+)\]/g, function( $0, $1, $2 ) { | |
result[$1] = result[$1] || {}; | |
result[$1][$2] = parsed.query[key]; | |
}); | |
} | |
else { | |
result[key] = parsed.query[key]; | |
} | |
}); | |
} | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment