Skip to content

Commit d05a5d1

Browse files
matinzdfacebook-github-bot
authored andcommitted
feat: add getAll function to formdata (#32444)
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
1 parent 8b81dea commit d05a5d1

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Libraries/Network/FormData.js

+6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ class FormData {
6464
this._parts.push([key, value]);
6565
}
6666

67+
getAll(key: string): Array<FormDataValue> {
68+
return this._parts
69+
.filter(([name]) => name === key)
70+
.map(([, value]) => value);
71+
}
72+
6773
getParts(): Array<FormDataPart> {
6874
return this._parts.map(([name, value]) => {
6975
const contentDisposition = 'form-data; name="' + name + '"';

Libraries/Network/__tests__/FormData-test.js

+31
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,35 @@ describe('FormData', function () {
7777
};
7878
expect(formData.getParts()[0]).toMatchObject(expectedPart);
7979
});
80+
81+
it('should return values based on the given key', function () {
82+
formData.append('username', 'Chris');
83+
formData.append('username', 'Bob');
84+
85+
expect(formData.getAll('username').length).toBe(2);
86+
87+
expect(formData.getAll('username')).toMatchObject(['Chris', 'Bob']);
88+
89+
formData.append('photo', {
90+
uri: 'arbitrary/path',
91+
type: 'image/jpeg',
92+
name: 'photo3.jpg',
93+
});
94+
95+
formData.append('photo', {
96+
uri: 'arbitrary/path',
97+
type: 'image/jpeg',
98+
name: 'photo2.jpg',
99+
});
100+
101+
const expectedPart = {
102+
uri: 'arbitrary/path',
103+
type: 'image/jpeg',
104+
name: 'photo2.jpg',
105+
};
106+
107+
expect(formData.getAll('photo')[1]).toMatchObject(expectedPart);
108+
109+
expect(formData.getAll('file').length).toBe(0);
110+
});
80111
});

0 commit comments

Comments
 (0)