ghtml 3.1.0 → 3.1.1
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 +3 -9
- package/package.json +6 -6
- package/src/index.js +9 -13
- package/test/index.js +6 -16
package/bench/index.js
CHANGED
|
@@ -29,18 +29,14 @@ bench.add("multiple string expressions", () => {
|
|
|
29
29
|
const items1 = ["Item 1", undefined, "Item 2", null, 2000, 1500.5];
|
|
30
30
|
bench.add("array expressions", () => {
|
|
31
31
|
result = html`<ul>
|
|
32
|
-
${items1.map((item) => {
|
|
33
|
-
return html`<li>${item}</li>`;
|
|
34
|
-
})}
|
|
32
|
+
${items1.map((item) => html`<li>${item}</li>`)}
|
|
35
33
|
</ul>`;
|
|
36
34
|
});
|
|
37
35
|
|
|
38
36
|
const items2 = ["Item 1", "Item <1.5>", "Item 2", "Item <2.5>", "Item 3"];
|
|
39
37
|
bench.add("array expressions with escapable chars", () => {
|
|
40
38
|
result = html`<ul>
|
|
41
|
-
${items2.map((item) => {
|
|
42
|
-
return html`<li>"${item}" & '${item}'</li>`;
|
|
43
|
-
})}
|
|
39
|
+
${items2.map((item) => html`<li>"${item}" & '${item}'</li>`)}
|
|
44
40
|
</ul>`;
|
|
45
41
|
});
|
|
46
42
|
|
|
@@ -59,9 +55,7 @@ bench.add("multiple types of expressions", () => {
|
|
|
59
55
|
<div>Id: <span>${user.id}</span></div>
|
|
60
56
|
${null}${123}${456n}
|
|
61
57
|
<ul>
|
|
62
|
-
!${items2.map((item) => {
|
|
63
|
-
return html`<li>${item}</li>`;
|
|
64
|
-
})}
|
|
58
|
+
!${items2.map((item) => html`<li>${item}</li>`)}
|
|
65
59
|
</ul>
|
|
66
60
|
`;
|
|
67
61
|
});
|
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.1",
|
|
7
7
|
"type": "commonjs",
|
|
8
8
|
"bin": "./bin/src/index.js",
|
|
9
9
|
"main": "./src/index.js",
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"glob": "^10.4.5"
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
|
-
"@fastify/pre-commit": "^2.
|
|
28
|
+
"@fastify/pre-commit": "^2.2.0",
|
|
29
29
|
"c8": "^10.1.2",
|
|
30
|
-
"globals": "^15.
|
|
31
|
-
"grules": "^0.
|
|
32
|
-
"tinybench": "^
|
|
33
|
-
"typescript": ">=5.
|
|
30
|
+
"globals": "^15.13.0",
|
|
31
|
+
"grules": "^0.26.1",
|
|
32
|
+
"tinybench": "^3.0.7",
|
|
33
|
+
"typescript": ">=5.7.2"
|
|
34
34
|
},
|
|
35
35
|
"repository": {
|
|
36
36
|
"type": "git",
|
package/src/index.js
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
const _isArray = Array.isArray;
|
|
4
|
-
|
|
5
|
-
const _iterator = Symbol.iterator;
|
|
6
|
-
const _asyncIterator = Symbol.asyncIterator;
|
|
7
|
-
|
|
8
3
|
const escapeRegExp = /["&'<=>]/g;
|
|
9
4
|
|
|
10
5
|
const escapeFunction = (string) => {
|
|
@@ -60,10 +55,11 @@ const escapeFunction = (string) => {
|
|
|
60
55
|
*/
|
|
61
56
|
const html = (literals, ...expressions) => {
|
|
62
57
|
let accumulator = "";
|
|
58
|
+
let i = 0;
|
|
63
59
|
|
|
64
|
-
for (
|
|
60
|
+
for (; i !== expressions.length; ++i) {
|
|
65
61
|
let literal = literals.raw[i];
|
|
66
|
-
let string =
|
|
62
|
+
let string = Array.isArray(expressions[i])
|
|
67
63
|
? expressions[i].join("")
|
|
68
64
|
: `${expressions[i] ?? ""}`;
|
|
69
65
|
|
|
@@ -96,7 +92,7 @@ const htmlGenerator = function* (literals, ...expressions) {
|
|
|
96
92
|
} else if (expression === undefined || expression === null) {
|
|
97
93
|
string = "";
|
|
98
94
|
} else {
|
|
99
|
-
if (typeof expression[
|
|
95
|
+
if (typeof expression[Symbol.iterator] === "function") {
|
|
100
96
|
const isRaw =
|
|
101
97
|
literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33;
|
|
102
98
|
|
|
@@ -116,7 +112,7 @@ const htmlGenerator = function* (literals, ...expressions) {
|
|
|
116
112
|
continue;
|
|
117
113
|
}
|
|
118
114
|
|
|
119
|
-
if (typeof expression[
|
|
115
|
+
if (typeof expression[Symbol.iterator] === "function") {
|
|
120
116
|
for (expression of expression) {
|
|
121
117
|
if (expression === undefined || expression === null) {
|
|
122
118
|
continue;
|
|
@@ -188,8 +184,8 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
|
|
|
188
184
|
string = "";
|
|
189
185
|
} else {
|
|
190
186
|
if (
|
|
191
|
-
typeof expression[
|
|
192
|
-
typeof expression[
|
|
187
|
+
typeof expression[Symbol.iterator] === "function" ||
|
|
188
|
+
typeof expression[Symbol.asyncIterator] === "function"
|
|
193
189
|
) {
|
|
194
190
|
const isRaw =
|
|
195
191
|
literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33;
|
|
@@ -211,8 +207,8 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
|
|
|
211
207
|
}
|
|
212
208
|
|
|
213
209
|
if (
|
|
214
|
-
typeof expression[
|
|
215
|
-
typeof expression[
|
|
210
|
+
typeof expression[Symbol.iterator] === "function" ||
|
|
211
|
+
typeof expression[Symbol.asyncIterator] === "function"
|
|
216
212
|
) {
|
|
217
213
|
for await (expression of expression) {
|
|
218
214
|
if (expression === undefined || expression === null) {
|
package/test/index.js
CHANGED
|
@@ -139,9 +139,7 @@ test("renders multiple html calls", () => {
|
|
|
139
139
|
|
|
140
140
|
test("renders multiple html calls with different expression types", () => {
|
|
141
141
|
const obj = {};
|
|
142
|
-
obj.toString = () =>
|
|
143
|
-
return "description of the object";
|
|
144
|
-
};
|
|
142
|
+
obj.toString = () => "description of the object";
|
|
145
143
|
|
|
146
144
|
// prettier-ignore
|
|
147
145
|
assert.strictEqual(
|
|
@@ -151,7 +149,7 @@ test("renders multiple html calls with different expression types", () => {
|
|
|
151
149
|
!${conditionFalse
|
|
152
150
|
? ""
|
|
153
151
|
:
|
|
154
|
-
html`<em> ${array1.map((i) =>
|
|
152
|
+
html`<em> ${array1.map((i) => i + 1)} </em>`}<br />
|
|
155
153
|
And also, ${false} ${null}${undefined}${obj} is ${true}
|
|
156
154
|
</p>
|
|
157
155
|
`,
|
|
@@ -194,9 +192,7 @@ test("htmlGenerator renders unsafe content", () => {
|
|
|
194
192
|
});
|
|
195
193
|
|
|
196
194
|
test("htmlGenerator works with nested htmlGenerator calls in an array", () => {
|
|
197
|
-
const generator = htmlGenerator`<ul>!${[1, 2, 3].map((index) => {
|
|
198
|
-
return htmlGenerator`<li>${index}</li>`;
|
|
199
|
-
})}</ul>`;
|
|
195
|
+
const generator = htmlGenerator`<ul>!${[1, 2, 3].map((index) => htmlGenerator`<li>${index}</li>`)}</ul>`;
|
|
200
196
|
let accumulator = "";
|
|
201
197
|
|
|
202
198
|
for (const value of generator) {
|
|
@@ -347,9 +343,7 @@ test("htmlAsyncGenerator works with other generators (escaped)", async () => {
|
|
|
347
343
|
});
|
|
348
344
|
|
|
349
345
|
test("htmlAsyncGenerator works with nested htmlAsyncGenerator calls in an array", async () => {
|
|
350
|
-
const generator = htmlAsyncGenerator`!${[1, 2, 3].map((i) => {
|
|
351
|
-
return htmlAsyncGenerator`${i}: <p>${readFile("test/test.md", "utf8")}</p>`;
|
|
352
|
-
})}`;
|
|
346
|
+
const generator = htmlAsyncGenerator`!${[1, 2, 3].map((i) => htmlAsyncGenerator`${i}: <p>${readFile("test/test.md", "utf8")}</p>`)}`;
|
|
353
347
|
let accumulator = "";
|
|
354
348
|
|
|
355
349
|
for await (const value of generator) {
|
|
@@ -363,9 +357,7 @@ test("htmlAsyncGenerator works with nested htmlAsyncGenerator calls in an array"
|
|
|
363
357
|
});
|
|
364
358
|
|
|
365
359
|
test("htmlAsyncGenerator renders chunks with promises (escaped)", async () => {
|
|
366
|
-
const generator = htmlAsyncGenerator`<ul>!${[1, 2].map((i) => {
|
|
367
|
-
return htmlAsyncGenerator`${i}: ${readFile("test/test.md", "utf8")}`;
|
|
368
|
-
})}</ul>`;
|
|
360
|
+
const generator = htmlAsyncGenerator`<ul>!${[1, 2].map((i) => htmlAsyncGenerator`${i}: ${readFile("test/test.md", "utf8")}`)}</ul>`;
|
|
369
361
|
const fileContent = readFileSync("test/test.md", "utf8").replaceAll(
|
|
370
362
|
">",
|
|
371
363
|
">",
|
|
@@ -394,9 +386,7 @@ test("htmlAsyncGenerator renders chunks with promises (escaped)", async () => {
|
|
|
394
386
|
});
|
|
395
387
|
|
|
396
388
|
test("htmlAsyncGenerator renders chunks with promises (raw)", async () => {
|
|
397
|
-
const generator = htmlAsyncGenerator`<ul>!${[1, 2].map((i) => {
|
|
398
|
-
return htmlAsyncGenerator`${i}: !${readFile("test/test.md", "utf8")}`;
|
|
399
|
-
})}</ul>`;
|
|
389
|
+
const generator = htmlAsyncGenerator`<ul>!${[1, 2].map((i) => htmlAsyncGenerator`${i}: !${readFile("test/test.md", "utf8")}`)}</ul>`;
|
|
400
390
|
const fileContent = readFileSync("test/test.md", "utf8");
|
|
401
391
|
|
|
402
392
|
let value = await generator.next();
|