-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathutils.ts
111 lines (91 loc) · 2.63 KB
/
utils.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
export function getUnserializable(
target?: any,
path: string[] = []
): false | { path: string[]; value: any } {
// Guard against undefined and null, e.g. a reducer that returns undefined
if ((isUndefined(target) || isNull(target)) && path.length === 0) {
return {
path: ['root'],
value: target,
};
}
const keys = Object.keys(target);
return keys.reduce<false | { path: string[]; value: any }>((result, key) => {
if (result) {
return result;
}
const value = (target as any)[key];
if (
isUndefined(value) ||
isNull(value) ||
isNumber(value) ||
isBoolean(value) ||
isString(value) ||
isArray(value)
) {
return false;
}
if (isPlainObject(value)) {
return getUnserializable(value, [...path, key]);
}
return {
path: [...path, key],
value,
};
}, false);
}
export function throwIfUnserializable(
unserializable: false | { path: string[]; value: any },
context: 'state' | 'action'
) {
if (unserializable === false) {
return;
}
const unserializablePath = unserializable.path.join('.');
const error: any = new Error(
`Detected unserializable ${context} at "${unserializablePath}"`
);
error.value = unserializable.value;
error.unserializablePath = unserializablePath;
throw error;
}
/**
* Object Utilities
*/
export function isUndefined(target: any): target is undefined {
return target === undefined;
}
export function isNull(target: any): target is null {
return target === null;
}
export function isArray(target: any): target is Array<any> {
return Array.isArray(target);
}
export function isString(target: any): target is string {
return typeof target === 'string';
}
export function isBoolean(target: any): target is boolean {
return typeof target === 'boolean';
}
export function isNumber(target: any): target is number {
return typeof target === 'number';
}
export function isObjectLike(target: any): target is object {
return typeof target === 'object' && target !== null;
}
export function isObject(target: any): target is object {
return isObjectLike(target) && !isArray(target);
}
export function isPlainObject(target: any): target is object {
if (!isObject(target)) {
return false;
}
const targetPrototype = Object.getPrototypeOf(target);
return targetPrototype === Object.prototype || targetPrototype === null;
}
export function isFunction(target: any): target is Function {
return typeof target === 'function';
}
export function hasOwnProperty(target: object, propertyName: string): boolean {
return Object.prototype.hasOwnProperty.call(target, propertyName);
}