jsonfixerdev 1.0.4 → 1.0.5

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 (67) hide show
  1. package/lib/cjs/index.js +34 -0
  2. package/lib/cjs/index.js.map +1 -0
  3. package/lib/cjs/package.json +3 -0
  4. package/lib/cjs/regular/jsonfixer.js +612 -0
  5. package/lib/cjs/regular/jsonfixer.js.map +1 -0
  6. package/lib/cjs/stream.js +13 -0
  7. package/lib/cjs/stream.js.map +1 -0
  8. package/lib/cjs/streaming/buffer/InputBuffer.js +75 -0
  9. package/lib/cjs/streaming/buffer/InputBuffer.js.map +1 -0
  10. package/lib/cjs/streaming/buffer/OutputBuffer.js +110 -0
  11. package/lib/cjs/streaming/buffer/OutputBuffer.js.map +1 -0
  12. package/lib/cjs/streaming/core.js +674 -0
  13. package/lib/cjs/streaming/core.js.map +1 -0
  14. package/lib/cjs/streaming/stack.js +51 -0
  15. package/lib/cjs/streaming/stack.js.map +1 -0
  16. package/lib/cjs/streaming/stream.js +37 -0
  17. package/lib/cjs/streaming/stream.js.map +1 -0
  18. package/lib/cjs/utils/JSONFixerError.js +15 -0
  19. package/lib/cjs/utils/JSONFixerError.js.map +1 -0
  20. package/lib/cjs/utils/stringUtils.js +186 -0
  21. package/lib/cjs/utils/stringUtils.js.map +1 -0
  22. package/lib/esm/index.js +16 -0
  23. package/lib/esm/index.js.map +1 -0
  24. package/lib/esm/regular/jsonfixer.js +605 -0
  25. package/lib/esm/regular/jsonfixer.js.map +1 -0
  26. package/lib/esm/stream.js +3 -0
  27. package/lib/esm/stream.js.map +1 -0
  28. package/lib/esm/streaming/buffer/InputBuffer.js +69 -0
  29. package/lib/esm/streaming/buffer/InputBuffer.js.map +1 -0
  30. package/lib/esm/streaming/buffer/OutputBuffer.js +104 -0
  31. package/lib/esm/streaming/buffer/OutputBuffer.js.map +1 -0
  32. package/lib/esm/streaming/core.js +668 -0
  33. package/lib/esm/streaming/core.js.map +1 -0
  34. package/lib/esm/streaming/stack.js +44 -0
  35. package/lib/esm/streaming/stack.js.map +1 -0
  36. package/lib/esm/streaming/stream.js +31 -0
  37. package/lib/esm/streaming/stream.js.map +1 -0
  38. package/lib/esm/utils/JSONFixerError.js +8 -0
  39. package/lib/esm/utils/JSONFixerError.js.map +1 -0
  40. package/lib/esm/utils/stringUtils.js +162 -0
  41. package/lib/esm/utils/stringUtils.js.map +1 -0
  42. package/lib/types/index.d.ts +5 -0
  43. package/lib/types/index.d.ts.map +1 -0
  44. package/lib/types/regular/jsonfixer.d.ts +19 -0
  45. package/lib/types/regular/jsonfixer.d.ts.map +1 -0
  46. package/lib/types/stream.d.ts +2 -0
  47. package/lib/types/stream.d.ts.map +1 -0
  48. package/lib/types/streaming/buffer/InputBuffer.d.ts +14 -0
  49. package/lib/types/streaming/buffer/InputBuffer.d.ts.map +1 -0
  50. package/lib/types/streaming/buffer/OutputBuffer.d.ts +17 -0
  51. package/lib/types/streaming/buffer/OutputBuffer.d.ts.map +1 -0
  52. package/lib/types/streaming/core.d.ts +11 -0
  53. package/lib/types/streaming/core.d.ts.map +1 -0
  54. package/lib/types/streaming/stack.d.ts +20 -0
  55. package/lib/types/streaming/stack.d.ts.map +1 -0
  56. package/lib/types/streaming/stream.d.ts +8 -0
  57. package/lib/types/streaming/stream.d.ts.map +1 -0
  58. package/lib/types/utils/JSONFixerError.d.ts +5 -0
  59. package/lib/types/utils/JSONFixerError.d.ts.map +1 -0
  60. package/lib/types/utils/stringUtils.d.ts +84 -0
  61. package/lib/types/utils/stringUtils.d.ts.map +1 -0
  62. package/lib/umd/jsonfixer.js +763 -0
  63. package/lib/umd/jsonfixer.js.map +1 -0
  64. package/lib/umd/jsonfixer.min.js +3 -0
  65. package/lib/umd/jsonfixer.min.js.map +1 -0
  66. package/lib/umd/package.json +3 -0
  67. package/package.json +4 -3
@@ -0,0 +1,763 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.JSONFixer = {}));
5
+ })(this, (function (exports) { 'use strict';
6
+
7
+ class JSONFixerError extends Error {
8
+ constructor(message, position) {
9
+ super(message);
10
+ this.position = position;
11
+ this.name = 'JSONFixerError';
12
+ }
13
+ }
14
+
15
+ const codeBackslash = 0x5c; // "\"
16
+ const codeSlash = 0x2f; // "/"
17
+ const codeAsterisk = 0x2a; // "*"
18
+ const codeOpeningBrace = 0x7b; // "{"
19
+ const codeClosingBrace = 0x7d; // "}"
20
+ const codeOpeningBracket = 0x5b; // "["
21
+ const codeClosingBracket = 0x5d; // "]"
22
+ const codeOpenParenthesis = 0x28; // "("
23
+ const codeCloseParenthesis = 0x29; // ")"
24
+ const codeSpace = 0x20; // " "
25
+ const codeNewline = 0xa; // "\n"
26
+ const codeTab = 0x9; // "\t"
27
+ const codeReturn = 0xd; // "\r"
28
+ const codeBackspace = 0x08; // "\b"
29
+ const codeFormFeed = 0x0c; // "\f"
30
+ const codeDoubleQuote = 0x0022; // "
31
+ const codePlus = 0x2b; // "+"
32
+ const codeMinus = 0x2d; // "-"
33
+ const codeQuote = 0x27; // "'"
34
+ const codeZero = 0x30; // "0"
35
+ const codeNine = 0x39; // "9"
36
+ const codeComma = 0x2c; // ","
37
+ const codeDot = 0x2e; // "." (dot, period)
38
+ const codeColon = 0x3a; // ":"
39
+ const codeSemicolon = 0x3b; // ";"
40
+ const codeUppercaseA = 0x41; // "A"
41
+ const codeLowercaseA = 0x61; // "a"
42
+ const codeUppercaseE = 0x45; // "E"
43
+ const codeLowercaseE = 0x65; // "e"
44
+ const codeUppercaseF = 0x46; // "F"
45
+ const codeLowercaseF = 0x66; // "f"
46
+ const codeNonBreakingSpace = 0xa0;
47
+ const codeEnQuad = 0x2000;
48
+ const codeHairSpace = 0x200a;
49
+ const codeNarrowNoBreakSpace = 0x202f;
50
+ const codeMediumMathematicalSpace = 0x205f;
51
+ const codeIdeographicSpace = 0x3000;
52
+ const codeDoubleQuoteLeft = 0x201c; // “
53
+ const codeDoubleQuoteRight = 0x201d; // ”
54
+ const codeQuoteLeft = 0x2018; // ‘
55
+ const codeQuoteRight = 0x2019; // ’
56
+ const codeGraveAccent = 0x0060; // `
57
+ const codeAcuteAccent = 0x00b4; // ´
58
+
59
+ function isHex(code) {
60
+ return code >= codeZero && code <= codeNine || code >= codeUppercaseA && code <= codeUppercaseF || code >= codeLowercaseA && code <= codeLowercaseF;
61
+ }
62
+ function isDigit(code) {
63
+ return code >= codeZero && code <= codeNine;
64
+ }
65
+ function isValidStringCharacter(code) {
66
+ return code >= 0x20 && code <= 0x10ffff;
67
+ }
68
+ function isDelimiter(char) {
69
+ return regexDelimiter.test(char) || isQuote(char.charCodeAt(0));
70
+ }
71
+ const regexDelimiter = /^[,:[\]/{}()\n+]$/;
72
+ function isStartOfValue(char) {
73
+ return regexStartOfValue.test(char) || char && isQuote(char.charCodeAt(0));
74
+ }
75
+
76
+ // alpha, number, minus, or opening bracket or brace
77
+ const regexStartOfValue = /^[[{\w-]$/;
78
+ function isControlCharacter(code) {
79
+ return code === codeNewline || code === codeReturn || code === codeTab || code === codeBackspace || code === codeFormFeed;
80
+ }
81
+
82
+ /**
83
+ * Check if the given character is a whitespace character like space, tab, or
84
+ * newline
85
+ */
86
+ function isWhitespace(code) {
87
+ return code === codeSpace || code === codeNewline || code === codeTab || code === codeReturn;
88
+ }
89
+
90
+ /**
91
+ * Check if the given character is a special whitespace character, some
92
+ * unicode variant
93
+ */
94
+ function isSpecialWhitespace(code) {
95
+ return code === codeNonBreakingSpace || code >= codeEnQuad && code <= codeHairSpace || code === codeNarrowNoBreakSpace || code === codeMediumMathematicalSpace || code === codeIdeographicSpace;
96
+ }
97
+
98
+ /**
99
+ * Test whether the given character is a quote or double quote character.
100
+ * Also tests for special variants of quotes.
101
+ */
102
+ function isQuote(code) {
103
+ // the first check double quotes, since that occurs most often
104
+ return isDoubleQuoteLike(code) || isSingleQuoteLike(code);
105
+ }
106
+
107
+ /**
108
+ * Test whether the given character is a double quote character.
109
+ * Also tests for special variants of double quotes.
110
+ */
111
+ function isDoubleQuoteLike(code) {
112
+ // the first check double quotes, since that occurs most often
113
+ return code === codeDoubleQuote || code === codeDoubleQuoteLeft || code === codeDoubleQuoteRight;
114
+ }
115
+
116
+ /**
117
+ * Test whether the given character is a double quote character.
118
+ * Does NOT test for special variants of double quotes.
119
+ */
120
+ function isDoubleQuote(code) {
121
+ return code === codeDoubleQuote;
122
+ }
123
+
124
+ /**
125
+ * Test whether the given character is a single quote character.
126
+ * Also tests for special variants of single quotes.
127
+ */
128
+ function isSingleQuoteLike(code) {
129
+ return code === codeQuote || code === codeQuoteLeft || code === codeQuoteRight || code === codeGraveAccent || code === codeAcuteAccent;
130
+ }
131
+
132
+ /**
133
+ * Test whether the given character is a single quote character.
134
+ * Does NOT test for special variants of single quotes.
135
+ */
136
+ function isSingleQuote(code) {
137
+ return code === codeQuote;
138
+ }
139
+
140
+ /**
141
+ * Strip last occurrence of textToStrip from text
142
+ */
143
+ function stripLastOccurrence(text, textToStrip) {
144
+ let stripRemainingText = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
145
+ const index = text.lastIndexOf(textToStrip);
146
+ return index !== -1 ? text.substring(0, index) + (stripRemainingText ? '' : text.substring(index + 1)) : text;
147
+ }
148
+ function insertBeforeLastWhitespace(text, textToInsert) {
149
+ let index = text.length;
150
+ if (!isWhitespace(text.charCodeAt(index - 1))) {
151
+ // no trailing whitespaces
152
+ return text + textToInsert;
153
+ }
154
+ while (isWhitespace(text.charCodeAt(index - 1))) {
155
+ index--;
156
+ }
157
+ return text.substring(0, index) + textToInsert + text.substring(index);
158
+ }
159
+ function removeAtIndex(text, start, count) {
160
+ return text.substring(0, start) + text.substring(start + count);
161
+ }
162
+
163
+ /**
164
+ * Test whether a string ends with a newline or comma character and optional whitespace
165
+ */
166
+ function endsWithCommaOrNewline(text) {
167
+ return /[,\n][ \t\r]*$/.test(text);
168
+ }
169
+ function nextNonWhiteSpaceCharacter(text, start) {
170
+ let i = start;
171
+ while (isWhitespace(text.charCodeAt(i))) {
172
+ i++;
173
+ }
174
+ return text.charAt(i);
175
+ }
176
+
177
+ const controlCharacters = {
178
+ '\b': '\\b',
179
+ '\f': '\\f',
180
+ '\n': '\\n',
181
+ '\r': '\\r',
182
+ '\t': '\\t'
183
+ };
184
+
185
+ // map with all escape characters
186
+ const escapeCharacters = {
187
+ '"': '"',
188
+ '\\': '\\',
189
+ '/': '/',
190
+ b: '\b',
191
+ f: '\f',
192
+ n: '\n',
193
+ r: '\r',
194
+ t: '\t'
195
+ // note that \u is handled separately in parseString()
196
+ };
197
+
198
+ /**
199
+ * Repair a string containing an invalid JSON document.
200
+ * For example changes JavaScript notation into JSON notation.
201
+ *
202
+ * Example:
203
+ *
204
+ * try {
205
+ * const json = "{name: 'John'}"
206
+ * const repaired = jsonfixer(json)
207
+ * console.log(repaired)
208
+ * // '{"name": "John"}'
209
+ * } catch (err) {
210
+ * console.error(err)
211
+ * }
212
+ *
213
+ */
214
+ function jsonfixer(text) {
215
+ let i = 0; // current index in text
216
+ let output = ''; // generated output
217
+
218
+ const processed = parseValue();
219
+ if (!processed) {
220
+ throwUnexpectedEnd();
221
+ }
222
+ const processedComma = parseCharacter(codeComma);
223
+ if (processedComma) {
224
+ parseWhitespaceAndSkipComments();
225
+ }
226
+ if (isStartOfValue(text[i]) && endsWithCommaOrNewline(output)) {
227
+ // start of a new value after end of the root level object: looks like
228
+ // newline delimited JSON -> turn into a root level array
229
+ if (!processedComma) {
230
+ // repair missing comma
231
+ output = insertBeforeLastWhitespace(output, ',');
232
+ }
233
+ parseNewlineDelimitedJSON();
234
+ } else if (processedComma) {
235
+ // repair: remove trailing comma
236
+ output = stripLastOccurrence(output, ',');
237
+ }
238
+
239
+ // repair redundant end quotes
240
+ while (text.charCodeAt(i) === codeClosingBrace || text.charCodeAt(i) === codeClosingBracket) {
241
+ i++;
242
+ parseWhitespaceAndSkipComments();
243
+ }
244
+ if (i >= text.length) {
245
+ // reached the end of the document properly
246
+ return output;
247
+ }
248
+ throwUnexpectedCharacter();
249
+ function parseValue() {
250
+ parseWhitespaceAndSkipComments();
251
+ const processed = parseObject() || parseArray() || parseString() || parseNumber() || parseKeywords() || parseUnquotedString();
252
+ parseWhitespaceAndSkipComments();
253
+ return processed;
254
+ }
255
+ function parseWhitespaceAndSkipComments() {
256
+ const start = i;
257
+ let changed = parseWhitespace();
258
+ do {
259
+ changed = parseComment();
260
+ if (changed) {
261
+ changed = parseWhitespace();
262
+ }
263
+ } while (changed);
264
+ return i > start;
265
+ }
266
+ function parseWhitespace() {
267
+ let whitespace = '';
268
+ let normal;
269
+ while ((normal = isWhitespace(text.charCodeAt(i))) || isSpecialWhitespace(text.charCodeAt(i))) {
270
+ if (normal) {
271
+ whitespace += text[i];
272
+ } else {
273
+ // repair special whitespace
274
+ whitespace += ' ';
275
+ }
276
+ i++;
277
+ }
278
+ if (whitespace.length > 0) {
279
+ output += whitespace;
280
+ return true;
281
+ }
282
+ return false;
283
+ }
284
+ function parseComment() {
285
+ // find a block comment '/* ... */'
286
+ if (text.charCodeAt(i) === codeSlash && text.charCodeAt(i + 1) === codeAsterisk) {
287
+ // repair block comment by skipping it
288
+ while (i < text.length && !atEndOfBlockComment(text, i)) {
289
+ i++;
290
+ }
291
+ i += 2;
292
+ return true;
293
+ }
294
+
295
+ // find a line comment '// ...'
296
+ if (text.charCodeAt(i) === codeSlash && text.charCodeAt(i + 1) === codeSlash) {
297
+ // repair line comment by skipping it
298
+ while (i < text.length && text.charCodeAt(i) !== codeNewline) {
299
+ i++;
300
+ }
301
+ return true;
302
+ }
303
+ return false;
304
+ }
305
+ function parseCharacter(code) {
306
+ if (text.charCodeAt(i) === code) {
307
+ output += text[i];
308
+ i++;
309
+ return true;
310
+ }
311
+ return false;
312
+ }
313
+ function skipCharacter(code) {
314
+ if (text.charCodeAt(i) === code) {
315
+ i++;
316
+ return true;
317
+ }
318
+ return false;
319
+ }
320
+ function skipEscapeCharacter() {
321
+ return skipCharacter(codeBackslash);
322
+ }
323
+
324
+ /**
325
+ * Parse an object like '{"key": "value"}'
326
+ */
327
+ function parseObject() {
328
+ if (text.charCodeAt(i) === codeOpeningBrace) {
329
+ output += '{';
330
+ i++;
331
+ parseWhitespaceAndSkipComments();
332
+ let initial = true;
333
+ while (i < text.length && text.charCodeAt(i) !== codeClosingBrace) {
334
+ let processedComma;
335
+ if (!initial) {
336
+ processedComma = parseCharacter(codeComma);
337
+ if (!processedComma) {
338
+ // repair missing comma
339
+ output = insertBeforeLastWhitespace(output, ',');
340
+ }
341
+ parseWhitespaceAndSkipComments();
342
+ } else {
343
+ processedComma = true;
344
+ initial = false;
345
+ }
346
+ const processedKey = parseString() || parseUnquotedString();
347
+ if (!processedKey) {
348
+ if (text.charCodeAt(i) === codeClosingBrace || text.charCodeAt(i) === codeOpeningBrace || text.charCodeAt(i) === codeClosingBracket || text.charCodeAt(i) === codeOpeningBracket || text[i] === undefined) {
349
+ // repair trailing comma
350
+ output = stripLastOccurrence(output, ',');
351
+ } else {
352
+ throwObjectKeyExpected();
353
+ }
354
+ break;
355
+ }
356
+ parseWhitespaceAndSkipComments();
357
+ const processedColon = parseCharacter(codeColon);
358
+ const truncatedText = i >= text.length;
359
+ if (!processedColon) {
360
+ if (isStartOfValue(text[i]) || truncatedText) {
361
+ // repair missing colon
362
+ output = insertBeforeLastWhitespace(output, ':');
363
+ } else {
364
+ throwColonExpected();
365
+ }
366
+ }
367
+ const processedValue = parseValue();
368
+ if (!processedValue) {
369
+ if (processedColon || truncatedText) {
370
+ // repair missing object value
371
+ output += 'null';
372
+ } else {
373
+ throwColonExpected();
374
+ }
375
+ }
376
+ }
377
+ if (text.charCodeAt(i) === codeClosingBrace) {
378
+ output += '}';
379
+ i++;
380
+ } else {
381
+ // repair missing end bracket
382
+ output = insertBeforeLastWhitespace(output, '}');
383
+ }
384
+ return true;
385
+ }
386
+ return false;
387
+ }
388
+
389
+ /**
390
+ * Parse an array like '["item1", "item2", ...]'
391
+ */
392
+ function parseArray() {
393
+ if (text.charCodeAt(i) === codeOpeningBracket) {
394
+ output += '[';
395
+ i++;
396
+ parseWhitespaceAndSkipComments();
397
+ let initial = true;
398
+ while (i < text.length && text.charCodeAt(i) !== codeClosingBracket) {
399
+ if (!initial) {
400
+ const processedComma = parseCharacter(codeComma);
401
+ if (!processedComma) {
402
+ // repair missing comma
403
+ output = insertBeforeLastWhitespace(output, ',');
404
+ }
405
+ } else {
406
+ initial = false;
407
+ }
408
+ const processedValue = parseValue();
409
+ if (!processedValue) {
410
+ // repair trailing comma
411
+ output = stripLastOccurrence(output, ',');
412
+ break;
413
+ }
414
+ }
415
+ if (text.charCodeAt(i) === codeClosingBracket) {
416
+ output += ']';
417
+ i++;
418
+ } else {
419
+ // repair missing closing array bracket
420
+ output = insertBeforeLastWhitespace(output, ']');
421
+ }
422
+ return true;
423
+ }
424
+ return false;
425
+ }
426
+
427
+ /**
428
+ * Parse and repair Newline Delimited JSON (NDJSON):
429
+ * multiple JSON objects separated by a newline character
430
+ */
431
+ function parseNewlineDelimitedJSON() {
432
+ // repair NDJSON
433
+ let initial = true;
434
+ let processedValue = true;
435
+ while (processedValue) {
436
+ if (!initial) {
437
+ // parse optional comma, insert when missing
438
+ const processedComma = parseCharacter(codeComma);
439
+ if (!processedComma) {
440
+ // repair: add missing comma
441
+ output = insertBeforeLastWhitespace(output, ',');
442
+ }
443
+ } else {
444
+ initial = false;
445
+ }
446
+ processedValue = parseValue();
447
+ }
448
+ if (!processedValue) {
449
+ // repair: remove trailing comma
450
+ output = stripLastOccurrence(output, ',');
451
+ }
452
+
453
+ // repair: wrap the output inside array brackets
454
+ output = `[\n${output}\n]`;
455
+ }
456
+
457
+ /**
458
+ * Parse a string enclosed by double quotes "...". Can contain escaped quotes
459
+ * Repair strings enclosed in single quotes or special quotes
460
+ * Repair an escaped string
461
+ *
462
+ * The function can run in two stages:
463
+ * - First, it assumes the string has a valid end quote
464
+ * - If it turns out that the string does not have a valid end quote followed
465
+ * by a delimiter (which should be the case), the function runs again in a
466
+ * more conservative way, stopping the string at the first next delimiter
467
+ * and fixing the string by inserting a quote there.
468
+ */
469
+ function parseString() {
470
+ let stopAtDelimiter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
471
+ let skipEscapeChars = text.charCodeAt(i) === codeBackslash;
472
+ if (skipEscapeChars) {
473
+ // repair: remove the first escape character
474
+ i++;
475
+ skipEscapeChars = true;
476
+ }
477
+ if (isQuote(text.charCodeAt(i))) {
478
+ // double quotes are correct JSON,
479
+ // single quotes come from JavaScript for example, we assume it will have a correct single end quote too
480
+ // otherwise, we will match any double-quote-like start with a double-quote-like end,
481
+ // or any single-quote-like start with a single-quote-like end
482
+ const isEndQuote = isDoubleQuote(text.charCodeAt(i)) ? isDoubleQuote : isSingleQuote(text.charCodeAt(i)) ? isSingleQuote : isSingleQuoteLike(text.charCodeAt(i)) ? isSingleQuoteLike : isDoubleQuoteLike;
483
+ const iBefore = i;
484
+ let str = '"';
485
+ i++;
486
+ const isEndOfString = stopAtDelimiter ? i => isDelimiter(text[i]) : i => isEndQuote(text.charCodeAt(i));
487
+ while (i < text.length && !isEndOfString(i)) {
488
+ if (text.charCodeAt(i) === codeBackslash) {
489
+ const char = text.charAt(i + 1);
490
+ const escapeChar = escapeCharacters[char];
491
+ if (escapeChar !== undefined) {
492
+ str += text.slice(i, i + 2);
493
+ i += 2;
494
+ } else if (char === 'u') {
495
+ let j = 2;
496
+ while (j < 6 && isHex(text.charCodeAt(i + j))) {
497
+ j++;
498
+ }
499
+ if (j === 6) {
500
+ str += text.slice(i, i + 6);
501
+ i += 6;
502
+ } else if (i + j >= text.length) {
503
+ // repair invalid or truncated unicode char at the end of the text
504
+ // by removing the unicode char and ending the string here
505
+ i = text.length;
506
+ } else {
507
+ throwInvalidUnicodeCharacter();
508
+ }
509
+ } else {
510
+ // repair invalid escape character: remove it
511
+ str += char;
512
+ i += 2;
513
+ }
514
+ } else {
515
+ const char = text.charAt(i);
516
+ const code = text.charCodeAt(i);
517
+ if (code === codeDoubleQuote && text.charCodeAt(i - 1) !== codeBackslash) {
518
+ // repair unescaped double quote
519
+ str += '\\' + char;
520
+ i++;
521
+ } else if (isControlCharacter(code)) {
522
+ // unescaped control character
523
+ str += controlCharacters[char];
524
+ i++;
525
+ } else {
526
+ if (!isValidStringCharacter(code)) {
527
+ throwInvalidCharacter(char);
528
+ }
529
+ str += char;
530
+ i++;
531
+ }
532
+ }
533
+ if (skipEscapeChars) {
534
+ skipEscapeCharacter();
535
+ }
536
+ }
537
+
538
+ // see whether we have an end quote followed by a valid delimiter
539
+ const hasEndQuote = isQuote(text.charCodeAt(i));
540
+ const valid = hasEndQuote && (i + 1 >= text.length || isDelimiter(nextNonWhiteSpaceCharacter(text, i + 1)));
541
+ if (!valid && !stopAtDelimiter) {
542
+ // we're dealing with a missing quote somewhere. Let's revert parsing
543
+ // this string and try again, running in a more conservative mode,
544
+ // stopping at the first next delimiter
545
+ i = iBefore;
546
+ return parseString(true);
547
+ }
548
+ if (hasEndQuote) {
549
+ str += '"';
550
+ i++;
551
+ } else {
552
+ // repair missing quote
553
+ str = insertBeforeLastWhitespace(str, '"');
554
+ }
555
+ output += str;
556
+ parseConcatenatedString();
557
+ return true;
558
+ }
559
+ return false;
560
+ }
561
+
562
+ /**
563
+ * Repair concatenated strings like "hello" + "world", change this into "helloworld"
564
+ */
565
+ function parseConcatenatedString() {
566
+ let processed = false;
567
+ parseWhitespaceAndSkipComments();
568
+ while (text.charCodeAt(i) === codePlus) {
569
+ processed = true;
570
+ i++;
571
+ parseWhitespaceAndSkipComments();
572
+
573
+ // repair: remove the end quote of the first string
574
+ output = stripLastOccurrence(output, '"', true);
575
+ const start = output.length;
576
+ const parsedStr = parseString();
577
+ if (parsedStr) {
578
+ // repair: remove the start quote of the second string
579
+ output = removeAtIndex(output, start, 1);
580
+ } else {
581
+ // repair: remove the + because it is not followed by a string
582
+ output = insertBeforeLastWhitespace(output, '"');
583
+ }
584
+ }
585
+ return processed;
586
+ }
587
+
588
+ /**
589
+ * Parse a number like 2.4 or 2.4e6
590
+ */
591
+ function parseNumber() {
592
+ const start = i;
593
+ if (text.charCodeAt(i) === codeMinus) {
594
+ i++;
595
+ if (expectDigitOrRepair(start)) {
596
+ return true;
597
+ }
598
+ }
599
+
600
+ // Note that in JSON leading zeros like "00789" are not allowed.
601
+ // We will allow all leading zeros here though and at the end of parseNumber
602
+ // check against trailing zeros and repair that if needed.
603
+ // Leading zeros can have meaning, so we should not clear them.
604
+ while (isDigit(text.charCodeAt(i))) {
605
+ i++;
606
+ }
607
+ if (text.charCodeAt(i) === codeDot) {
608
+ i++;
609
+ if (expectDigitOrRepair(start)) {
610
+ return true;
611
+ }
612
+ while (isDigit(text.charCodeAt(i))) {
613
+ i++;
614
+ }
615
+ }
616
+ if (text.charCodeAt(i) === codeLowercaseE || text.charCodeAt(i) === codeUppercaseE) {
617
+ i++;
618
+ if (text.charCodeAt(i) === codeMinus || text.charCodeAt(i) === codePlus) {
619
+ i++;
620
+ }
621
+ if (expectDigitOrRepair(start)) {
622
+ return true;
623
+ }
624
+ while (isDigit(text.charCodeAt(i))) {
625
+ i++;
626
+ }
627
+ }
628
+ if (i > start) {
629
+ // repair a number with leading zeros like "00789"
630
+ const num = text.slice(start, i);
631
+ const hasInvalidLeadingZero = /^0\d/.test(num);
632
+ output += hasInvalidLeadingZero ? `"${num}"` : num;
633
+ return true;
634
+ }
635
+ return false;
636
+ }
637
+
638
+ /**
639
+ * Parse keywords true, false, null
640
+ * Repair Python keywords True, False, None
641
+ */
642
+ function parseKeywords() {
643
+ return parseKeyword('true', 'true') || parseKeyword('false', 'false') || parseKeyword('null', 'null') ||
644
+ // repair Python keywords True, False, None
645
+ parseKeyword('True', 'true') || parseKeyword('False', 'false') || parseKeyword('None', 'null');
646
+ }
647
+ function parseKeyword(name, value) {
648
+ if (text.slice(i, i + name.length) === name) {
649
+ output += value;
650
+ i += name.length;
651
+ return true;
652
+ }
653
+ return false;
654
+ }
655
+
656
+ /**
657
+ * Repair and unquoted string by adding quotes around it
658
+ * Repair a MongoDB function call like NumberLong("2")
659
+ * Repair a JSONP function call like callback({...});
660
+ */
661
+ function parseUnquotedString() {
662
+ // note that the symbol can end with whitespaces: we stop at the next delimiter
663
+ const start = i;
664
+ while (i < text.length && !isDelimiter(text[i])) {
665
+ i++;
666
+ }
667
+ if (i > start) {
668
+ if (text.charCodeAt(i) === codeOpenParenthesis) {
669
+ // repair a MongoDB function call like NumberLong("2")
670
+ // repair a JSONP function call like callback({...});
671
+ i++;
672
+ parseValue();
673
+ if (text.charCodeAt(i) === codeCloseParenthesis) {
674
+ // repair: skip close bracket of function call
675
+ i++;
676
+ if (text.charCodeAt(i) === codeSemicolon) {
677
+ // repair: skip semicolon after JSONP call
678
+ i++;
679
+ }
680
+ }
681
+ return true;
682
+ } else {
683
+ // repair unquoted string
684
+ // also, repair undefined into null
685
+
686
+ // first, go back to prevent getting trailing whitespaces in the string
687
+ while (isWhitespace(text.charCodeAt(i - 1)) && i > 0) {
688
+ i--;
689
+ }
690
+ const symbol = text.slice(start, i);
691
+ output += symbol === 'undefined' ? 'null' : JSON.stringify(symbol);
692
+ if (text.charCodeAt(i) === codeDoubleQuote) {
693
+ // we had a missing start quote, but now we encountered the end quote, so we can skip that one
694
+ i++;
695
+ }
696
+ return true;
697
+ }
698
+ }
699
+ }
700
+ function expectDigit(start) {
701
+ if (!isDigit(text.charCodeAt(i))) {
702
+ const numSoFar = text.slice(start, i);
703
+ throw new JSONFixerError(`Invalid number '${numSoFar}', expecting a digit ${got()}`, i);
704
+ }
705
+ }
706
+ function expectDigitOrRepair(start) {
707
+ if (i >= text.length) {
708
+ // repair numbers cut off at the end
709
+ // this will only be called when we end after a '.', '-', or 'e' and does not
710
+ // change the number more than it needs to make it valid JSON
711
+ output += text.slice(start, i) + '0';
712
+ return true;
713
+ } else {
714
+ expectDigit(start);
715
+ return false;
716
+ }
717
+ }
718
+ function throwInvalidCharacter(char) {
719
+ throw new JSONFixerError('Invalid character ' + JSON.stringify(char), i);
720
+ }
721
+ function throwUnexpectedCharacter() {
722
+ throw new JSONFixerError('Unexpected character ' + JSON.stringify(text[i]), i);
723
+ }
724
+ function throwUnexpectedEnd() {
725
+ throw new JSONFixerError('Unexpected end of json string', text.length);
726
+ }
727
+ function throwObjectKeyExpected() {
728
+ throw new JSONFixerError('Object key expected', i);
729
+ }
730
+ function throwColonExpected() {
731
+ throw new JSONFixerError('Colon expected', i);
732
+ }
733
+ function throwInvalidUnicodeCharacter() {
734
+ const chars = text.slice(i, i + 6);
735
+ throw new JSONFixerError(`Invalid unicode character "${chars}"`, i);
736
+ }
737
+ function got() {
738
+ return text[i] ? `but got '${text[i]}'` : 'but reached end of input';
739
+ }
740
+ }
741
+ function atEndOfBlockComment(text, i) {
742
+ return text[i] === '*' && text[i + 1] === '/';
743
+ }
744
+
745
+ // Cross-platform, non-streaming JavaScript API
746
+ function getNormalizedString(value) {
747
+ const text = typeof value === 'string' ? value.trim() : '';
748
+ if (!text) {
749
+ return '';
750
+ }
751
+ try {
752
+ return jsonfixer(text);
753
+ } catch {
754
+ return text;
755
+ }
756
+ }
757
+
758
+ exports.JSONFixerError = JSONFixerError;
759
+ exports.getNormalizedString = getNormalizedString;
760
+ exports.jsonfixer = jsonfixer;
761
+
762
+ }));
763
+ //# sourceMappingURL=jsonfixer.js.map