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 +112 -58
- package/package.json +1 -1
- package/tsconfig.json +1 -1
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
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
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
|
-
|
|
334
|
-
|
|
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
|
-
|
|
340
|
-
|
|
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
|
-
|
|
344
|
-
|
|
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(
|
|
402
|
+
return JSON.parse(stripJsonComments(data))
|
|
349
403
|
}
|
|
350
404
|
|
|
351
405
|
// ----------------
|
package/package.json
CHANGED