Skip to content

Commit

Permalink
use array instead of map
Browse files Browse the repository at this point in the history
  • Loading branch information
jdeniau committed Oct 17, 2018
1 parent 3d52f6c commit fa9dbea
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 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,7 +58,7 @@ function FormData(options) {
this._overheadLength = 0;
this._valueLength = 0;
this._valuesToMeasure = [];
this._entryList = {};
this._entryList = [];

CombinedStream.call(this);

Expand Down Expand Up @@ -78,18 +105,30 @@ FormData.prototype.append = function(field, value, options) {
// pass along options.knownLength
this._trackLength(header, value, options);

if (!this._entryList[field]) {
this._entryList[field] = [];
}
this._entryList[field].push(value);
this._entryList.push(createEntry(field, value));
};

FormData.prototype.get = function(field) {
return this._entryList[field] ? this._entryList[field][0] : null;
var foundItem = this._entryList.find(function(item) {
return item.name === field;
});

if (!foundItem) {
return null;
}

return foundItem.value;
};

FormData.prototype.getAll = function(field) {
return this._entryList[field] || [];
return this._entryList
.filter(function(item) {
return item.name === field;
})
.map(function(item) {
return item.value;
})
;
};

FormData.prototype._trackLength = function(header, value, options) {
Expand Down

0 comments on commit fa9dbea

Please sign in to comment.