diff --git a/lib/form2json.js b/lib/form2json.js index 24485e1..4515888 100644 --- a/lib/form2json.js +++ b/lib/form2json.js @@ -1,13 +1,3 @@ -exports.decode = function(data) { - return data.split('&').map(splitPair).reduce(fillIn(), {}); -}; - -/** - * A safe and fast alternative to decodeURIComponent - */ -exports.unescape = process.binding("http_parser").urlDecode; - - /** * Returns the specified property of the given object. If the object has no such property, * the property is set to the given value. @@ -82,3 +72,29 @@ function splitPair(pair) { value: exports.unescape(s.join('=')) || '' }; } + +/** + * Decodes the given x-www-form-encoded String. Refer to README.md for details. + */ +exports.decode = function(data) { + return data.split('&').map(splitPair).reduce(fillIn(), {}); +}; + +/** + * Transforms a flat object into a hierarchy using the same rules as decode(). + */ +exports.transform = function(obj) { + var pairs = []; + for (var name in obj) { + if (obj.hasOwnProperty(name)) { + pairs.push({ name: name, value: obj[name] }); + } + } + return pairs.reduce(fillIn(), {}); +}; + +var rplus = /\+/g; + +exports.unescape = function(s) { + return process.binding("http_parser").urlDecode(s).replace(rplus, ' '); +};