-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
71 lines (56 loc) · 1.4 KB
/
index.mjs
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
62
63
64
65
66
67
68
69
70
71
/**
*
* @param {string} tagName
* @param {object|string} props
* @param {string[]} kittens
* @returns {string}
*/
export function createElement(tagName, props, kittens) {
if (!kittens || !kittens.length) {
return lonelyKitten(tagName, props);
}
return `<${tagName}${attributes(props)}>${litterOfKittens(kittens)}</${tagName}>`
}
export const toString = () => '<!-- markitten -->';
function lonelyKitten(tagName, props) {
return `<${tagName}${attributes(props)}/>`
}
function litterOfKittens(kittens) {
return Array.isArray(kittens) ? kittens.join('') : String(kittens);
}
function attributes(props) {
if (typeof props === 'string') {
props = extractIdClassName(props);
}
let result = '';
if (props) {
for (let key in props) {
result += ` ${key}="${props[key]}"`;
}
}
return result;
}
function extractIdClassName(idClassName) {
let [id, ...classNames] = idClassName.split('.');
if (id[0] === '#') {
id = id.substring(1);
} else {
classNames = [id, ...classNames];
id = null;
}
const props = {};
if (id) { props.id = id; }
if (classNames.length) { props.class = classNames.join(' '); }
return props;
}
/**
*
* @param {string} tagName
* @returns {function}
*/
export function createFactory(tagName) {
tagName = String(tagName);
const fn = createElement.bind(null, tagName)
fn.toString = lonelyKitten.bind(null, tagName);
return fn;
}