-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathutils.ts
41 lines (31 loc) · 1013 Bytes
/
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
export function isCallable(fn: unknown): fn is (...args: any[]) => any {
return typeof fn === 'function';
}
export function isNullOrUndefined(value: unknown): value is undefined | null {
return value === null || value === undefined;
}
export function isEmptyArray(arr: unknown): boolean {
return Array.isArray(arr) && arr.length === 0;
}
export const isObject = (obj: unknown): obj is Record<string, unknown> =>
obj !== null && !!obj && typeof obj === 'object' && !Array.isArray(obj);
export function isIndex(value: unknown): value is number {
return Number(value) >= 0;
}
export function toNumber(value: string): number | string {
const n = parseFloat(value);
return isNaN(n) ? value : n;
}
export function merge(target: any, source: any) {
Object.keys(source).forEach(key => {
if (isObject(source[key])) {
if (!target[key]) {
target[key] = {};
}
merge(target[key], source[key]);
return;
}
target[key] = source[key];
});
return target;
}