-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathh.js
26 lines (24 loc) · 859 Bytes
/
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
/**
* Hyperscript function. Enables definition of HTML/SVG using functions.
* @param {string} type The name of the HTML or SVG tag to create.
* @param {object} props And object of property/value pairs.
* @param {string, number, boolean, any[]} children Any child elements.
*/
export function h(type, props, ...args) {
let node
const children = []
// Go thru arguments from front.
while (args.length) {
// If child is array, process.
if (Array.isArray((node = args.shift()))) {
node.map(item => args.push(item))
// Else check if child is string or number.
} else if (node != null && typeof node !== 'boolean') {
typeof node === "number" ? node = node + '' : node
children.push(node)
}
}
return typeof type === "string"
? {type, props: props || {}, children }
: type(props || {}, children)
}