-
Notifications
You must be signed in to change notification settings - Fork 14
/
utils.ts
129 lines (111 loc) · 3.38 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import type { RawType, StorageLike } from '@/types'
export const isArray = Array.isArray
export function isSet(val: unknown): val is Set<any> {
return getTypeString(val) === '[object Set]'
}
export function isMap(val: unknown): val is Map<any, any> {
return getTypeString(val) === '[object Map]'
}
export function isDate(val: unknown): val is Date {
return getTypeString(val) === '[object Date]'
}
export function isRegExp(val: unknown): val is RegExp {
return getTypeString(val) === '[object RegExp]'
}
export function isURL(val: unknown): val is URL {
return getTypeString(val) === '[object URL]'
}
export function isError(val: unknown): val is Error {
return !!val && Object.getPrototypeOf(val)?.name === 'Error'
}
export function isFunction(val: unknown): val is Function {
return typeof val === 'function'
}
export function isNumber(val: unknown): val is number {
return typeof val === 'number'
}
export function isString(val: unknown): val is string {
return typeof val === 'string'
}
export function isObject(val: unknown): val is Record<any, any> {
return val !== null && typeof val === 'object'
}
export function isPromise<T = any>(val: unknown): val is Promise<T> {
return (
(isObject(val) || isFunction(val))
&& isFunction((val as any).then)
&& isFunction((val as any).catch)
)
}
export function isIntegerKey(key: unknown) {
return typeof key === 'string'
&& key !== 'NaN'
&& key[0] !== '-'
&& `${parseInt(key, 10)}` === key
}
export async function isStorage(storage: StorageLike) {
return ['clear', 'getItem', 'key', 'setItem', 'removeItem'].every(method => isFunction(storage[method]))
}
export function isLocalStorage(storage: StorageLike) {
return storage === window.localStorage
}
export function isSessionStorage(storage: StorageLike) {
return storage === window.sessionStorage
}
export function getTypeString(value: unknown): string {
return Object.prototype.toString.call(value)
}
export function getRawType(value: unknown): RawType {
return getTypeString(value).slice(8, -1) as RawType
}
export function hasChanged(value: any, oldValue: any): boolean {
return !Object.is(value, oldValue)
}
export function transformJSON(
data: string,
): object | string {
try {
return JSON.parse(data)
}
catch (e) {
return data
}
}
// prototies exist in the prototype chain
export function propertyIsInPrototype(object: object, prototypeName: string) {
return !hasOwn(object, prototypeName) && (prototypeName in object)
}
const hasOwnProperty = Object.prototype.hasOwnProperty
export function hasOwn(val: object,
key: string | symbol): key is keyof typeof val {
return hasOwnProperty.call(val, key)
}
export function transformEval(code: string) {
// runs in the global scope rather than the local one
const eval2 = eval
return (function () {
return eval2(code)
})()
}
export function formatTime(time: any) {
if (isDate(time))
return time.getTime()
if (isString(time))
return +time.padEnd(13, '0')
return time
}
let prevPromise = Promise.resolve()
// TODO: Maybe there is a better solution
export function pThen(getter: Function, callback: Function) {
const maybePromise = getter()
if (isPromise(maybePromise)) {
prevPromise = prevPromise.then(() => getter()).then((res) => {
prevPromise = Promise.resolve()
return callback(res)
})
return prevPromise
}
else {
return callback(maybePromise)
}
}