deno.land / x / lume@v2.1.4 / core / utils / merge_data.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
export type MergeStrategy = "array" | "stringArray" | "object";
interface DataToMerge { mergedKeys?: Record<string, MergeStrategy>; [key: string]: unknown;}
/** Merge the cascade data */export function mergeData(...datas: DataToMerge[]): DataToMerge { return datas.reduce((previous, current) => { const data: DataToMerge = { ...previous, ...current };
// Merge special keys const mergedKeys = { ...previous.mergedKeys, ...current.mergedKeys, };
for (const [key, type] of Object.entries(mergedKeys)) { switch (type) { case "stringArray": data[key] = mergeStringArray(previous[key], current[key]); break; case "array": data[key] = mergeArray(previous[key], current[key]); break; case "object": data[key] = mergeObject(previous[key], current[key]); break; } }
return data; });}
function toArray(value: unknown): unknown[] { return Array.isArray(value) ? value : (value === undefined || value === null) ? [] : [value];}
function mergeArray(previous: unknown, current: unknown): unknown[] { return [...new Set([...toArray(previous), ...toArray(current)])];}
function mergeStringArray(previous: unknown, current: unknown): string[] { return [...new Set(mergeArray(previous, current).map(String))];}
function mergeObject( previous: unknown, current: unknown,): Record<string, unknown> { return { ...previous ?? {}, ...current ?? {} };}
lume

Version Info

Tagged at
7 months ago