-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"signal-form": patch | ||
--- | ||
|
||
Validate search params |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,42 @@ | ||
type ParsedObject = { [key: string]: any }; | ||
export function parseFormData(data: any): any { | ||
if (data instanceof URLSearchParams || data instanceof FormData) { | ||
let result: any = {}; | ||
|
||
export function parseFormData(formData: FormData): ParsedObject { | ||
let result: ParsedObject = {}; | ||
// Iterate over each form data entry | ||
for (let [key, value] of data) { | ||
let current = result; | ||
let keys = key.split(/[\.\[\]]/).filter((k) => k !== ""); // Split the key into parts and filter out empty strings | ||
|
||
// Iterate over each form data entry | ||
for (let [key, value] of formData) { | ||
let current = result; | ||
let keys = key.split(/[\.\[\]]/).filter((k) => k !== ""); // Split the key into parts and filter out empty strings | ||
|
||
keys.forEach((k, index) => { | ||
// Check if it's the last key | ||
if (index === keys.length - 1) { | ||
// Keys may occur multiple times in the form data, this could be the | ||
// case for multi-selects for example. In this case we need to convert | ||
// the value to an array. | ||
if (current.hasOwnProperty(k)) { | ||
// if it's already an array, append the value, otherwise convert it | ||
if (Array.isArray(current[k])) { | ||
current[k] = [...current[k], value]; | ||
keys.forEach((k, index) => { | ||
// Check if it's the last key | ||
if (index === keys.length - 1) { | ||
// Keys may occur multiple times in the form data, this could be the | ||
// case for multi-selects for example. In this case we need to convert | ||
// the value to an array. | ||
if (current.hasOwnProperty(k)) { | ||
// if it's already an array, append the value, otherwise convert it | ||
if (Array.isArray(current[k])) { | ||
current[k] = [...current[k], value]; | ||
} else { | ||
current[k] = [current[k], value]; | ||
} | ||
} else { | ||
current[k] = [current[k], value]; | ||
current[k] = value; | ||
} | ||
} else { | ||
current[k] = value; | ||
} | ||
} else { | ||
// Initialize the next level in the structure if it doesn't exist | ||
if (!current[k]) { | ||
current[k] = isNaN(parseInt(keys[index + 1] || "")) ? {} : []; | ||
// Initialize the next level in the structure if it doesn't exist | ||
if (!current[k]) { | ||
current[k] = isNaN(parseInt(keys[index + 1] || "")) ? {} : []; | ||
} | ||
|
||
// Move to the next level in the structure | ||
current = current[k]; | ||
} | ||
}); | ||
} | ||
|
||
// Move to the next level in the structure | ||
current = current[k]; | ||
} | ||
}); | ||
return result; | ||
} else { | ||
return data; | ||
} | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters