Skip to content

Commit 4b19735

Browse files
committed
perf: 🍺 optimize hasFilesDeep function
1 parent ad4fd10 commit 4b19735

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/util/formData.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,23 @@ function appendToFormData(formData: FormData, key: string, value: any) {
2424
}
2525
return formData
2626
}
27-
if (typeof value === 'object') return objectToFormData(value, formData, key)
28-
throw new Error(`Unexpected value type: ${typeof value}`)
27+
objectToFormData(value, formData, key)
2928
}
3029
export function hasFilesDeep(obj: any): boolean {
31-
if (obj === null) return false
30+
if (!obj) return false
31+
3232
if (typeof obj === 'object') {
33-
const values = Object.values(obj)
34-
for (let i = 0; i < values.length; i++) {
35-
if (isFile(values[i])) return true
36-
}
33+
const objValues = Object.values(obj)
34+
if (objValues.some(isFile)) return true
3735
}
36+
3837
if (Array.isArray(obj)) {
39-
const firstNonNullElement = obj.find((el) => el !== null)
40-
return firstNonNullElement ? hasFilesDeep(firstNonNullElement) : false
38+
const nonNullElement = obj.find((el) => el !== null)
39+
if (nonNullElement) {
40+
return hasFilesDeep(nonNullElement)
41+
}
4142
}
43+
4244
return isFile(obj)
4345
}
4446
export function hasFiles(form: any) {

0 commit comments

Comments
 (0)