-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
html.ts
45 lines (37 loc) · 1.02 KB
/
html.ts
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
export const attributes = ( attrs ) => {
const result = Object.entries(attrs)
.map(([key, value]) => {
if( value === '' ) {
return key
}
return `${key}="${value}"`
})
.join(' ')
return result
}
// From
// https://2ality.com/2015/01/template-strings-html.html#comment-2078932192
export const html = (literalSections, ...substs) => {
// Use raw literal sections: we don’t want
// backslashes (\n etc.) to be interpreted
let raw = literalSections.raw
let result = ''
substs.forEach((subst, i) => {
// Retrieve the literal section preceding
// the current substitution
let lit = raw[i]
// In the example, map() returns an array:
// If substitution is an array (and not a string),
// we turn it into a string
if (Array.isArray(subst)) {
subst = subst.join('')
}
result += lit
result += subst
})
// Take care of last literal section
// (Never fails, because an empty template string
// produces one literal section, an empty string)
result += raw[raw.length-1] // (A)
return result
}