Split one `| a | b |` row into trimmed, unescaped cell strings. Honors * `\|` (escaped pipe) and `\\` so cells can contain literal pipes.
(line: string)
| 71 | /** Split one `| a | b |` row into trimmed, unescaped cell strings. Honors |
| 72 | * `\|` (escaped pipe) and `\\` so cells can contain literal pipes. */ |
| 73 | function splitRow(line: string): string[] { |
| 74 | let trimmed = line.trim() |
| 75 | // Drop a single leading / trailing unescaped pipe (the common form). |
| 76 | if (trimmed.startsWith('|')) trimmed = trimmed.slice(1) |
| 77 | if (endsWithUnescapedPipe(trimmed)) trimmed = trimmed.slice(0, -1) |
| 78 | |
| 79 | const cells: string[] = [] |
| 80 | let current = '' |
| 81 | for (let i = 0; i < trimmed.length; i++) { |
| 82 | const ch = trimmed[i] |
| 83 | if (ch === '\\' && i + 1 < trimmed.length) { |
| 84 | // Preserve the escape sequence verbatim for an exact round-trip; |
| 85 | // unescaping happens in `unescapeCell` for display. |
| 86 | current += ch + trimmed[i + 1] |
| 87 | i++ |
| 88 | continue |
| 89 | } |
| 90 | if (ch === '|') { |
| 91 | cells.push(current) |
| 92 | current = '' |
| 93 | continue |
| 94 | } |
| 95 | current += ch |
| 96 | } |
| 97 | cells.push(current) |
| 98 | return cells.map((c) => unescapeCell(c.trim())) |
| 99 | } |
| 100 | |
| 101 | function endsWithUnescapedPipe(text: string): boolean { |
| 102 | if (!text.endsWith('|')) return false |
no test coverage detected