* Split `s` into the contents of each top-level object literal. Brackets and * string literals are balanced so nested arrays/objects/strings inside an * object don't cause an early split.
(s: string)
| 934 | * object don't cause an early split. |
| 935 | */ |
| 936 | function splitTopLevelObjects(s: string): string[] { |
| 937 | const out: string[] = []; |
| 938 | let depth = 0; |
| 939 | let objStart = -1; |
| 940 | let inStr: string | null = null; |
| 941 | for (let i = 0; i < s.length; i++) { |
| 942 | const ch = s[i]!; |
| 943 | if (inStr) { |
| 944 | if (ch === '\\') { i++; continue; } |
| 945 | if (ch === inStr) inStr = null; |
| 946 | continue; |
| 947 | } |
| 948 | if (ch === '"' || ch === "'" || ch === '`') { inStr = ch; continue; } |
| 949 | if (depth === 0 && ch === '{') { |
| 950 | depth = 1; |
| 951 | objStart = i; |
| 952 | continue; |
| 953 | } |
| 954 | if (ch === '{' || ch === '[' || ch === '(') depth++; |
| 955 | else if (ch === '}' || ch === ']' || ch === ')') { |
| 956 | depth--; |
| 957 | if (depth === 0 && objStart >= 0 && ch === '}') { |
| 958 | out.push(s.slice(objStart + 1, i)); |
| 959 | objStart = -1; |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | return out; |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * Read a string-valued field — `key: 'value'` — out of one object literal's |
no test coverage detected