Skip to content

Commit 58a523d

Browse files
committed
Use Map instead of object
1 parent d6a5d9f commit 58a523d

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1-
import { isPlainObject } from "./isPlainObject";
2-
31
export function formDataToPlainObject(formData: FormData) {
4-
const object: Record<string, unknown> = {};
2+
const object: Map<string, unknown> = new Map();
53
formData.forEach((value, key) => {
6-
if (typeof value === "object" && !isPlainObject(value)) {
4+
if (typeof value !== "string") {
75
return;
86
}
97

10-
if (!(key in object)) {
11-
object[key] = value;
12-
return;
13-
}
8+
if (object.has(key)) {
9+
// If the key already exists, treat it as an array
10+
const entry = object.get(key);
1411

15-
if (!Array.isArray(object[key])) {
16-
object[key] = [object[key], value];
12+
if (Array.isArray(entry)) {
13+
// If it's already an array, just push the new value
14+
entry.push(value);
15+
return;
16+
}
17+
18+
// Convert it to an array
19+
object.set(key, [object.get(key), value]);
1720
return;
1821
}
1922

20-
object[key].push(value);
23+
object.set(key, value);
2124
});
22-
return object;
25+
26+
return Object.fromEntries(object);
2327
}

0 commit comments

Comments
 (0)