* `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]>)
| 962 | * come in any order and overlap. |
| 963 | */ |
| 964 | function applyEdits(s: string, ranges: readonly number[], words: ReadonlyArray<readonly [number, number, string]>): string { |
| 965 | if (ranges.length === 0 && words.length === 0) return s; |
| 966 | const edits: Array<readonly [number, number, string]> = words.slice(); |
| 967 | for (let r = 0; r < ranges.length; r += 2) edits.push([ranges[r]!, ranges[r + 1]!, '']); |
| 968 | edits.sort((a, b) => a[0] - b[0]); |
| 969 | let out = ''; |
| 970 | let last = 0; |
| 971 | for (const [start, to, word] of edits) { |
| 972 | const from = Math.max(start, last); |
| 973 | if (to <= from) continue; |
| 974 | const written = from === start && word.length <= to - from ? word : ''; |
| 975 | out += s.slice(last, from) + written + s.slice(from + written.length, to).replace(/[^\r\n]/g, ' '); |
| 976 | last = to; |
| 977 | } |
| 978 | return out + s.slice(last); |
| 979 | } |
| 980 | |
| 981 | /** The start of the `class` word before index `i`, past whitespace, or -1. */ |
| 982 | function classBefore(s: string, i: number): number { |
no test coverage detected