ghtml 3.1.0 → 3.1.2

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 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.0",
6
+ "version": "3.1.2",
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.1.0",
28
+ "@fastify/pre-commit": "^2.2.0",
29
29
  "c8": "^10.1.2",
30
- "globals": "^15.11.0",
31
- "grules": "^0.25.10",
32
- "tinybench": "^2.9.0",
33
- "typescript": ">=5.6.2"
30
+ "globals": "^16.0.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,16 +55,17 @@ const escapeFunction = (string) => {
60
55
  */
61
56
  const html = (literals, ...expressions) => {
62
57
  let accumulator = "";
58
+ let i = 0;
63
59
 
64
- for (let i = 0; i !== expressions.length; ++i) {
60
+ for (; i !== expressions.length; ++i) {
65
61
  let literal = literals.raw[i];
66
- let string = _isArray(expressions[i])
62
+ let string = Array.isArray(expressions[i])
67
63
  ? expressions[i].join("")
68
64
  : `${expressions[i] ?? ""}`;
69
65
 
70
- if (literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33) {
66
+ if (literal.length && literal.charCodeAt(literal.length - 1) === 33) {
71
67
  literal = literal.slice(0, -1);
72
- } else if (string.length !== 0) {
68
+ } else if (string.length) {
73
69
  string = escapeFunction(string);
74
70
  }
75
71
 
@@ -96,15 +92,15 @@ const htmlGenerator = function* (literals, ...expressions) {
96
92
  } else if (expression === undefined || expression === null) {
97
93
  string = "";
98
94
  } else {
99
- if (typeof expression[_iterator] === "function") {
95
+ if (typeof expression[Symbol.iterator] === "function") {
100
96
  const isRaw =
101
- literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33;
97
+ literal.length && literal.charCodeAt(literal.length - 1) === 33;
102
98
 
103
99
  if (isRaw) {
104
100
  literal = literal.slice(0, -1);
105
101
  }
106
102
 
107
- if (literal.length !== 0) {
103
+ if (literal.length) {
108
104
  yield literal;
109
105
  }
110
106
 
@@ -116,7 +112,7 @@ const htmlGenerator = function* (literals, ...expressions) {
116
112
  continue;
117
113
  }
118
114
 
119
- if (typeof expression[_iterator] === "function") {
115
+ if (typeof expression[Symbol.iterator] === "function") {
120
116
  for (expression of expression) {
121
117
  if (expression === undefined || expression === null) {
122
118
  continue;
@@ -124,7 +120,7 @@ const htmlGenerator = function* (literals, ...expressions) {
124
120
 
125
121
  string = `${expression}`;
126
122
 
127
- if (string.length !== 0) {
123
+ if (string.length) {
128
124
  if (!isRaw) {
129
125
  string = escapeFunction(string);
130
126
  }
@@ -139,7 +135,7 @@ const htmlGenerator = function* (literals, ...expressions) {
139
135
  string = `${expression}`;
140
136
  }
141
137
 
142
- if (string.length !== 0) {
138
+ if (string.length) {
143
139
  if (!isRaw) {
144
140
  string = escapeFunction(string);
145
141
  }
@@ -154,18 +150,18 @@ const htmlGenerator = function* (literals, ...expressions) {
154
150
  string = `${expression}`;
155
151
  }
156
152
 
157
- if (literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33) {
153
+ if (literal.length && literal.charCodeAt(literal.length - 1) === 33) {
158
154
  literal = literal.slice(0, -1);
159
- } else if (string.length !== 0) {
155
+ } else if (string.length) {
160
156
  string = escapeFunction(string);
161
157
  }
162
158
 
163
- if (literal.length !== 0 || string.length !== 0) {
159
+ if (literal.length || string.length) {
164
160
  yield literal + string;
165
161
  }
166
162
  }
167
163
 
168
- if (literals.raw[expressions.length].length !== 0) {
164
+ if (literals.raw[expressions.length].length) {
169
165
  yield literals.raw[expressions.length];
170
166
  }
171
167
  };
@@ -188,17 +184,17 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
188
184
  string = "";
189
185
  } else {
190
186
  if (
191
- typeof expression[_iterator] === "function" ||
192
- typeof expression[_asyncIterator] === "function"
187
+ typeof expression[Symbol.iterator] === "function" ||
188
+ typeof expression[Symbol.asyncIterator] === "function"
193
189
  ) {
194
190
  const isRaw =
195
- literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33;
191
+ literal.length && literal.charCodeAt(literal.length - 1) === 33;
196
192
 
197
193
  if (isRaw) {
198
194
  literal = literal.slice(0, -1);
199
195
  }
200
196
 
201
- if (literal.length !== 0) {
197
+ if (literal.length) {
202
198
  yield literal;
203
199
  }
204
200
 
@@ -211,8 +207,8 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
211
207
  }
212
208
 
213
209
  if (
214
- typeof expression[_iterator] === "function" ||
215
- typeof expression[_asyncIterator] === "function"
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) {
@@ -221,7 +217,7 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
221
217
 
222
218
  string = `${expression}`;
223
219
 
224
- if (string.length !== 0) {
220
+ if (string.length) {
225
221
  if (!isRaw) {
226
222
  string = escapeFunction(string);
227
223
  }
@@ -236,7 +232,7 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
236
232
  string = `${expression}`;
237
233
  }
238
234
 
239
- if (string.length !== 0) {
235
+ if (string.length) {
240
236
  if (!isRaw) {
241
237
  string = escapeFunction(string);
242
238
  }
@@ -251,18 +247,18 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
251
247
  string = `${expression}`;
252
248
  }
253
249
 
254
- if (literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33) {
250
+ if (literal.length && literal.charCodeAt(literal.length - 1) === 33) {
255
251
  literal = literal.slice(0, -1);
256
- } else if (string.length !== 0) {
252
+ } else if (string.length) {
257
253
  string = escapeFunction(string);
258
254
  }
259
255
 
260
- if (literal.length !== 0 || string.length !== 0) {
256
+ if (literal.length || string.length) {
261
257
  yield literal + string;
262
258
  }
263
259
  }
264
260
 
265
- if (literals.raw[expressions.length].length !== 0) {
261
+ if (literals.raw[expressions.length].length) {
266
262
  yield literals.raw[expressions.length];
267
263
  }
268
264
  };
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) => {return i + 1;})} </em>`}<br />
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
  "&#62;",
@@ -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();