ghtml 3.1.2 → 3.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bench/index.js +0 -1
- package/package.json +2 -2
- package/src/index.js +33 -20
package/bench/index.js
CHANGED
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Replace your template engine with fast JavaScript by leveraging the power of tagged templates.",
|
|
4
4
|
"author": "Gürgün Dayıoğlu",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "3.1.
|
|
6
|
+
"version": "3.1.4",
|
|
7
7
|
"type": "commonjs",
|
|
8
8
|
"bin": "./bin/src/index.js",
|
|
9
9
|
"main": "./src/index.js",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"c8": "^10.1.2",
|
|
30
30
|
"globals": "^16.0.0",
|
|
31
31
|
"grules": "^0.26.1",
|
|
32
|
-
"tinybench": "^
|
|
32
|
+
"tinybench": "^5.0.1",
|
|
33
33
|
"typescript": ">=5.7.2"
|
|
34
34
|
},
|
|
35
35
|
"repository": {
|
package/src/index.js
CHANGED
|
@@ -3,10 +3,14 @@
|
|
|
3
3
|
const escapeRegExp = /["&'<=>]/g;
|
|
4
4
|
|
|
5
5
|
const escapeFunction = (string) => {
|
|
6
|
+
if (!escapeRegExp.test(string)) {
|
|
7
|
+
return string;
|
|
8
|
+
}
|
|
9
|
+
|
|
6
10
|
let escaped = "";
|
|
7
11
|
let start = 0;
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
do {
|
|
10
14
|
const i = escapeRegExp.lastIndex - 1;
|
|
11
15
|
|
|
12
16
|
switch (string.charCodeAt(i)) {
|
|
@@ -43,7 +47,7 @@ const escapeFunction = (string) => {
|
|
|
43
47
|
}
|
|
44
48
|
|
|
45
49
|
start = escapeRegExp.lastIndex;
|
|
46
|
-
}
|
|
50
|
+
} while (escapeRegExp.test(string));
|
|
47
51
|
|
|
48
52
|
return escaped + string.slice(start);
|
|
49
53
|
};
|
|
@@ -55,13 +59,18 @@ const escapeFunction = (string) => {
|
|
|
55
59
|
*/
|
|
56
60
|
const html = (literals, ...expressions) => {
|
|
57
61
|
let accumulator = "";
|
|
58
|
-
|
|
62
|
+
const expressionsLength = expressions.length;
|
|
59
63
|
|
|
60
|
-
for (; i
|
|
64
|
+
for (let i = 0; i < expressionsLength; ++i) {
|
|
61
65
|
let literal = literals.raw[i];
|
|
62
|
-
let string =
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
let string =
|
|
67
|
+
typeof expressions[i] === "string"
|
|
68
|
+
? expressions[i]
|
|
69
|
+
: expressions[i] == null
|
|
70
|
+
? ""
|
|
71
|
+
: Array.isArray(expressions[i])
|
|
72
|
+
? expressions[i].join("")
|
|
73
|
+
: `${expressions[i]}`;
|
|
65
74
|
|
|
66
75
|
if (literal.length && literal.charCodeAt(literal.length - 1) === 33) {
|
|
67
76
|
literal = literal.slice(0, -1);
|
|
@@ -72,7 +81,7 @@ const html = (literals, ...expressions) => {
|
|
|
72
81
|
accumulator += literal + string;
|
|
73
82
|
}
|
|
74
83
|
|
|
75
|
-
return accumulator + literals.raw[
|
|
84
|
+
return accumulator + literals.raw[expressionsLength];
|
|
76
85
|
};
|
|
77
86
|
|
|
78
87
|
/**
|
|
@@ -82,14 +91,16 @@ const html = (literals, ...expressions) => {
|
|
|
82
91
|
* @returns {Generator<string, void, void>} Generator<string, void, void>
|
|
83
92
|
*/
|
|
84
93
|
const htmlGenerator = function* (literals, ...expressions) {
|
|
85
|
-
|
|
94
|
+
const expressionsLength = expressions.length;
|
|
95
|
+
|
|
96
|
+
for (let i = 0; i < expressionsLength; ++i) {
|
|
86
97
|
let expression = expressions[i];
|
|
87
98
|
let literal = literals.raw[i];
|
|
88
99
|
let string;
|
|
89
100
|
|
|
90
101
|
if (typeof expression === "string") {
|
|
91
102
|
string = expression;
|
|
92
|
-
} else if (expression
|
|
103
|
+
} else if (expression == null) {
|
|
93
104
|
string = "";
|
|
94
105
|
} else {
|
|
95
106
|
if (typeof expression[Symbol.iterator] === "function") {
|
|
@@ -108,13 +119,13 @@ const htmlGenerator = function* (literals, ...expressions) {
|
|
|
108
119
|
if (typeof expression === "string") {
|
|
109
120
|
string = expression;
|
|
110
121
|
} else {
|
|
111
|
-
if (expression
|
|
122
|
+
if (expression == null) {
|
|
112
123
|
continue;
|
|
113
124
|
}
|
|
114
125
|
|
|
115
126
|
if (typeof expression[Symbol.iterator] === "function") {
|
|
116
127
|
for (expression of expression) {
|
|
117
|
-
if (expression
|
|
128
|
+
if (expression == null) {
|
|
118
129
|
continue;
|
|
119
130
|
}
|
|
120
131
|
|
|
@@ -161,8 +172,8 @@ const htmlGenerator = function* (literals, ...expressions) {
|
|
|
161
172
|
}
|
|
162
173
|
}
|
|
163
174
|
|
|
164
|
-
if (literals.raw[
|
|
165
|
-
yield literals.raw[
|
|
175
|
+
if (literals.raw[expressionsLength].length) {
|
|
176
|
+
yield literals.raw[expressionsLength];
|
|
166
177
|
}
|
|
167
178
|
};
|
|
168
179
|
|
|
@@ -173,14 +184,16 @@ const htmlGenerator = function* (literals, ...expressions) {
|
|
|
173
184
|
* @returns {AsyncGenerator<string, void, void>} AsyncGenerator<string, void, void>
|
|
174
185
|
*/
|
|
175
186
|
const htmlAsyncGenerator = async function* (literals, ...expressions) {
|
|
176
|
-
|
|
187
|
+
const expressionsLength = expressions.length;
|
|
188
|
+
|
|
189
|
+
for (let i = 0; i < expressionsLength; ++i) {
|
|
177
190
|
let expression = await expressions[i];
|
|
178
191
|
let literal = literals.raw[i];
|
|
179
192
|
let string;
|
|
180
193
|
|
|
181
194
|
if (typeof expression === "string") {
|
|
182
195
|
string = expression;
|
|
183
|
-
} else if (expression
|
|
196
|
+
} else if (expression == null) {
|
|
184
197
|
string = "";
|
|
185
198
|
} else {
|
|
186
199
|
if (
|
|
@@ -202,7 +215,7 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
|
|
|
202
215
|
if (typeof expression === "string") {
|
|
203
216
|
string = expression;
|
|
204
217
|
} else {
|
|
205
|
-
if (expression
|
|
218
|
+
if (expression == null) {
|
|
206
219
|
continue;
|
|
207
220
|
}
|
|
208
221
|
|
|
@@ -211,7 +224,7 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
|
|
|
211
224
|
typeof expression[Symbol.asyncIterator] === "function"
|
|
212
225
|
) {
|
|
213
226
|
for await (expression of expression) {
|
|
214
|
-
if (expression
|
|
227
|
+
if (expression == null) {
|
|
215
228
|
continue;
|
|
216
229
|
}
|
|
217
230
|
|
|
@@ -258,8 +271,8 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
|
|
|
258
271
|
}
|
|
259
272
|
}
|
|
260
273
|
|
|
261
|
-
if (literals.raw[
|
|
262
|
-
yield literals.raw[
|
|
274
|
+
if (literals.raw[expressionsLength].length) {
|
|
275
|
+
yield literals.raw[expressionsLength];
|
|
263
276
|
}
|
|
264
277
|
};
|
|
265
278
|
|