deno.land / x / lume@v2.1.4 / core / utils / object.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const reactElement = Symbol.for("react.element");const objectConstructor = {}.constructor;
/** TypeScript helper to create optional properties recursively */export type DeepPartial<T> = T extends object ? { [P in keyof T]?: DeepPartial<T[P]>; } : T;
/** Check if the argument passed is a plain object */export function isPlainObject(obj: unknown): obj is Record<string, unknown> { return typeof obj === "object" && obj !== null && obj.constructor === objectConstructor && // @ts-ignore: Check if the argument passed is a React element obj["$$typeof"] !== reactElement && // @ts-ignore: Check if the argument passed is a Page.data object obj !== obj.page?.data;}
/** * Merge two objects recursively. * It's used to merge user options with default options. */export function merge<Type>( defaults: Type, user?: Type,): Required<Type> { const merged = { ...defaults };
if (!user) { return merged as unknown as Required<Type>; }
for (const [key, value] of Object.entries(user)) { if (value === undefined) { continue; }
// @ts-ignore: No index signature with a parameter of type 'string' was found on type 'unknown' if (isPlainObject(merged[key]) && isPlainObject(value)) { // @ts-ignore: Type 'string' cannot be used to index type 'Type' merged[key] = merge(merged[key], value); continue; }
// @ts-ignore: Type 'string' cannot be used to index type 'Type' merged[key] = value; }
return merged as unknown as Required<Type>;}
/** * Merge two objects recursively. * It's like merge() but it mutates the first value. */export function assign<Type>( target: Type, override?: Type,) { if (!override) { return; }
for (const [key, value] of Object.entries(override)) { if (value === undefined) { continue; }
// @ts-ignore: No index signature with a parameter of type 'string' was found on type 'unknown' if (isPlainObject(target[key]) && isPlainObject(value)) { // @ts-ignore: Type 'string' cannot be used to index type 'Type' target[key] = { ...target[key] }; // @ts-ignore: Type 'string' cannot be used to index type 'Type' assign(target[key], value); continue; }
// @ts-ignore: Type 'string' cannot be used to index type 'Type' target[key] = value; }}
lume

Version Info

Tagged at
7 months ago