-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtemplate-strings.ts
53 lines (44 loc) · 1.27 KB
/
template-strings.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
46
47
48
49
50
51
52
53
export var templateStrings = '123';
module m1 {
var lyrics = "Never gonna give you up \
\nNever gonna let you down";
console.log(lyrics);
}
module m2 {
var lyrics = `Never gonna give you up
Never gonna let you down`;
console.log(lyrics);
}
module m3 {
var lyrics = 'Never gonna give you up';
var html = '<div>' + lyrics + '</div>';
}
module m4 {
var lyrics = 'Never gonna give you up';
var html = `<div>${lyrics}</div>`;
}
module m5 {
console.log(`1 and 1 one make ${1 + 1}`);
}
module m6 {
var say = "a bird in hand > two in the bush";
var html = htmlEscape `<div> I would just like to say : ${say}</div>`
// a sample tag function
function htmlEscape(literals, ...placeholders) {
let result = "";
// Interleave the literals with the placeholders
for (let i = 0; i < placeholders.length; i++) {
result += literals[i];
result += placeholders[i]
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>');
}
// add the last literal
result += literals[literals.length - 1];
return result;
}
console.log(html);
}