corebasic 1.0.224 → 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.
@@ -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/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.226",
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,