-
Notifications
You must be signed in to change notification settings - Fork 2
/
html.js
42 lines (41 loc) · 1.27 KB
/
html.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
// Tag function for tag templates literals constructing HTML.
//
// Adds a few semantic niceties:
//
// 1. Falsey values in expressions don't produce output.
//
// 2. Array values in expressions get stringified and concatenated.
//
// These make it much more convenient do achieve conditional markup
// using boolean expressions, without control structures.
export default function html (/* strings, values... */) {
const strings = arguments[0]
const values = Array.prototype.slice.call(arguments, 1)
let result = ''
strings.forEach(function (string, index) {
result += string
if (index < values.length) {
result += toString(values[index])
}
})
// Trim so that the newline after the opening backtick and first
// expression loading the header with <!doctype html> ends up on the
// first line.
return result.trim()
}
function toString (value) {
/* istanbul ignore else */
if (value === false || value === undefined || value === null) {
return ''
} else if (Array.isArray(value)) {
return value.join('')
} else if (typeof value === 'string') {
return value
} else if (typeof value === 'number') {
return value.toString()
} else {
throw new Error(
'Invalid template value ' + typeof value + JSON.stringify(value)
)
}
}