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: https://github.com/facebook/react-native/pull/32444

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

Reviewed By: lunaleaps

Differential Revision: D31798633

Pulled By: cortinico

fbshipit-source-id: ef29bb54e930532a671adbe707be8d1a64ff0d82
This commit is contained in:
matinzd 2022-03-31 07:59:01 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 8b81dea74e
Коммит d05a5d1551
2 изменённых файлов: 37 добавлений и 0 удалений

Просмотреть файл

@ -64,6 +64,12 @@ class FormData {
this._parts.push([key, value]); this._parts.push([key, value]);
} }
getAll(key: string): Array<FormDataValue> {
return this._parts
.filter(([name]) => name === key)
.map(([, value]) => value);
}
getParts(): Array<FormDataPart> { getParts(): Array<FormDataPart> {
return this._parts.map(([name, value]) => { return this._parts.map(([name, value]) => {
const contentDisposition = 'form-data; name="' + name + '"'; const contentDisposition = 'form-data; name="' + name + '"';

Просмотреть файл

@ -77,4 +77,35 @@ describe('FormData', function () {
}; };
expect(formData.getParts()[0]).toMatchObject(expectedPart); 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);
});
}); });