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 CHANGED
@@ -105,7 +105,6 @@ bench.add("sparse escape", () => {
105
105
  });
106
106
 
107
107
  (async () => {
108
- await bench.warmup();
109
108
  await bench.run();
110
109
 
111
110
  const table = bench.table();
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.2",
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": "^3.0.7",
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
- while (escapeRegExp.test(string)) {
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
- let i = 0;
62
+ const expressionsLength = expressions.length;
59
63
 
60
- for (; i !== expressions.length; ++i) {
64
+ for (let i = 0; i < expressionsLength; ++i) {
61
65
  let literal = literals.raw[i];
62
- let string = Array.isArray(expressions[i])
63
- ? expressions[i].join("")
64
- : `${expressions[i] ?? ""}`;
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[expressions.length];
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
- for (let i = 0; i !== expressions.length; ++i) {
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 === undefined || expression === null) {
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 === undefined || expression === null) {
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 === undefined || expression === null) {
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[expressions.length].length) {
165
- yield literals.raw[expressions.length];
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
- for (let i = 0; i !== expressions.length; ++i) {
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 === undefined || expression === null) {
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 === undefined || expression === null) {
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 === undefined || expression === null) {
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[expressions.length].length) {
262
- yield literals.raw[expressions.length];
274
+ if (literals.raw[expressionsLength].length) {
275
+ yield literals.raw[expressionsLength];
263
276
  }
264
277
  };
265
278