-
Notifications
You must be signed in to change notification settings - Fork 14
/
toVdom.js
39 lines (35 loc) · 1 KB
/
toVdom.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
var Vtext = require('virtual-dom/vnode/vtext.js')
var isVdom = require('./isVdom')
var Component = require('./component')
function toVdom (object) {
if (object === undefined || object === null || object === false) {
return new Vtext('')
} else if (typeof (object) !== 'object') {
return new Vtext(String(object))
} else if (object instanceof Date) {
return new Vtext(String(object))
} else if (object instanceof Error) {
return new Vtext(object.toString())
} else if (isVdom(object)) {
return object
} else if (typeof object.render === 'function') {
return new Component(object)
} else {
return new Vtext(JSON.stringify(object))
}
}
module.exports = toVdom
function addChild (children, child) {
if (child instanceof Array) {
for (var n = 0; n < child.length; n++) {
addChild(children, child[n])
}
} else {
children.push(toVdom(child))
}
}
module.exports.recursive = function (child) {
var children = []
addChild(children, child)
return children
}