-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
61 lines (47 loc) · 1.48 KB
/
bundle.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
61
(function () {
'use strict';
function getArgs(args) {
if (args.length === 1) {
return [ undefined, args[0] ]
}
return [ args[0], args[1] ]
}
function convertToTextNodeIfNodeIsString(possibleString) {
return typeof possibleString === 'string' ?
document.createTextNode(possibleString) :
possibleString;
}
function camelToKebab(camel) {
return camel.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`);
}
function createElement(tag, ...args) {
const [ attributes, children ] = getArgs(args);
const node = document.createElement(tag);
if (attributes) {
for (let attribute in attributes) {
if (attribute === 'className') {
node.classList.add(attributes[attribute]);
} else {
node.setAttribute(camelToKebab(attribute), attributes[attribute]);
}
}
}
if (children) {
const childNodes = Array.isArray(children) ? children : [children];
childNodes.forEach(child => node.appendChild(convertToTextNodeIfNodeIsString(child)));
}
return node
}
const div = createElement.bind(null, 'div');
const p = createElement.bind(null, 'p');
const ul = createElement.bind(null, 'ul');
const li = createElement.bind(null, 'li');
const a = createElement.bind(null, 'a');
const items = ['foo', 'bar', 'baz'];
const container = div({ className: 'hello'},
div({ className: 'ul-container', dataRef: 'hello' }, ul(
items.map(item => li(a({className: 'li-link', href: `${item}`}, item)))
))
);
document.body.appendChild(container);
}());