ghtml 3.0.12 → 3.1.0

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
@@ -1,10 +1,12 @@
1
- /* eslint-disable no-unused-vars */
2
- import { html } from "ghtml";
3
- import { Bench } from "tinybench";
4
- import { writeFileSync } from "node:fs";
5
- import { Buffer } from "node:buffer";
1
+ "use strict";
2
+
3
+ const { html } = require("..");
4
+ const { Bench } = require("tinybench");
5
+ const { writeFileSync } = require("node:fs");
6
6
 
7
7
  const bench = new Bench({ time: 500 });
8
+
9
+ // eslint-disable-next-line no-unused-vars
8
10
  let result = "";
9
11
 
10
12
  bench.add("simple HTML formatting", () => {
@@ -108,13 +110,15 @@ bench.add("sparse escape", () => {
108
110
  result = html`<p>${`${"noescape".repeat(250)}<>`.repeat(5)}</p>`;
109
111
  });
110
112
 
111
- await bench.warmup();
112
- await bench.run();
113
+ (async () => {
114
+ await bench.warmup();
115
+ await bench.run();
113
116
 
114
- const table = bench.table();
115
- globalThis.console.table(table);
117
+ const table = bench.table();
118
+ globalThis.console.table(table);
116
119
 
117
- writeFileSync(
118
- "bench/results.json",
119
- Buffer.from(JSON.stringify(table), "utf8").toString("base64"),
120
- );
120
+ writeFileSync(
121
+ "bench/results.json",
122
+ Buffer.from(JSON.stringify(table), "utf8").toString("base64"),
123
+ );
124
+ })();
package/bin/src/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env node
2
- import { generateHashesAndReplace } from "./utils.js";
3
- import process from "node:process";
2
+
3
+ "use strict";
4
+
5
+ const { generateHashesAndReplace } = require("./utils.js");
4
6
 
5
7
  const parseArguments = (args) => {
6
8
  let roots = null;
package/bin/src/utils.js CHANGED
@@ -1,7 +1,9 @@
1
- import { Glob } from "glob";
2
- import { createHash } from "node:crypto";
3
- import { readFile, writeFile } from "node:fs/promises";
4
- import { win32, posix } from "node:path";
1
+ "use strict";
2
+
3
+ const { Glob } = require("glob");
4
+ const { createHash } = require("node:crypto");
5
+ const { readFile, writeFile } = require("node:fs/promises");
6
+ const { win32, posix } = require("node:path");
5
7
 
6
8
  const generateFileHash = async (filePath) => {
7
9
  try {
@@ -77,7 +79,7 @@ const updateFilePathsWithHashes = async (
77
79
  }
78
80
  };
79
81
 
80
- export const generateHashesAndReplace = async ({
82
+ const generateHashesAndReplace = async ({
81
83
  roots,
82
84
  refs,
83
85
  prefix,
@@ -130,3 +132,5 @@ export const generateHashesAndReplace = async ({
130
132
  skipPatterns,
131
133
  );
132
134
  };
135
+
136
+ module.exports.generateHashesAndReplace = generateHashesAndReplace;
@@ -1,7 +1,15 @@
1
1
  import grules from "grules";
2
+ import globals from "globals";
2
3
 
3
4
  export default [
4
5
  ...grules,
6
+ {
7
+ languageOptions: {
8
+ globals: {
9
+ ...globals.node,
10
+ },
11
+ },
12
+ },
5
13
  {
6
14
  rules: {
7
15
  "no-await-in-loop": "off",
package/package.json CHANGED
@@ -3,8 +3,8 @@
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.0.12",
7
- "type": "module",
6
+ "version": "3.1.0",
7
+ "type": "commonjs",
8
8
  "bin": "./bin/src/index.js",
9
9
  "main": "./src/index.js",
10
10
  "exports": {
@@ -27,6 +27,7 @@
27
27
  "devDependencies": {
28
28
  "@fastify/pre-commit": "^2.1.0",
29
29
  "c8": "^10.1.2",
30
+ "globals": "^15.11.0",
30
31
  "grules": "^0.25.10",
31
32
  "tinybench": "^2.9.0",
32
33
  "typescript": ">=5.6.2"
@@ -1 +1,5 @@
1
+ /**
2
+ * @param {string} path path
3
+ * @returns {string} string
4
+ */
1
5
  export function includeFile(path: string): string;
@@ -1,4 +1,6 @@
1
- import { readFileSync } from "node:fs";
1
+ "use strict";
2
+
3
+ const { readFileSync } = require("node:fs");
2
4
 
3
5
  const cache = new Map();
4
6
 
@@ -6,7 +8,7 @@ const cache = new Map();
6
8
  * @param {string} path path
7
9
  * @returns {string} string
8
10
  */
9
- export const includeFile = (path) => {
11
+ const includeFile = (path) => {
10
12
  let file = cache.get(path);
11
13
 
12
14
  if (file === undefined) {
@@ -16,3 +18,5 @@ export const includeFile = (path) => {
16
18
 
17
19
  return file;
18
20
  };
21
+
22
+ module.exports.includeFile = includeFile;
package/src/index.d.ts CHANGED
@@ -1,3 +1,20 @@
1
+ /**
2
+ * @param {TemplateStringsArray} literals literals
3
+ * @param {...any} expressions expressions
4
+ * @returns {string} string
5
+ */
1
6
  export function html(literals: TemplateStringsArray, ...expressions: any[]): string;
7
+ /**
8
+ * @param {TemplateStringsArray} literals literals
9
+ * @param {...any} expressions expressions
10
+ * @yields {string} string
11
+ * @returns {Generator<string, void, void>} Generator<string, void, void>
12
+ */
2
13
  export function htmlGenerator(literals: TemplateStringsArray, ...expressions: any[]): Generator<string, void, void>;
14
+ /**
15
+ * @param {TemplateStringsArray} literals literals
16
+ * @param {...any} expressions expressions
17
+ * @yields {string} string
18
+ * @returns {AsyncGenerator<string, void, void>} AsyncGenerator<string, void, void>
19
+ */
3
20
  export function htmlAsyncGenerator(literals: TemplateStringsArray, ...expressions: any[]): AsyncGenerator<string, void, void>;
package/src/index.js CHANGED
@@ -1,3 +1,10 @@
1
+ "use strict";
2
+
3
+ const _isArray = Array.isArray;
4
+
5
+ const _iterator = Symbol.iterator;
6
+ const _asyncIterator = Symbol.asyncIterator;
7
+
1
8
  const escapeRegExp = /["&'<=>]/g;
2
9
 
3
10
  const escapeFunction = (string) => {
@@ -51,19 +58,19 @@ const escapeFunction = (string) => {
51
58
  * @param {...any} expressions expressions
52
59
  * @returns {string} string
53
60
  */
54
- export const html = (literals, ...expressions) => {
61
+ const html = (literals, ...expressions) => {
55
62
  let accumulator = "";
56
63
 
57
64
  for (let i = 0; i !== expressions.length; ++i) {
58
65
  let literal = literals.raw[i];
59
- let string = Array.isArray(expressions[i])
66
+ let string = _isArray(expressions[i])
60
67
  ? expressions[i].join("")
61
68
  : `${expressions[i] ?? ""}`;
62
69
 
63
- if (literal && literal.charCodeAt(literal.length - 1) === 33) {
70
+ if (literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33) {
64
71
  literal = literal.slice(0, -1);
65
- } else {
66
- string &&= escapeFunction(string);
72
+ } else if (string.length !== 0) {
73
+ string = escapeFunction(string);
67
74
  }
68
75
 
69
76
  accumulator += literal + string;
@@ -78,7 +85,7 @@ export const html = (literals, ...expressions) => {
78
85
  * @yields {string} string
79
86
  * @returns {Generator<string, void, void>} Generator<string, void, void>
80
87
  */
81
- export const htmlGenerator = function* (literals, ...expressions) {
88
+ const htmlGenerator = function* (literals, ...expressions) {
82
89
  for (let i = 0; i !== expressions.length; ++i) {
83
90
  let expression = expressions[i];
84
91
  let literal = literals.raw[i];
@@ -89,15 +96,15 @@ export const htmlGenerator = function* (literals, ...expressions) {
89
96
  } else if (expression === undefined || expression === null) {
90
97
  string = "";
91
98
  } else {
92
- if (expression[Symbol.iterator]) {
99
+ if (typeof expression[_iterator] === "function") {
93
100
  const isRaw =
94
- Boolean(literal) && literal.charCodeAt(literal.length - 1) === 33;
101
+ literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33;
95
102
 
96
103
  if (isRaw) {
97
104
  literal = literal.slice(0, -1);
98
105
  }
99
106
 
100
- if (literal) {
107
+ if (literal.length !== 0) {
101
108
  yield literal;
102
109
  }
103
110
 
@@ -109,15 +116,15 @@ export const htmlGenerator = function* (literals, ...expressions) {
109
116
  continue;
110
117
  }
111
118
 
112
- if (expression[Symbol.iterator]) {
119
+ if (typeof expression[_iterator] === "function") {
113
120
  for (expression of expression) {
114
121
  if (expression === undefined || expression === null) {
115
122
  continue;
116
123
  }
117
124
 
118
- string = String(expression);
125
+ string = `${expression}`;
119
126
 
120
- if (string) {
127
+ if (string.length !== 0) {
121
128
  if (!isRaw) {
122
129
  string = escapeFunction(string);
123
130
  }
@@ -129,10 +136,10 @@ export const htmlGenerator = function* (literals, ...expressions) {
129
136
  continue;
130
137
  }
131
138
 
132
- string = String(expression);
139
+ string = `${expression}`;
133
140
  }
134
141
 
135
- if (string) {
142
+ if (string.length !== 0) {
136
143
  if (!isRaw) {
137
144
  string = escapeFunction(string);
138
145
  }
@@ -144,21 +151,21 @@ export const htmlGenerator = function* (literals, ...expressions) {
144
151
  continue;
145
152
  }
146
153
 
147
- string = String(expression);
154
+ string = `${expression}`;
148
155
  }
149
156
 
150
- if (literal && literal.charCodeAt(literal.length - 1) === 33) {
157
+ if (literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33) {
151
158
  literal = literal.slice(0, -1);
152
- } else {
153
- string &&= escapeFunction(string);
159
+ } else if (string.length !== 0) {
160
+ string = escapeFunction(string);
154
161
  }
155
162
 
156
- if (literal || string) {
163
+ if (literal.length !== 0 || string.length !== 0) {
157
164
  yield literal + string;
158
165
  }
159
166
  }
160
167
 
161
- if (literals.raw[expressions.length]) {
168
+ if (literals.raw[expressions.length].length !== 0) {
162
169
  yield literals.raw[expressions.length];
163
170
  }
164
171
  };
@@ -169,7 +176,7 @@ export const htmlGenerator = function* (literals, ...expressions) {
169
176
  * @yields {string} string
170
177
  * @returns {AsyncGenerator<string, void, void>} AsyncGenerator<string, void, void>
171
178
  */
172
- export const htmlAsyncGenerator = async function* (literals, ...expressions) {
179
+ const htmlAsyncGenerator = async function* (literals, ...expressions) {
173
180
  for (let i = 0; i !== expressions.length; ++i) {
174
181
  let expression = await expressions[i];
175
182
  let literal = literals.raw[i];
@@ -180,15 +187,18 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
180
187
  } else if (expression === undefined || expression === null) {
181
188
  string = "";
182
189
  } else {
183
- if (expression[Symbol.iterator] || expression[Symbol.asyncIterator]) {
190
+ if (
191
+ typeof expression[_iterator] === "function" ||
192
+ typeof expression[_asyncIterator] === "function"
193
+ ) {
184
194
  const isRaw =
185
- Boolean(literal) && literal.charCodeAt(literal.length - 1) === 33;
195
+ literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33;
186
196
 
187
197
  if (isRaw) {
188
198
  literal = literal.slice(0, -1);
189
199
  }
190
200
 
191
- if (literal) {
201
+ if (literal.length !== 0) {
192
202
  yield literal;
193
203
  }
194
204
 
@@ -201,17 +211,17 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
201
211
  }
202
212
 
203
213
  if (
204
- expression[Symbol.iterator] ||
205
- expression[Symbol.asyncIterator]
214
+ typeof expression[_iterator] === "function" ||
215
+ typeof expression[_asyncIterator] === "function"
206
216
  ) {
207
217
  for await (expression of expression) {
208
218
  if (expression === undefined || expression === null) {
209
219
  continue;
210
220
  }
211
221
 
212
- string = String(expression);
222
+ string = `${expression}`;
213
223
 
214
- if (string) {
224
+ if (string.length !== 0) {
215
225
  if (!isRaw) {
216
226
  string = escapeFunction(string);
217
227
  }
@@ -223,10 +233,10 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
223
233
  continue;
224
234
  }
225
235
 
226
- string = String(expression);
236
+ string = `${expression}`;
227
237
  }
228
238
 
229
- if (string) {
239
+ if (string.length !== 0) {
230
240
  if (!isRaw) {
231
241
  string = escapeFunction(string);
232
242
  }
@@ -238,21 +248,25 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
238
248
  continue;
239
249
  }
240
250
 
241
- string = String(expression);
251
+ string = `${expression}`;
242
252
  }
243
253
 
244
- if (literal && literal.charCodeAt(literal.length - 1) === 33) {
254
+ if (literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33) {
245
255
  literal = literal.slice(0, -1);
246
- } else {
247
- string &&= escapeFunction(string);
256
+ } else if (string.length !== 0) {
257
+ string = escapeFunction(string);
248
258
  }
249
259
 
250
- if (literal || string) {
260
+ if (literal.length !== 0 || string.length !== 0) {
251
261
  yield literal + string;
252
262
  }
253
263
  }
254
264
 
255
- if (literals.raw[expressions.length]) {
265
+ if (literals.raw[expressions.length].length !== 0) {
256
266
  yield literals.raw[expressions.length];
257
267
  }
258
268
  };
269
+
270
+ module.exports.html = html;
271
+ module.exports.htmlGenerator = htmlGenerator;
272
+ module.exports.htmlAsyncGenerator = htmlAsyncGenerator;
package/test/index.js CHANGED
@@ -1,8 +1,10 @@
1
- import { html, htmlGenerator, htmlAsyncGenerator } from "../src/index.js";
2
- import { readFile } from "node:fs/promises";
3
- import { readFileSync } from "node:fs";
4
- import test from "node:test";
5
- import assert from "node:assert";
1
+ "use strict";
2
+
3
+ const { html, htmlGenerator, htmlAsyncGenerator } = require("..");
4
+ const { readFile } = require("node:fs/promises");
5
+ const { readFileSync } = require("node:fs");
6
+ const test = require("node:test");
7
+ const assert = require("node:assert/strict");
6
8
 
7
9
  const conditionTrue = true;
8
10
  const conditionFalse = false;