提交 | 用户 | 时间
|
820397
|
1 |
// copy to vben-admin |
H |
2 |
|
|
3 |
const toString = Object.prototype.toString |
|
4 |
|
|
5 |
export const is = (val: unknown, type: string) => { |
|
6 |
return toString.call(val) === `[object ${type}]` |
|
7 |
} |
|
8 |
|
|
9 |
export const isDef = <T = unknown>(val?: T): val is T => { |
|
10 |
return typeof val !== 'undefined' |
|
11 |
} |
|
12 |
|
|
13 |
export const isUnDef = <T = unknown>(val?: T): val is T => { |
|
14 |
return !isDef(val) |
|
15 |
} |
|
16 |
|
|
17 |
export const isObject = (val: any): val is Record<any, any> => { |
|
18 |
return val !== null && is(val, 'Object') |
|
19 |
} |
|
20 |
|
|
21 |
export const isEmpty = <T = unknown>(val: T): val is T => { |
|
22 |
if (val === null) { |
|
23 |
return true |
|
24 |
} |
|
25 |
if (isArray(val) || isString(val)) { |
|
26 |
return val.length === 0 |
|
27 |
} |
|
28 |
|
|
29 |
if (val instanceof Map || val instanceof Set) { |
|
30 |
return val.size === 0 |
|
31 |
} |
|
32 |
|
|
33 |
if (isObject(val)) { |
|
34 |
return Object.keys(val).length === 0 |
|
35 |
} |
|
36 |
|
|
37 |
return false |
|
38 |
} |
|
39 |
|
|
40 |
export const isDate = (val: unknown): val is Date => { |
|
41 |
return is(val, 'Date') |
|
42 |
} |
|
43 |
|
|
44 |
export const isNull = (val: unknown): val is null => { |
|
45 |
return val === null |
|
46 |
} |
|
47 |
|
|
48 |
export const isNullAndUnDef = (val: unknown): val is null | undefined => { |
|
49 |
return isUnDef(val) && isNull(val) |
|
50 |
} |
|
51 |
|
|
52 |
export const isNullOrUnDef = (val: unknown): val is null | undefined => { |
|
53 |
return isUnDef(val) || isNull(val) |
|
54 |
} |
|
55 |
|
|
56 |
export const isNumber = (val: unknown): val is number => { |
|
57 |
return is(val, 'Number') |
|
58 |
} |
|
59 |
|
|
60 |
export const isPromise = <T = any>(val: unknown): val is Promise<T> => { |
|
61 |
return is(val, 'Promise') && isObject(val) && isFunction(val.then) && isFunction(val.catch) |
|
62 |
} |
|
63 |
|
|
64 |
export const isString = (val: unknown): val is string => { |
|
65 |
return is(val, 'String') |
|
66 |
} |
|
67 |
|
|
68 |
export const isFunction = (val: unknown): val is Function => { |
|
69 |
return typeof val === 'function' |
|
70 |
} |
|
71 |
|
|
72 |
export const isBoolean = (val: unknown): val is boolean => { |
|
73 |
return is(val, 'Boolean') |
|
74 |
} |
|
75 |
|
|
76 |
export const isRegExp = (val: unknown): val is RegExp => { |
|
77 |
return is(val, 'RegExp') |
|
78 |
} |
|
79 |
|
|
80 |
export const isArray = (val: any): val is Array<any> => { |
|
81 |
return val && Array.isArray(val) |
|
82 |
} |
|
83 |
|
|
84 |
export const isWindow = (val: any): val is Window => { |
|
85 |
return typeof window !== 'undefined' && is(val, 'Window') |
|
86 |
} |
|
87 |
|
|
88 |
export const isElement = (val: unknown): val is Element => { |
|
89 |
return isObject(val) && !!val.tagName |
|
90 |
} |
|
91 |
|
|
92 |
export const isMap = (val: unknown): val is Map<any, any> => { |
|
93 |
return is(val, 'Map') |
|
94 |
} |
|
95 |
|
|
96 |
export const isServer = typeof window === 'undefined' |
|
97 |
|
|
98 |
export const isClient = !isServer |
|
99 |
|
|
100 |
export const isUrl = (path: string): boolean => { |
|
101 |
const reg = |
|
102 |
/(((^https?:(?:\/\/)?)(?:[-:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&%@.\w_]*)#?(?:[\w]*))?)$/ |
|
103 |
return reg.test(path) |
|
104 |
} |
|
105 |
|
|
106 |
export const isDark = (): boolean => { |
|
107 |
return window.matchMedia('(prefers-color-scheme: dark)').matches |
|
108 |
} |
|
109 |
|
|
110 |
// 是否是图片链接 |
|
111 |
export const isImgPath = (path: string): boolean => { |
|
112 |
return /(https?:\/\/|data:image\/).*?\.(png|jpg|jpeg|gif|svg|webp|ico)/gi.test(path) |
|
113 |
} |
|
114 |
|
|
115 |
export const isEmptyVal = (val: any): boolean => { |
|
116 |
return val === '' || val === null || val === undefined |
|
117 |
} |