-
Notifications
You must be signed in to change notification settings - Fork 668
/
Copy pathutil.js
95 lines (78 loc) · 2.49 KB
/
util.js
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
// @flow
import Vue from 'vue'
import semver from 'semver'
import { config } from '@vue/test-utils'
export function throwError(msg: string): void {
throw new Error(`[vue-test-utils]: ${msg}`)
}
export function warn(msg: string): void {
console.error(`[vue-test-utils]: ${msg}`)
}
const camelizeRE = /-(\w)/g
export const camelize = (str: string): string => {
const camelizedStr = str.replace(camelizeRE, (_, c) =>
c ? c.toUpperCase() : ''
)
return camelizedStr.charAt(0).toLowerCase() + camelizedStr.slice(1)
}
/**
* Capitalize a string.
*/
export const capitalize = (str: string): string =>
str.charAt(0).toUpperCase() + str.slice(1)
/**
* Hyphenate a camelCase string.
*/
const hyphenateRE = /\B([A-Z])/g
export const hyphenate = (str: string): string =>
str.replace(hyphenateRE, '-$1').toLowerCase()
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop)
}
export function keys<T: string>(obj: any): Array<T> {
return Object.keys(obj)
}
export function resolveComponent(id: string, components: Object) {
if (typeof id !== 'string') {
return
}
// check local registration variations first
if (hasOwnProperty(components, id)) {
return components[id]
}
var camelizedId = camelize(id)
if (hasOwnProperty(components, camelizedId)) {
return components[camelizedId]
}
var PascalCaseId = capitalize(camelizedId)
if (hasOwnProperty(components, PascalCaseId)) {
return components[PascalCaseId]
}
// fallback to prototype chain
return components[id] || components[camelizedId] || components[PascalCaseId]
}
const UA =
typeof window !== 'undefined' &&
'navigator' in window &&
navigator.userAgent.toLowerCase()
export const isPhantomJS = UA && UA.includes && UA.match(/phantomjs/i)
export const isEdge = UA && UA.indexOf('edge/') > 0
export const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge
// get the event used to trigger v-model handler that updates bound data
export function getCheckedEvent() {
const version = Vue.version
if (semver.satisfies(version, '2.1.9 - 2.1.10')) {
return 'click'
}
if (semver.satisfies(version, '2.2 - 2.4')) {
return isChrome ? 'click' : 'change'
}
// change is handler for version 2.0 - 2.1.8, and 2.5+
return 'change'
}
export function warnDeprecated(method: string, fallback: string = '') {
if (!config.showDeprecationWarnings) return
let msg = `${method} is deprecated and will removed in the next major version`
if (fallback) msg += ` ${fallback}`
warn(msg)
}