corebasic 1.0.225 → 1.0.226

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.
Files changed (2) hide show
  1. package/dist/libs/utils.js +102 -44
  2. package/package.json +1 -1
@@ -228,59 +228,117 @@ export function validityToUTCMillisecs(start, validity) {
228
228
  export async function fileToJson(path, file) {
229
229
  let data = (await readFile(new URL(file, path).toString().replace('file://', ''), 'utf8'));
230
230
  // return JSON.parse((JSON as Record<string, any>).minify(data))
231
- const json_minify = function (json) {
232
- var tokenizer = /"|(\/\*)|(\*\/)|(\/\/)|\n|\r|\[|]/g, in_string = false, in_multiline_comment = false, in_singleline_comment = false, tmp, tmp2, new_str = [], ns = 0, from = 0, lc, rc, prevFrom;
233
- tokenizer.lastIndex = 0;
234
- while (tmp = tokenizer.exec(json)) {
235
- lc = RegExp.leftContext;
236
- rc = RegExp.rightContext;
237
- if (!in_multiline_comment && !in_singleline_comment) {
238
- tmp2 = lc.substring(from);
239
- if (!in_string) {
240
- tmp2 = tmp2.replace(/(\n|\r|\s)*/g, "");
231
+ // import stripJsonComments from "strip-json-comments";
232
+ // ====================================================
233
+ const singleComment = Symbol('singleComment');
234
+ const multiComment = Symbol('multiComment');
235
+ const stripWithoutWhitespace = () => '';
236
+ // Replace all characters except ASCII spaces, tabs and line endings with regular spaces to ensure valid JSON output.
237
+ const stripWithWhitespace = (string, start, end) => string.slice(start, end).replace(/[^ \t\r\n]/g, ' ');
238
+ const isEscaped = (jsonString, quotePosition) => {
239
+ let index = quotePosition - 1;
240
+ let backslashCount = 0;
241
+ while (jsonString[index] === '\\') {
242
+ index -= 1;
243
+ backslashCount += 1;
244
+ }
245
+ return Boolean(backslashCount % 2);
246
+ };
247
+ function stripJsonComments(jsonString, { whitespace = true, trailingCommas = false } = {}) {
248
+ if (typeof jsonString !== 'string') {
249
+ throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof jsonString}\``);
250
+ }
251
+ const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;
252
+ let isInsideString = false;
253
+ let isInsideComment = false;
254
+ // let isInsideComment = false;
255
+ let offset = 0;
256
+ let buffer = '';
257
+ let result = '';
258
+ let commaIndex = -1;
259
+ for (let index = 0; index < jsonString.length; index++) {
260
+ const currentCharacter = jsonString[index];
261
+ const nextCharacter = jsonString[index + 1];
262
+ if (!isInsideComment && currentCharacter === '"') {
263
+ // Enter or exit string
264
+ const escaped = isEscaped(jsonString, index);
265
+ if (!escaped) {
266
+ isInsideString = !isInsideString;
241
267
  }
242
- new_str[ns++] = tmp2;
243
268
  }
244
- prevFrom = from;
245
- from = tokenizer.lastIndex;
246
- // found a " character, and we're not currently in
247
- // a comment? check for previous `\` escaping immediately
248
- // leftward adjacent to this match
249
- if (tmp[0] === "\"" && !in_multiline_comment && !in_singleline_comment) {
250
- // limit left-context matching to only go back
251
- // to the position of the last token match
252
- //
253
- // see: https://github.com/getify/JSON.minify/issues/64
254
- lc.lastIndex = prevFrom;
255
- // perform leftward adjacent escaping match
256
- tmp2 = lc.match(/(\\)*$/);
257
- // start of string with ", or unescaped " character found to end string?
258
- if (!in_string || !tmp2 || (tmp2[0].length % 2) === 0) {
259
- in_string = !in_string;
260
- }
261
- from--; // include " character in next catch
262
- rc = json.substring(from);
269
+ if (isInsideString) {
270
+ continue;
263
271
  }
264
- else if (tmp[0] === "/*" && !in_string && !in_multiline_comment && !in_singleline_comment) {
265
- in_multiline_comment = true;
272
+ if (!isInsideComment && currentCharacter + nextCharacter === '//') {
273
+ // Enter single-line comment
274
+ buffer += jsonString.slice(offset, index);
275
+ offset = index;
276
+ isInsideComment = singleComment;
277
+ index++;
266
278
  }
267
- else if (tmp[0] === "*/" && !in_string && in_multiline_comment && !in_singleline_comment) {
268
- in_multiline_comment = false;
279
+ else if (isInsideComment === singleComment && currentCharacter + nextCharacter === '\r\n') {
280
+ // Exit single-line comment via \r\n
281
+ index++;
282
+ isInsideComment = false;
283
+ buffer += strip(jsonString, offset, index);
284
+ offset = index;
285
+ continue;
269
286
  }
270
- else if (tmp[0] === "//" && !in_string && !in_multiline_comment && !in_singleline_comment) {
271
- in_singleline_comment = true;
287
+ else if (isInsideComment === singleComment && currentCharacter === '\n') {
288
+ // Exit single-line comment via \n
289
+ isInsideComment = false;
290
+ buffer += strip(jsonString, offset, index);
291
+ offset = index;
272
292
  }
273
- else if ((tmp[0] === "\n" || tmp[0] === "\r") && !in_string && !in_multiline_comment && in_singleline_comment) {
274
- in_singleline_comment = false;
293
+ else if (!isInsideComment && currentCharacter + nextCharacter === '/*') {
294
+ // Enter multiline comment
295
+ buffer += jsonString.slice(offset, index);
296
+ offset = index;
297
+ isInsideComment = multiComment;
298
+ index++;
299
+ continue;
275
300
  }
276
- else if (!in_multiline_comment && !in_singleline_comment && !(/\n|\r|\s/.test(tmp[0]))) {
277
- new_str[ns++] = tmp[0];
301
+ else if (isInsideComment === multiComment && currentCharacter + nextCharacter === '*/') {
302
+ // Exit multiline comment
303
+ index++;
304
+ isInsideComment = false;
305
+ buffer += strip(jsonString, offset, index + 1);
306
+ offset = index + 1;
307
+ continue;
308
+ }
309
+ else if (trailingCommas && !isInsideComment) {
310
+ if (commaIndex !== -1) {
311
+ if (currentCharacter === '}' || currentCharacter === ']') {
312
+ // Strip trailing comma
313
+ buffer += jsonString.slice(offset, index);
314
+ result += strip(buffer, 0, 1) + buffer.slice(1);
315
+ buffer = '';
316
+ offset = index;
317
+ commaIndex = -1;
318
+ }
319
+ else if (currentCharacter !== ' ' && currentCharacter !== '\t' && currentCharacter !== '\r' && currentCharacter !== '\n') {
320
+ // Hit non-whitespace following a comma; comma is not trailing
321
+ buffer += jsonString.slice(offset, index);
322
+ offset = index;
323
+ commaIndex = -1;
324
+ }
325
+ }
326
+ else if (currentCharacter === ',') {
327
+ // Flush buffer prior to this point, and save new comma index
328
+ result += buffer + jsonString.slice(offset, index);
329
+ buffer = '';
330
+ offset = index;
331
+ commaIndex = index;
332
+ }
278
333
  }
279
334
  }
280
- new_str[ns++] = rc;
281
- return new_str.join("");
282
- };
283
- return JSON.parse(json_minify(data));
335
+ const remaining = (isInsideComment === singleComment)
336
+ ? strip(jsonString, offset)
337
+ : jsonString.slice(offset);
338
+ return result + buffer + remaining;
339
+ }
340
+ // ====================================================
341
+ return JSON.parse(stripJsonComments(data));
284
342
  }
285
343
  // ----------------
286
344
  // File
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.225",
4
+ "version": "1.0.226",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "./index.ts",