Skip to content
This repository has been archived by the owner on Mar 11, 2019. It is now read-only.

Commit

Permalink
Add Stream#parseForm()
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Mar 15, 2012
1 parent 6cc20b9 commit 2219bfe
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/adapters/node/lang.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
if (typeof define !== 'function') { var define = (require('amdefine'))(module); }

define(function(require, exports){
define([
"exports",
"querystring"
], function(exports, querystring){
"use strict";

var slice = [].slice;
Expand Down Expand Up @@ -59,4 +62,8 @@ define(function(require, exports){
exports.parseJSON = function(str){
return JSON.parse(str);
};

exports.parseForm = function(str){
return querystring.parse(str);
};
});
11 changes: 11 additions & 0 deletions stream/Stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,17 @@ define([
**/
parseJSON: function(){
return later(this.join(""), lang.parseJSON);
},

/**
* stream.Stream#parseForm() -> promise.Promise
*
* Joins the stream into a string and then parses it as if it were
* form-urlencoded. Always returns a promise so parse errors can be handled
* more gracefully.
**/
parseForm: function(){
return later(this.join(""), lang.parseForm);
}
});

Expand Down
22 changes: 22 additions & 0 deletions test/stream-utils/makeStreamTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,28 @@ define([
}
},

"parseForm": {
beforeEach: function(){
values.splice(0, 3, 'foo=', 'bar&ba', 'z=thud');
},

"returns promise": function(){
finish();
var promise = instance.parseForm();
assert(isPromise(promise));
refute(promise.isFulfilled());
return promise;
},

"parses": function(){
finish();
return instance.parseForm().then(function(json){
assert.same(json.foo, "bar");
assert.same(json.baz, "thud");
});
}
},

"isRepeatable": {
"returns expected value": function(){
assert.same(instance.isRepeatable(), repeatable);
Expand Down

0 comments on commit 2219bfe

Please sign in to comment.