-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize-dictionaries.ts
More file actions
40 lines (38 loc) · 992 Bytes
/
normalize-dictionaries.ts
File metadata and controls
40 lines (38 loc) · 992 Bytes
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
import { isDictionary } from "../utils";
import type { NormalizedData } from "./types";
export function normalizeDictionaries(data: any): NormalizedData {
if (Array.isArray(data)) {
return data;
} else if (isDictionary(data)) {
const types = Array.from(
new Set(
Object.values(data)
.filter((value) => value !== null)
.map((value) => typeof value),
),
);
if (types.length === 1) {
return Object.entries(data).map(([key, value]: [string, any]) => ({
key,
value,
}));
} else {
const baseRow = Object.fromEntries(
types.map((type) => [`value_${type}`, null]),
);
return Object.entries(data).map(([key, value]) => ({
key,
...baseRow,
...(value === null
? {}
: {
[`value_${typeof value}`]: value,
}),
}));
}
} else if (Object(data) === data) {
return [data];
} else {
return [];
}
}