The arguments between the `<` at `open` and the `>` at `close`, split at the top level, each with where it starts.
(text: string, open: number, close: number)
| 236 | |
| 237 | /** The arguments between the `<` at `open` and the `>` at `close`, split at the top level, each with where it starts. */ |
| 238 | function argumentsBetween(text: string, open: number, close: number): Array<{ text: string; offset: number }> { |
| 239 | const out: Array<{ text: string; offset: number }> = []; |
| 240 | let depth = 0; |
| 241 | let parens = 0; |
| 242 | let start = open + 1; |
| 243 | const push = (end: number): void => { |
| 244 | const raw = text.slice(start, end); |
| 245 | const trimmed = raw.trim(); |
| 246 | if (trimmed) out.push({ text: trimmed, offset: start + raw.indexOf(trimmed) }); |
| 247 | }; |
| 248 | for (let i = open + 1; i < close; i++) { |
| 249 | const c = text[i]!; |
| 250 | if (c === '(' || c === '[') parens++; |
| 251 | else if (c === ')' || c === ']') parens--; |
| 252 | else if (parens > 0) continue; |
| 253 | else if (c === '<') depth++; |
| 254 | else if (c === '>' && text[i - 1] !== '-') depth--; |
| 255 | else if (c === ',' && depth === 0) { |
| 256 | push(i); |
| 257 | start = i + 1; |
| 258 | } |
| 259 | } |
| 260 | push(close); |
| 261 | return out; |
| 262 | } |
| 263 | |
| 264 | /** The `>` closing the argument list opened at `open`, or -1 when `<` opens none there. */ |
| 265 | function closingAngle(text: string, open: number): number { |
no test coverage detected