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

feat: add support for get, getAll, has and set #461

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions lib/form_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ function FormData(options) {
this._overheadLength = 0;
this._valueLength = 0;
this._valuesToMeasure = [];
this._entryList = [];

CombinedStream.call(this);

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

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

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

FormData.prototype._trackLength = function(header, value, options) {
Expand Down Expand Up @@ -412,6 +415,44 @@ FormData.prototype.getLength = function(cb) {
});
};

FormData.prototype._create_entry = function(field, value) {
return { name: field, value: value };
};

FormData.prototype.set = function(field, value) {
const entry = this._create_entry(field, value);

this._entryList = this._entryList.filter(function(entry) {
return entry.name !== field;
});

this._entryList.push(entry);
};

FormData.prototype.get = function(field) {
const foundEntry = this._entryList.find(function(entry) {
return entry.name === field;
});

return foundEntry === undefined ? null : foundEntry;
};

FormData.prototype.getAll = function(field) {
return this._entryList
.filter(function(entry) {
return entry.name === field;
})
.map(function(entry) {
return entry.value;
});
};

FormData.prototype.has = function(field) {
return this._entryList.some(function(entry) {
return entry.name === field;
});
};

FormData.prototype.submit = function(params, cb) {
var request
, options
Expand Down