Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
62 lines (51 loc) · 1.54 KB
/
Copy pathutils.ts
File metadata and controls
62 lines (51 loc) · 1.54 KB
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
export const noop = () => {}
export const isPromise = <T = any>(value: any): value is PromiseLike<T> => {
return value && typeof value === 'object' && typeof value.then === 'function'
}
export const run = (callback: any) => {
return callback()
}
export const isFunction = (thing: any): thing is Function => {
return typeof thing === 'function'
}
export const safeNotEqual = (a, b) => {
return a != a
? b == b
: a !== b || (a && typeof a === 'object') || typeof a === 'function'
}
export const notEqual = (a, b) => {
return a != a ? b == b : a !== b
}
export const validateStore = (store, name) => {
if (store != null && typeof store.subscribe !== 'function')
throw new Error(`'${name}' is not a store with a 'subscribe' method`)
}
export const subscribe = (store, ...callbacks) => {
if (store == null) return noop
const unsub = store.subscribe(...callbacks)
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub
}
export const getStoreValue = (store) => {
let value
subscribe(store, (_) => (value = _))()
return value
}
export const deepEqual = (x, y) => {
if (x === y) {
return true
} else if (
typeof x == 'object' &&
x != null &&
typeof y == 'object' &&
y != null
) {
if (Object.keys(x).length != Object.keys(y).length) return false
for (var prop in x) {
if (y.hasOwnProperty(prop)) {
if (!deepEqual(x[prop], y[prop])) return false
} else return false
}
return true
} else return false
}
export const clone = (object) => JSON.parse(JSON.stringify(object))