-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
utils.ts
195 lines (167 loc) · 6.05 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {
IKeyValueMap,
IObservableArray,
ObservableMap,
globalState,
isObservableArray,
isObservableMap
} from "../internal"
export const OBFUSCATED_ERROR =
"An invariant failed, however the error is obfuscated because this is an production build."
export const EMPTY_ARRAY = []
Object.freeze(EMPTY_ARRAY)
export const EMPTY_OBJECT = {}
Object.freeze(EMPTY_OBJECT)
export interface Lambda {
(): void
name?: string
}
export function getNextId() {
return ++globalState.mobxGuid
}
export function fail(message: string | boolean): never {
invariant(false, message)
throw "X" // unreachable
}
export function invariant(check: false, message?: string | boolean): never
export function invariant(check: true, message?: string | boolean): void
export function invariant(check: any, message?: string | boolean): void
export function invariant(check: boolean, message?: string | boolean) {
if (!check) throw new Error("[mobx] " + (message || OBFUSCATED_ERROR))
}
/**
* Prints a deprecation message, but only one time.
* Returns false if the deprecated message was already printed before
*/
const deprecatedMessages: string[] = []
export function deprecated(msg: string): boolean
export function deprecated(thing: string, replacement: string): boolean
export function deprecated(msg: string, thing?: string): boolean {
if (process.env.NODE_ENV === "production") return false
if (thing) {
return deprecated(`'${msg}', use '${thing}' instead.`)
}
if (deprecatedMessages.indexOf(msg) !== -1) return false
deprecatedMessages.push(msg)
console.error("[mobx] Deprecated: " + msg)
return true
}
/**
* Makes sure that the provided function is invoked at most once.
*/
export function once(func: Lambda): Lambda {
let invoked = false
return function() {
if (invoked) return
invoked = true
return (func as any).apply(this, arguments)
}
}
export const noop = () => {}
export function unique<T>(list: T[]): T[] {
const res: T[] = []
list.forEach(item => {
if (res.indexOf(item) === -1) res.push(item)
})
return res
}
export function isObject(value: any): boolean {
return value !== null && typeof value === "object"
}
export function isPlainObject(value) {
if (value === null || typeof value !== "object") return false
const proto = Object.getPrototypeOf(value)
return proto === Object.prototype || proto === null
}
export function convertToMap(dataStructure) {
if (isES6Map(dataStructure) || isObservableMap(dataStructure)) {
return dataStructure
} else if (Array.isArray(dataStructure)) {
return new Map(dataStructure)
} else if (isPlainObject(dataStructure)) {
return new Map(Object.entries(dataStructure))
} else {
return fail(`Cannot convert to map from '${dataStructure}'`)
}
}
export function makeNonEnumerable(object: any, propNames: PropertyKey[]) {
for (let i = 0; i < propNames.length; i++) {
addHiddenProp(object, propNames[i], object[propNames[i]])
}
}
export function addHiddenProp(object: any, propName: PropertyKey, value: any) {
Object.defineProperty(object, propName, {
enumerable: false,
writable: true,
configurable: true,
value
})
}
export function addHiddenFinalProp(object: any, propName: PropertyKey, value: any) {
Object.defineProperty(object, propName, {
enumerable: false,
writable: false,
configurable: true,
value
})
}
export function isPropertyConfigurable(object: any, prop: PropertyKey): boolean {
const descriptor = Object.getOwnPropertyDescriptor(object, prop)
return !descriptor || (descriptor.configurable !== false && descriptor.writable !== false)
}
export function assertPropertyConfigurable(object: any, prop: PropertyKey) {
if (process.env.NODE_ENV !== "production" && !isPropertyConfigurable(object, prop))
fail(
`Cannot make property '${prop.toString()}' observable, it is not configurable and writable in the target object`
)
}
export function createInstanceofPredicate<T>(
name: string,
clazz: new (...args: any[]) => T
): (x: any) => x is T {
const propName = "isMobX" + name
clazz.prototype[propName] = true
return function(x) {
return isObject(x) && x[propName] === true
} as any
}
/**
* Returns whether the argument is an array, disregarding observability.
*/
export function isArrayLike(x: any): x is Array<any> | IObservableArray<any> {
return Array.isArray(x) || isObservableArray(x)
}
export function isES6Map(thing): boolean {
return thing instanceof Map
}
export function isES6Set(thing): thing is Set<any> {
return thing instanceof Set
}
/**
* Returns the following: own keys, prototype keys & own symbol keys, if they are enumerable.
*/
export function getPlainObjectKeys(object) {
const enumerables = new Set<PropertyKey>()
for (let key in object) enumerables.add(key) // *all* enumerables
Object.getOwnPropertySymbols(object).forEach(k => {
if (Object.getOwnPropertyDescriptor(object, k)!.enumerable) enumerables.add(k)
}) // *own* symbols
// Note: this implementation is missing enumerable, inherited, symbolic property names! That would however pretty expensive to add,
// as there is no efficient iterator that returns *all* properties
return Array.from(enumerables)
}
export function stringifyKey(key: any): string {
if (key && key.toString) return key.toString()
else return new String(key).toString()
}
export function getMapLikeKeys<K, V>(map: ObservableMap<K, V>): ReadonlyArray<K>
export function getMapLikeKeys<V>(map: IKeyValueMap<V> | any): ReadonlyArray<string>
export function getMapLikeKeys(map: any): any {
if (isPlainObject(map)) return Object.keys(map)
if (Array.isArray(map)) return map.map(([key]) => key)
if (isES6Map(map) || isObservableMap(map)) return Array.from(map.keys())
return fail(`Cannot get keys from '${map}'`)
}
export function toPrimitive(value) {
return value === null ? null : typeof value === "object" ? "" + value : value
}