-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmicroh.js
30 lines (26 loc) · 887 Bytes
/
microh.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
const { isArray } = Array
const microh = (h, classKey = 'className') => {
const vnode = h('a', { z: 1 })
const [tagKey, attrKey] = Object.keys(vnode).filter(k => {
const val = vnode[k]
return val && (val === 'a' || val.z)
})
const parseTag = (tag, attrs) => {
if (typeof tag != 'string') return tag
const idx = tag.indexOf('.')
if (idx >= 0) {
const className = tag.slice(idx + 1).replace(/\./g, ' ')
attrs[classKey] = attrs[classKey] ? className + ' ' + String(attrs[classKey]) : className
tag = tag.slice(0, idx)
}
return tag || 'div'
}
return (tag, ...children) => {
let attrs = children[0]
if (attrs && typeof attrs == 'object' && !isArray(attrs) && (!attrs[tagKey] || !attrs[attrKey]))
children.shift()
else attrs = {}
return h(parseTag(tag, attrs), attrs, ...children)
}
}
export default microh