File tree Expand file tree Collapse file tree 1 file changed +16
-12
lines changed
Expand file tree Collapse file tree 1 file changed +16
-12
lines changed Original file line number Diff line number Diff line change 1- import { isPlainObject } from "./isPlainObject" ;
2-
31export 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}
You can’t perform that action at this time.
0 commit comments