Skip to content

Commit

Permalink
Added transform() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Gnass committed Nov 2, 2010
1 parent e226d0b commit d9a1298
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions 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.
Expand Down Expand Up @@ -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, ' ');
};

0 comments on commit d9a1298

Please sign in to comment.