-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom-to-object.js
30 lines (26 loc) · 916 Bytes
/
dom-to-object.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
export default function(tree) {
const parse = {
'null' : () => null,
'number' : node => Number(node.textContent),
'boolean' : node => node.textContent === String(Boolean(node.textContent)),
'string' : node => String(node.textContent),
'array' : node => [...node.children].map(tag => consume(tag)),
'key' : node => String(node.textContent),
'value' : node => consume(node.children[0]),
'property' : node => [node.children[0].textContent, consume(node.children[1])],
'object' : node => {
const obj = {}
Array.from(node.children).forEach(({children}) => {
const [key, value] = children
obj[key.textContent] = consume(value)
})
return obj
}
}
const consume = node => parse[node.tagName.toLowerCase()](node)
if (arguments.length > 0 && tree.tagName) {
return consume(tree)
} else {
return []
}
}