-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
78 lines (61 loc) · 1.92 KB
/
utils.ts
File metadata and controls
78 lines (61 loc) · 1.92 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
export const noop = () => { }
export const identity = x => x
export const assign = <T, S>(tar: T, src: S): T & S => {
// @ts-ignore
for (const k in src) tar[k] = src[k]
return tar as T & S
}
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 blankObject = () => {
return Object.create(null)
}
export const runAll = (fns) => {
fns.forEach(run)
}
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 isEmpty = (obj) => {
return Object.keys(obj).length === 0
}
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 once = (fn) => {
let ran = false
return function (this: any, ...args) {
if (ran) return
ran = true
fn.call(this, ...args)
}
}
export const nullToEmpty = (value) => {
return value == null ? '' : value
}
export const hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
export const actionDestroyer = (action_result) => {
return action_result && isFunction(action_result.destroy) ? action_result.destroy : noop
}