-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreform.ts
63 lines (59 loc) · 1.77 KB
/
reform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { parseName } from "./parse_name.ts";
function isPlainObject(value: unknown): value is ReformData {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
export interface ReformData {
[name: string]: ReformDataValue;
}
export type ReformDataValue =
| File
| string
| ReformData
| (ReformDataValue | null)[];
export function reform<T extends ReformData = ReformData>(form: FormData): T {
const reformed: ReformData = {};
for (const [name, value] of form.entries()) {
const keys = parseName(name); // always length > 1
// deno-lint-ignore no-explicit-any
let data: any = reformed;
for (const [keyIndex, key] of keys.entries()) {
if (keyIndex + 1 === keys.length) {
if (typeof key === "string") {
data[key] = value;
} else if (typeof key === "number") {
data[key] = value;
// fill sparse
for (let i = 0; i < key; i++) {
if (!(i in data)) {
data[i] = null;
}
}
} else {
data.push(value);
}
break;
}
const nextKey = keys[keyIndex + 1];
if (typeof key === "string" || typeof key === "number") {
if (typeof nextKey === "string") {
data[key] = isPlainObject(data[key]) ? data[key] : {};
data = data[key];
} else {
data[key] = Array.isArray(data[key]) ? data[key] : [];
data = data[key];
}
} else if (Array.isArray(data)) {
if (typeof nextKey === "string") {
const next: ReformData = {};
data.push(next);
data = next;
} else {
const next = [] as ReformDataValue[];
data.push(next);
data = next;
}
}
}
}
return reformed as T;
}