1 | 'use strict';
|
---|
2 |
|
---|
3 | import utils from '../utils.js';
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * It takes a string like `foo[x][y][z]` and returns an array like `['foo', 'x', 'y', 'z']
|
---|
7 | *
|
---|
8 | * @param {string} name - The name of the property to get.
|
---|
9 | *
|
---|
10 | * @returns An array of strings.
|
---|
11 | */
|
---|
12 | function parsePropPath(name) {
|
---|
13 | // foo[x][y][z]
|
---|
14 | // foo.x.y.z
|
---|
15 | // foo-x-y-z
|
---|
16 | // foo x y z
|
---|
17 | return utils.matchAll(/\w+|\[(\w*)]/g, name).map(match => {
|
---|
18 | return match[0] === '[]' ? '' : match[1] || match[0];
|
---|
19 | });
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Convert an array to an object.
|
---|
24 | *
|
---|
25 | * @param {Array<any>} arr - The array to convert to an object.
|
---|
26 | *
|
---|
27 | * @returns An object with the same keys and values as the array.
|
---|
28 | */
|
---|
29 | function arrayToObject(arr) {
|
---|
30 | const obj = {};
|
---|
31 | const keys = Object.keys(arr);
|
---|
32 | let i;
|
---|
33 | const len = keys.length;
|
---|
34 | let key;
|
---|
35 | for (i = 0; i < len; i++) {
|
---|
36 | key = keys[i];
|
---|
37 | obj[key] = arr[key];
|
---|
38 | }
|
---|
39 | return obj;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * It takes a FormData object and returns a JavaScript object
|
---|
44 | *
|
---|
45 | * @param {string} formData The FormData object to convert to JSON.
|
---|
46 | *
|
---|
47 | * @returns {Object<string, any> | null} The converted object.
|
---|
48 | */
|
---|
49 | function formDataToJSON(formData) {
|
---|
50 | function buildPath(path, value, target, index) {
|
---|
51 | let name = path[index++];
|
---|
52 |
|
---|
53 | if (name === '__proto__') return true;
|
---|
54 |
|
---|
55 | const isNumericKey = Number.isFinite(+name);
|
---|
56 | const isLast = index >= path.length;
|
---|
57 | name = !name && utils.isArray(target) ? target.length : name;
|
---|
58 |
|
---|
59 | if (isLast) {
|
---|
60 | if (utils.hasOwnProp(target, name)) {
|
---|
61 | target[name] = [target[name], value];
|
---|
62 | } else {
|
---|
63 | target[name] = value;
|
---|
64 | }
|
---|
65 |
|
---|
66 | return !isNumericKey;
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (!target[name] || !utils.isObject(target[name])) {
|
---|
70 | target[name] = [];
|
---|
71 | }
|
---|
72 |
|
---|
73 | const result = buildPath(path, value, target[name], index);
|
---|
74 |
|
---|
75 | if (result && utils.isArray(target[name])) {
|
---|
76 | target[name] = arrayToObject(target[name]);
|
---|
77 | }
|
---|
78 |
|
---|
79 | return !isNumericKey;
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (utils.isFormData(formData) && utils.isFunction(formData.entries)) {
|
---|
83 | const obj = {};
|
---|
84 |
|
---|
85 | utils.forEachEntry(formData, (name, value) => {
|
---|
86 | buildPath(parsePropPath(name), value, obj, 0);
|
---|
87 | });
|
---|
88 |
|
---|
89 | return obj;
|
---|
90 | }
|
---|
91 |
|
---|
92 | return null;
|
---|
93 | }
|
---|
94 |
|
---|
95 | export default formDataToJSON;
|
---|