vectocore4 0.0.28 → 0.0.30

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/dist/cjs/index.js CHANGED
@@ -340,46 +340,59 @@ class Lens {
340
340
  });
341
341
  }
342
342
  jsonAutoComplete(jsonString) {
343
- const dataCloseChar = {
344
- "{": "}",
345
- "[": "]",
346
- '"': '"',
347
- };
348
343
  if (!jsonString)
349
344
  return null;
350
- let string = jsonString
351
- .trim()
352
- .replace(/(\r\n|\n|\r|\s{2,})/gm, "")
353
- .replace(/(?<=:)([a-zA-Z]+)(?=\s*(?![,\}])(?:[,\}\s]|$))/g, " null");
354
- let missingChars = [];
355
- for (let i = 0; i < string.length; i++) {
356
- const char = string[i];
357
- if (char === missingChars[missingChars.length - 1]) {
358
- missingChars.pop();
345
+ const stack = [];
346
+ let isInsideString = false;
347
+ let isEscaped = false;
348
+ for (let i = 0; i < jsonString.length; i++) {
349
+ const char = jsonString[i];
350
+ if (isEscaped) {
351
+ isEscaped = false;
352
+ continue;
359
353
  }
360
- else if (dataCloseChar[char]) {
361
- missingChars.push(dataCloseChar[char]);
362
- if (char === "{") {
363
- missingChars.push(":");
354
+ if (char === "\\") {
355
+ isEscaped = true;
356
+ continue;
357
+ }
358
+ if (char === '"') {
359
+ isInsideString = !isInsideString;
360
+ continue;
361
+ }
362
+ if (!isInsideString) {
363
+ if (char === "{")
364
+ stack.push("}");
365
+ else if (char === "[")
366
+ stack.push("]");
367
+ else if (char === "}" || char === "]") {
368
+ if (stack.length > 0 && stack[stack.length - 1] === char) {
369
+ stack.pop();
370
+ }
364
371
  }
365
372
  }
366
373
  }
367
- if (missingChars[missingChars.length - 1] === ":") {
368
- if (string[string.length - 1] !== "{") {
369
- missingChars[missingChars.length - 1] = ": null";
370
- }
371
- else {
372
- missingChars.pop();
374
+ let result = jsonString.trim();
375
+ // 1. 문자열이 열린 채로 끝났다면 따옴표 닫기
376
+ if (isInsideString) {
377
+ result += '"';
378
+ }
379
+ // 2. trailing comma 제거 (중요: ", ]" 또는 ", }" 방지)
380
+ result = result.replace(/,\s*$/, "");
381
+ // 3. 스택을 역순으로 닫기 전에, 속성 이름만 있고 값이 없는 경우 처리
382
+ // 예: "key": 문자로 끝난 경우
383
+ if (result.endsWith(":")) {
384
+ result += "null";
385
+ }
386
+ // 4. 남아있는 모든 괄호 닫기
387
+ for (let i = stack.length - 1; i >= 0; i--) {
388
+ const closingChar = stack[i];
389
+ // 배열 중간에서 끊겼을 때를 위한 보정 (예: [{}, { -> [{}, {}])
390
+ if (closingChar === "]" && result.endsWith(",")) {
391
+ result = result.slice(0, -1);
373
392
  }
393
+ result += closingChar;
374
394
  }
375
- const missingCharsString = missingChars.reverse().join("");
376
- const completeString = string + missingCharsString;
377
- const cleanedString = completeString
378
- .replace(/"":/g, "")
379
- .replace(/":}|": }/g, '": null }')
380
- .replace(/,""}|,}|,\"\w+\"}/g, "}")
381
- .replace(/},]/g, "}]");
382
- return cleanedString;
395
+ return result;
383
396
  }
384
397
  }
385
398
  exports.Lens = Lens;
package/dist/mjs/index.js CHANGED
@@ -301,46 +301,59 @@ export class Lens {
301
301
  }
302
302
  }
303
303
  jsonAutoComplete(jsonString) {
304
- const dataCloseChar = {
305
- "{": "}",
306
- "[": "]",
307
- '"': '"',
308
- };
309
304
  if (!jsonString)
310
305
  return null;
311
- let string = jsonString
312
- .trim()
313
- .replace(/(\r\n|\n|\r|\s{2,})/gm, "")
314
- .replace(/(?<=:)([a-zA-Z]+)(?=\s*(?![,\}])(?:[,\}\s]|$))/g, " null");
315
- let missingChars = [];
316
- for (let i = 0; i < string.length; i++) {
317
- const char = string[i];
318
- if (char === missingChars[missingChars.length - 1]) {
319
- missingChars.pop();
306
+ const stack = [];
307
+ let isInsideString = false;
308
+ let isEscaped = false;
309
+ for (let i = 0; i < jsonString.length; i++) {
310
+ const char = jsonString[i];
311
+ if (isEscaped) {
312
+ isEscaped = false;
313
+ continue;
320
314
  }
321
- else if (dataCloseChar[char]) {
322
- missingChars.push(dataCloseChar[char]);
323
- if (char === "{") {
324
- missingChars.push(":");
315
+ if (char === "\\") {
316
+ isEscaped = true;
317
+ continue;
318
+ }
319
+ if (char === '"') {
320
+ isInsideString = !isInsideString;
321
+ continue;
322
+ }
323
+ if (!isInsideString) {
324
+ if (char === "{")
325
+ stack.push("}");
326
+ else if (char === "[")
327
+ stack.push("]");
328
+ else if (char === "}" || char === "]") {
329
+ if (stack.length > 0 && stack[stack.length - 1] === char) {
330
+ stack.pop();
331
+ }
325
332
  }
326
333
  }
327
334
  }
328
- if (missingChars[missingChars.length - 1] === ":") {
329
- if (string[string.length - 1] !== "{") {
330
- missingChars[missingChars.length - 1] = ": null";
331
- }
332
- else {
333
- missingChars.pop();
335
+ let result = jsonString.trim();
336
+ // 1. 문자열이 열린 채로 끝났다면 따옴표 닫기
337
+ if (isInsideString) {
338
+ result += '"';
339
+ }
340
+ // 2. trailing comma 제거 (중요: ", ]" 또는 ", }" 방지)
341
+ result = result.replace(/,\s*$/, "");
342
+ // 3. 스택을 역순으로 닫기 전에, 속성 이름만 있고 값이 없는 경우 처리
343
+ // 예: "key": 문자로 끝난 경우
344
+ if (result.endsWith(":")) {
345
+ result += "null";
346
+ }
347
+ // 4. 남아있는 모든 괄호 닫기
348
+ for (let i = stack.length - 1; i >= 0; i--) {
349
+ const closingChar = stack[i];
350
+ // 배열 중간에서 끊겼을 때를 위한 보정 (예: [{}, { -> [{}, {}])
351
+ if (closingChar === "]" && result.endsWith(",")) {
352
+ result = result.slice(0, -1);
334
353
  }
354
+ result += closingChar;
335
355
  }
336
- const missingCharsString = missingChars.reverse().join("");
337
- const completeString = string + missingCharsString;
338
- const cleanedString = completeString
339
- .replace(/"":/g, "")
340
- .replace(/":}|": }/g, '": null }')
341
- .replace(/,""}|,}|,\"\w+\"}/g, "}")
342
- .replace(/},]/g, "}]");
343
- return cleanedString;
356
+ return result;
344
357
  }
345
358
  }
346
359
  export class AIMS {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vectocore4",
3
- "version": "0.0.28",
3
+ "version": "0.0.30",
4
4
  "description": "",
5
5
  "types": "dist/index.d.ts",
6
6
  "main": "dist/cjs/index.js",