-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathutils.ts
101 lines (83 loc) · 2.39 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
import Vue from 'vue'
const toString = (x: any) => Object.prototype.toString.call(x)
export function isNative(Ctor: any): boolean {
return typeof Ctor === 'function' && /native code/.test(Ctor.toString())
}
export const hasSymbol =
typeof Symbol !== 'undefined' &&
isNative(Symbol) &&
typeof Reflect !== 'undefined' &&
isNative(Reflect.ownKeys)
export const noopFn: any = (_: any) => _
const sharedPropertyDefinition = {
enumerable: true,
configurable: true,
get: noopFn,
set: noopFn,
}
export function proxy(
target: any,
key: string,
{ get, set }: { get?: Function; set?: Function }
) {
sharedPropertyDefinition.get = get || noopFn
sharedPropertyDefinition.set = set || noopFn
Object.defineProperty(target, key, sharedPropertyDefinition)
}
export function def(obj: Object, key: string, val: any, enumerable?: boolean) {
Object.defineProperty(obj, key, {
value: val,
enumerable: !!enumerable,
writable: true,
configurable: true,
})
}
export function hasOwn(obj: Object, key: string): boolean {
return Object.hasOwnProperty.call(obj, key)
}
export function assert(condition: any, msg: string) {
if (!condition) {
throw new Error(`[vue-composition-api] ${msg}`)
}
}
export function isPrimitive(value: any): boolean {
return (
typeof value === 'string' ||
typeof value === 'number' ||
// $flow-disable-line
typeof value === 'symbol' ||
typeof value === 'boolean'
)
}
export function isArray<T>(x: unknown): x is T[] {
return Array.isArray(x)
}
export function isValidArrayIndex(val: any): boolean {
const n = parseFloat(String(val))
return n >= 0 && Math.floor(n) === n && isFinite(val)
}
export function isObject(val: unknown): val is Record<any, any> {
return val !== null && typeof val === 'object'
}
export function isPlainObject(x: unknown): x is Record<any, any> {
return toString(x) === '[object Object]'
}
export function isFunction(x: unknown): x is Function {
return typeof x === 'function'
}
export function isUndef(v: any): boolean {
return v === undefined || v === null
}
export function warn(msg: string, vm?: Vue | null) {
Vue.util.warn(msg, vm)
}
export function logError(err: Error, vm: Vue, info: string) {
if (__DEV__) {
warn(`Error in ${info}: "${err.toString()}"`, vm)
}
if (typeof window !== 'undefined' && typeof console !== 'undefined') {
console.error(err)
} else {
throw err
}
}