Permalink
Browse files

Added transform() method

  • Loading branch information...
1 parent e226d0b commit d9a1298a5851e0d9f87b61b0c9574646d882f6ba Felix Gnass committed Nov 2, 2010
Showing with 26 additions and 10 deletions.
  1. +26 −10 lib/form2json.js
View
@@ -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, ' ');
+};

0 comments on commit d9a1298

Please sign in to comment.