forked from vuejs/vue-web-component-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
96 lines (86 loc) · 2.13 KB
/
utils.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
96
const camelizeRE = /-(\w)/g
export const camelize = str => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
}
const hyphenateRE = /\B([A-Z])/g
export const hyphenate = str => {
return str.replace(hyphenateRE, '-$1').toLowerCase()
}
export function getInitialProps (propsList) {
const res = {}
propsList.forEach(key => {
res[key] = undefined
})
return res
}
export function injectHook (options, key, hook) {
options[key] = [].concat(options[key] || [])
options[key].unshift(hook)
}
export function callHooks (vm, hook) {
if (vm) {
const hooks = vm.$options[hook] || []
hooks.forEach(hook => {
hook.call(vm)
})
}
}
export function createCustomEvent (name, args) {
return new CustomEvent(name, {
bubbles: false,
cancelable: false,
detail: args
})
}
const isBoolean = val => /function Boolean/.test(String(val))
const isNumber = val => /function Number/.test(String(val))
export function convertAttributeValue (value, name, { type } = {}) {
if (isBoolean(type)) {
if (value === 'true' || value === 'false') {
return value === 'true'
}
if (value === '' || value === name || value != null) {
return true
}
return value
} else if (isNumber(type)) {
const parsed = parseFloat(value, 10)
return isNaN(parsed) ? value : parsed
} else {
return value
}
}
export function toVNodes (h, children) {
const res = []
for (let i = 0, l = children.length; i < l; i++) {
res.push(toVNode(h, children[i]))
}
return res
}
function toVNode (h, node) {
if (node.nodeType === 3) {
return node.data.trim() ? node.data : null
} else if (node.nodeType === 1) {
const data = {
attrs: getAttributes(node),
domProps: {
innerHTML: node.innerHTML
}
}
if (data.attrs.slot) {
data.slot = data.attrs.slot
delete data.attrs.slot
}
return h(node.tagName, data)
} else {
return null
}
}
function getAttributes (node) {
const res = {}
for (let i = 0, l = node.attributes.length; i < l; i++) {
const attr = node.attributes[i]
res[attr.nodeName] = attr.nodeValue
}
return res
}