-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathh.js
60 lines (48 loc) · 1.45 KB
/
h.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
import { renderNode, renderTextNode } from './render'
const EMPTY_ARR = []
const EMPTY_OBJ = {}
const isArray = Array.isArray
// The functions `virtualNode` and `virtualTextNode` are based on Superfine's
// https://github.com/jorgebucaran/superfine/blob/main/index.js
// NOTES:
// 1. These checks allow passing a node directly, outside of an array
// Before h('div', [ <node> ])
// After h('div', <node>)
// 2. The _node property is nessecary to have an optional props object
// Before h('div', {}, [ <children> ])
// After h('div', [ <children> ])
const virtualNode = (type, props, ch) => ({
type,
props,
children: isArray(ch) ? ch : ch == null ? EMPTY_ARR : [ch], // 1
dom: null,
key: props.key,
_node: true // 2
})
const virtualTextNode = (value, dom) => ({
type: value,
props: EMPTY_OBJ,
children: EMPTY_ARR,
dom,
key: null,
tag: 3,
_node: true // 2
})
// NOTE: renderNode returns an HTML string while virtualNode returns virtual
// nodes. Likewise for renderTextNode and virtualTextNode.
const node = STATIC ? renderNode : virtualNode
const text = STATIC ? renderTextNode : virtualTextNode
// Syntax variations
// h('div')
// h('div', { <props> })
// h('div', { <props> }, [ <children> ])
// h('div', [ <children> ])
const h = a => (b, c) => {
const cond = STATIC
? typeof b === 'string'
: b._node === true
return b == null || isArray(b) || cond
? node(a, EMPTY_OBJ, b)
: node(a, b, c)
}
export { h, text }