MCPcopy Create free account
hub / github.com/zxlie/FeHelper / extractBalancedJSONContainer

Function extractBalancedJSONContainer

apps/json-format/json-auto-utils.js:344–394  ·  view source on GitHub ↗
(source)

Source from the content-addressed store, hash-verified

342 }
343
344 function extractBalancedJSONContainer(source) {
345 source = String(source || '');
346 const start = source.search(/[\{\[]/);
347 if (start < 0) return '';
348
349 const stack = [];
350 let quote = '';
351 let escaped = false;
352
353 for (let i = start; i < source.length; i++) {
354 const char = source[i];
355
356 if (quote) {
357 if (escaped) {
358 escaped = false;
359 continue;
360 }
361 if (char === '\\') {
362 escaped = true;
363 continue;
364 }
365 if (char === quote) {
366 quote = '';
367 }
368 continue;
369 }
370
371 if (char === '"' || char === "'") {
372 quote = char;
373 continue;
374 }
375
376 if (char === '{' || char === '[') {
377 stack.push(char);
378 continue;
379 }
380
381 if (char === '}' || char === ']') {
382 const expected = char === '}' ? '{' : '[';
383 if (stack[stack.length - 1] !== expected) {
384 return '';
385 }
386 stack.pop();
387 if (!stack.length) {
388 return source.slice(start, i + 1).trim();
389 }
390 }
391 }
392
393 return '';
394 }
395
396 function buildJSONLikeCandidates(source, options) {
397 options = options || {};

Callers 1

buildJSONLikeCandidatesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected