(str, sep = ',')
| 2043 | } |
| 2044 | |
| 2045 | function splitTopLevel(str, sep = ',') { |
| 2046 | let arr = [] |
| 2047 | let current = '' |
| 2048 | let quote = '' |
| 2049 | let braceDepth = 0 |
| 2050 | let bracketDepth = 0 |
| 2051 | let parenDepth = 0 |
| 2052 | for (let i = 0; i < str.length; i++) { |
| 2053 | const char = str[i] |
| 2054 | const prev = str[i - 1] |
| 2055 | if (quote) { |
| 2056 | current += char |
| 2057 | if (char === quote && prev !== '\\') quote = '' |
| 2058 | continue |
| 2059 | } |
| 2060 | if (char === '"' || char === "'") { |
| 2061 | quote = char |
| 2062 | current += char |
| 2063 | continue |
| 2064 | } |
| 2065 | if (char === '{') braceDepth++ |
| 2066 | if (char === '}') braceDepth = Math.max(0, braceDepth - 1) |
| 2067 | if (char === '[') bracketDepth++ |
| 2068 | if (char === ']') bracketDepth = Math.max(0, bracketDepth - 1) |
| 2069 | if (char === '(') parenDepth++ |
| 2070 | if (char === ')') parenDepth = Math.max(0, parenDepth - 1) |
| 2071 | if (char === sep && braceDepth === 0 && bracketDepth === 0 && parenDepth === 0) { |
| 2072 | arr.push(current.trim()) |
| 2073 | current = '' |
| 2074 | } else { |
| 2075 | current += char |
| 2076 | } |
| 2077 | } |
| 2078 | arr.push(current.trim()) |
| 2079 | return arr |
| 2080 | } |
| 2081 | |
| 2082 | function splitFirstTopLevel(str, sep) { |
| 2083 | const parts = splitTopLevel(str, sep) |
no outgoing calls
no test coverage detected