-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.js
34 lines (29 loc) · 880 Bytes
/
index.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
const elementFormatter = (tag, parents = []) => function elementFunc(...texts) {
let result = `<${tag}>${texts.join('')}</${tag}>`;
parents.reverse().forEach((parent) => {
result = `<${parent}>${result}</${parent}>`;
});
return result;
};
function CreateFormatter(parents = []) {
const Format = {
tags: ['div', 'h1', 'p', 'span'],
};
Format.tags.forEach((tag) => {
const formatter = elementFormatter(tag, parents);
formatter.toString = formatter;
formatter.valueOf = formatter;
Format[tag] = formatter;
Format.tags.forEach((otherTag) => {
Object.defineProperty(formatter, otherTag, {
get() {
const nestedFormatter = CreateFormatter([...parents, tag]);
return nestedFormatter[otherTag];
},
});
});
});
return Format;
}
const Format = CreateFormatter();
module.exports = Format;