Skip to content

Commit

Permalink
feat: add getAll function to formdata (#32444)
Browse files Browse the repository at this point in the history
Summary:
The getAll() method of the [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData) interface returns all the values associated with a given key from within a FormData object.

## Changelog

[General] [Added] - Add getAll function to FormData class for getting all parts containing that key. This is also available in web API.

Pull Request resolved: #32444

Test Plan: New test added in FormData-test.js

Reviewed By: lunaleaps

Differential Revision: D31798633

Pulled By: cortinico

fbshipit-source-id: ef29bb54e930532a671adbe707be8d1a64ff0d82
  • Loading branch information
matinzd authored and facebook-github-bot committed Mar 31, 2022
1 parent 8b81dea commit d05a5d1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Libraries/Network/FormData.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ class FormData {
this._parts.push([key, value]);
}

getAll(key: string): Array<FormDataValue> {
return this._parts
.filter(([name]) => name === key)
.map(([, value]) => value);
}

getParts(): Array<FormDataPart> {
return this._parts.map(([name, value]) => {
const contentDisposition = 'form-data; name="' + name + '"';
Expand Down
31 changes: 31 additions & 0 deletions Libraries/Network/__tests__/FormData-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,35 @@ describe('FormData', function () {
};
expect(formData.getParts()[0]).toMatchObject(expectedPart);
});

it('should return values based on the given key', function () {
formData.append('username', 'Chris');
formData.append('username', 'Bob');

expect(formData.getAll('username').length).toBe(2);

expect(formData.getAll('username')).toMatchObject(['Chris', 'Bob']);

formData.append('photo', {
uri: 'arbitrary/path',
type: 'image/jpeg',
name: 'photo3.jpg',
});

formData.append('photo', {
uri: 'arbitrary/path',
type: 'image/jpeg',
name: 'photo2.jpg',
});

const expectedPart = {
uri: 'arbitrary/path',
type: 'image/jpeg',
name: 'photo2.jpg',
};

expect(formData.getAll('photo')[1]).toMatchObject(expectedPart);

expect(formData.getAll('file').length).toBe(0);
});
});

0 comments on commit d05a5d1

Please sign in to comment.