-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.ts
49 lines (40 loc) · 1.13 KB
/
util.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
import { useEffect, useRef, useState } from 'react';
export function useForceUpdate(): () => void {
const mounted = useRef(false);
const [, updateState] = useState(0);
function handleUpdate(): void {
if (mounted.current) {
updateState((state) => state + 1);
}
}
useEffect(() => {
mounted.current = true;
return () => {
mounted.current = false;
};
}, []);
return () => {
handleUpdate();
};
}
export function isEqual<T>(a: T, b: T): boolean {
if (a === b) return true; // faster in case there's actual equality
return JSON.stringify(a) === JSON.stringify(b);
}
export function uniq(value: string[]): string[] {
return [...new Set(value)];
}
export function compact<T>(array: Array<T | undefined | false | null>): T[] {
return array.filter(Boolean) as T[];
}
export function copy<T>(value: T): T {
if (Array.isArray(value)) {
return value.slice() as unknown as T;
} else if (value && isObject(value)) {
return { ...value };
}
return value;
}
export function isObject(value: unknown): boolean {
return !!value && typeof value === 'object' && value.constructor === Object;
}