corebasic 1.0.224 → 1.0.225

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/libs/utils.ts CHANGED
@@ -280,72 +280,126 @@ export async function fileToJson(path, file) {
280
280
  let data = (await readFile(new URL(file, path).toString().replace('file://',''), 'utf8'))
281
281
  // return JSON.parse((JSON as Record<string, any>).minify(data))
282
282
 
283
- const json_minify = function (json: string): string {
284
-
285
- var tokenizer = /"|(\/\*)|(\*\/)|(\/\/)|\n|\r|\[|]/g,
286
- in_string = false,
287
- in_multiline_comment = false,
288
- in_singleline_comment = false,
289
- tmp, tmp2, new_str = [], ns = 0, from = 0, lc, rc,
290
- prevFrom
291
- ;
292
-
293
- tokenizer.lastIndex = 0;
294
-
295
- while ( tmp = tokenizer.exec(json) ) {
296
- lc = RegExp.leftContext;
297
- rc = RegExp.rightContext;
298
- if (!in_multiline_comment && !in_singleline_comment) {
299
- tmp2 = lc.substring(from);
300
- if (!in_string) {
301
- tmp2 = tmp2.replace(/(\n|\r|\s)*/g,"");
302
- }
303
- new_str[ns++] = tmp2;
304
- }
305
- prevFrom = from;
306
- from = tokenizer.lastIndex;
307
-
308
- // found a " character, and we're not currently in
309
- // a comment? check for previous `\` escaping immediately
310
- // leftward adjacent to this match
311
- if (tmp[0] === "\"" && !in_multiline_comment && !in_singleline_comment) {
312
- // limit left-context matching to only go back
313
- // to the position of the last token match
314
- //
315
- // see: https://github.com/getify/JSON.minify/issues/64
316
- lc.lastIndex = prevFrom;
317
-
318
- // perform leftward adjacent escaping match
319
- tmp2 = lc.match(/(\\)*$/);
320
- // start of string with ", or unescaped " character found to end string?
321
- if (!in_string || !tmp2 || (tmp2[0].length % 2) === 0) {
322
- in_string = !in_string;
283
+ // import stripJsonComments from "strip-json-comments";
284
+ // ====================================================
285
+ const singleComment = Symbol('singleComment');
286
+ const multiComment = Symbol('multiComment');
287
+
288
+ const stripWithoutWhitespace = () => '';
289
+
290
+ // Replace all characters except ASCII spaces, tabs and line endings with regular spaces to ensure valid JSON output.
291
+ const stripWithWhitespace = (string, start, end?: number) => string.slice(start, end).replace(/[^ \t\r\n]/g, ' ');
292
+
293
+ const isEscaped = (jsonString, quotePosition) => {
294
+ let index = quotePosition - 1;
295
+ let backslashCount = 0;
296
+
297
+ while (jsonString[index] === '\\') {
298
+ index -= 1;
299
+ backslashCount += 1;
300
+ }
301
+
302
+ return Boolean(backslashCount % 2);
303
+ };
304
+
305
+ function stripJsonComments(jsonString, {whitespace = true, trailingCommas = false} = {}) {
306
+ if (typeof jsonString !== 'string') {
307
+ throw new TypeError(`Expected argument \`jsonString\` to be a \`string\`, got \`${typeof jsonString}\``);
308
+ }
309
+
310
+ const strip = whitespace ? stripWithWhitespace : stripWithoutWhitespace;
311
+
312
+ let isInsideString = false;
313
+ let isInsideComment: false | typeof singleComment | typeof multiComment = false;
314
+ // let isInsideComment = false;
315
+ let offset = 0;
316
+ let buffer = '';
317
+ let result = '';
318
+ let commaIndex = -1;
319
+
320
+ for (let index = 0; index < jsonString.length; index++) {
321
+ const currentCharacter = jsonString[index];
322
+ const nextCharacter = jsonString[index + 1];
323
+
324
+ if (!isInsideComment && currentCharacter === '"') {
325
+ // Enter or exit string
326
+ const escaped = isEscaped(jsonString, index);
327
+ if (!escaped) {
328
+ isInsideString = !isInsideString;
323
329
  }
324
- from--; // include " character in next catch
325
- rc = json.substring(from);
326
- }
327
- else if (tmp[0] === "/*" && !in_string && !in_multiline_comment && !in_singleline_comment) {
328
- in_multiline_comment = true;
329
- }
330
- else if (tmp[0] === "*/" && !in_string && in_multiline_comment && !in_singleline_comment) {
331
- in_multiline_comment = false;
332
330
  }
333
- else if (tmp[0] === "//" && !in_string && !in_multiline_comment && !in_singleline_comment) {
334
- in_singleline_comment = true;
335
- }
336
- else if ((tmp[0] === "\n" || tmp[0] === "\r") && !in_string && !in_multiline_comment && in_singleline_comment) {
337
- in_singleline_comment = false;
331
+
332
+ if (isInsideString) {
333
+ continue;
338
334
  }
339
- else if (!in_multiline_comment && !in_singleline_comment && !(/\n|\r|\s/.test(tmp[0]))) {
340
- new_str[ns++] = tmp[0];
335
+
336
+ if (!isInsideComment && currentCharacter + nextCharacter === '//') {
337
+ // Enter single-line comment
338
+ buffer += jsonString.slice(offset, index);
339
+ offset = index;
340
+ isInsideComment = singleComment;
341
+ index++;
342
+ } else if (isInsideComment === singleComment && currentCharacter + nextCharacter === '\r\n') {
343
+ // Exit single-line comment via \r\n
344
+ index++;
345
+ isInsideComment = false;
346
+ buffer += strip(jsonString, offset, index);
347
+ offset = index;
348
+ continue;
349
+ } else if (isInsideComment === singleComment && currentCharacter === '\n') {
350
+ // Exit single-line comment via \n
351
+ isInsideComment = false;
352
+ buffer += strip(jsonString, offset, index);
353
+ offset = index;
354
+ } else if (!isInsideComment && currentCharacter + nextCharacter === '/*') {
355
+ // Enter multiline comment
356
+ buffer += jsonString.slice(offset, index);
357
+ offset = index;
358
+ isInsideComment = multiComment;
359
+ index++;
360
+ continue;
361
+ } else if (isInsideComment === multiComment && currentCharacter + nextCharacter === '*/') {
362
+ // Exit multiline comment
363
+ index++;
364
+ isInsideComment = false;
365
+ buffer += strip(jsonString, offset, index + 1);
366
+ offset = index + 1;
367
+ continue;
368
+ } else if (trailingCommas && !isInsideComment) {
369
+ if (commaIndex !== -1) {
370
+ if (currentCharacter === '}' || currentCharacter === ']') {
371
+ // Strip trailing comma
372
+ buffer += jsonString.slice(offset, index);
373
+ result += strip(buffer, 0, 1) + buffer.slice(1);
374
+ buffer = '';
375
+ offset = index;
376
+ commaIndex = -1;
377
+ } else if (currentCharacter !== ' ' && currentCharacter !== '\t' && currentCharacter !== '\r' && currentCharacter !== '\n') {
378
+ // Hit non-whitespace following a comma; comma is not trailing
379
+ buffer += jsonString.slice(offset, index);
380
+ offset = index;
381
+ commaIndex = -1;
382
+ }
383
+ } else if (currentCharacter === ',') {
384
+ // Flush buffer prior to this point, and save new comma index
385
+ result += buffer + jsonString.slice(offset, index);
386
+ buffer = '';
387
+ offset = index;
388
+ commaIndex = index;
389
+ }
341
390
  }
342
391
  }
343
- new_str[ns++] = rc;
344
- return new_str.join("");
392
+
393
+ const remaining = (isInsideComment === singleComment)
394
+ ? strip(jsonString, offset)
395
+ : jsonString.slice(offset);
396
+
397
+ return result + buffer + remaining;
345
398
  }
399
+ // ====================================================
346
400
 
347
401
 
348
- return JSON.parse(json_minify(data))
402
+ return JSON.parse(stripJsonComments(data))
349
403
  }
350
404
 
351
405
  // ----------------
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "corebasic",
3
3
  "type": "module",
4
- "version": "1.0.224",
4
+ "version": "1.0.225",
5
5
  "description": "",
6
6
  "main": "dist/index.js",
7
7
  "types": "./index.ts",
package/tsconfig.json CHANGED
@@ -38,7 +38,7 @@
38
38
  // "noPropertyAccessFromIndexSignature": true,
39
39
 
40
40
  // Recommended Options
41
- "strict": true,
41
+ "strict": false,
42
42
  "jsx": "react-jsx",
43
43
  "verbatimModuleSyntax": true,
44
44
  "isolatedModules": true,