-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
data.ts
65 lines (50 loc) · 1.34 KB
/
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
60
61
62
63
64
65
/**
* Extends the target array with the given values.
*/
export function extend<T>(array: T[], values: Set<T>): void {
const l2 = values.size;
if (l2 === 0) return;
const l1 = array.length;
array.length += l2;
let i = 0;
values.forEach((value) => {
array[l1 + i] = value;
i++;
});
}
/**
* Checks whether the given value is a plain object.
*/
export function isPlainObject(value: unknown): boolean {
return typeof value === "object" && value !== null && value.constructor === Object;
}
/**
* Helper to use `Object.assign` with more than two objects.
*/
export function assign<T>(target: Partial<T> | undefined, ...objects: Array<Partial<T | undefined>>): T {
target = target || {};
for (let i = 0, l = objects.length; i < l; i++) {
const o = objects[i];
if (!o) continue;
Object.assign(target, o);
}
return target as T;
}
/**
* Very simple recursive `Object.assign` like function.
*/
export function assignDeep<T>(target: Partial<T> | undefined, ...objects: Array<Partial<T | undefined>>): T {
target = target || {};
for (let i = 0, l = objects.length; i < l; i++) {
const o = objects[i];
if (!o) continue;
for (const k in o) {
if (isPlainObject(o[k])) {
target[k] = assignDeep(target[k], o[k]);
} else {
target[k] = o[k];
}
}
}
return target as T;
}