* `s` with every `[from, to)` range in `ranges` blanked to spaces, newlines kept, and every `[from, to, word]` in * `words` (which overlap neither each other nor a range) written over by its word, padded with spaces. Ranges may * come in any order and overlap.
(s: string, ranges: readonly number[], words: ReadonlyArray<readonly [number, number, string]>)
| 1032 | * come in any order and overlap. |
| 1033 | */ |
| 1034 | function applyEdits(s: string, ranges: readonly number[], words: ReadonlyArray<readonly [number, number, string]>): string { |
| 1035 | if (ranges.length === 0 && words.length === 0) return s; |
| 1036 | const edits: Array<readonly [number, number, string]> = words.slice(); |
| 1037 | for (let r = 0; r < ranges.length; r += 2) edits.push([ranges[r]!, ranges[r + 1]!, '']); |
| 1038 | edits.sort((a, b) => a[0] - b[0]); |
| 1039 | let out = ''; |
| 1040 | let last = 0; |
| 1041 | for (const [start, to, word] of edits) { |
| 1042 | const from = Math.max(start, last); |
| 1043 | if (to <= from) continue; |
| 1044 | const written = from === start && word.length <= to - from ? word : ''; |
| 1045 | out += s.slice(last, from) + written + s.slice(from + written.length, to).replace(/[^\r\n]/g, ' '); |
| 1046 | last = to; |
| 1047 | } |
| 1048 | return out + s.slice(last); |
| 1049 | } |
| 1050 | |
| 1051 | /** The start of the `class` word before index `i`, past whitespace, or -1. */ |
| 1052 | function classBefore(s: string, i: number): number { |
no test coverage detected