Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement FormData.get and FormData.getAll #388

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 53 additions & 0 deletions lib/form_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,33 @@ module.exports = FormData;
// make it a Stream
util.inherits(FormData, CombinedStream);

/**
* @param {string} name the entry name
* @param {string} value the entry value
* @returns {Object}
*/
function createEntry(name, value/*, filename*/) {
// TODO: handle Blob and File
// Related to https://xhr.spec.whatwg.org/#concept-formdata-entry

// Case 1:
// If value is a Blob object and not a File object, then set value to a new
// File object, representing the same bytes, whose name attribute value is
// "blob".

// Case 2:
// If value is (now) a File object and filename is given, then set value to a
// new File object, representing the same bytes, whose name attribute value
// is filename.

var entry = {
name: name,
value: value,
};

return entry;
}

/**
* Create readable "multipart/form-data" streams.
* Can be used to submit forms
Expand All @@ -31,6 +58,7 @@ function FormData(options) {
this._overheadLength = 0;
this._valueLength = 0;
this._valuesToMeasure = [];
this._entryList = [];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused with this.

You use an array as the data structure but then you use it as an object

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad !
It works because Javascript Array are objects, and thus it become attribute of an object, I will change this to a map ;)


CombinedStream.call(this);

Expand Down Expand Up @@ -76,6 +104,31 @@ FormData.prototype.append = function(field, value, options) {

// pass along options.knownLength
this._trackLength(header, value, options);

this._entryList.push(createEntry(field, value));
};

FormData.prototype.get = function(field) {
var foundItem = this._entryList.find(function(item) {
return item.name === field;
});

return foundItem ? foundItem.value : null;
};

FormData.prototype.getAll = function(field) {
return this._entryList
.reduce(
function(acc, entry) {
if (entry.name === field) {
acc.push(entry.value);
}

return acc;
},
[]
)
;
};

FormData.prototype._trackLength = function(header, value, options) {
Expand Down
33 changes: 33 additions & 0 deletions test/integration/test-form-get-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var common = require('../common');
var assert = common.assert;
var FormData = require(common.dir.lib + '/form_data');

(function testEmptyForm() {
var form = new FormData();

// Make sure our response is async
assert.strictEqual(form.get('foo'), null);
assert.deepEqual(form.getAll('foo'), []);
})();

(function testWithOneValue() {
var form = new FormData();

form.append('foo', 'bar');

// Make sure our response is async
assert.strictEqual(form.get('foo'), 'bar');
assert.deepEqual(form.getAll('foo'), ['bar']);
})();

(function testWithMultipleValue() {
var form = new FormData();

form.append('foo', 'bar');
form.append('foo', 'baz');
form.append('foo', 'bad');

// Make sure our response is async
assert.strictEqual(form.get('foo'), 'bar');
assert.deepEqual(form.getAll('foo'), ['bar', 'baz', 'bad']);
})();