(url: string, text: string, index: SiteFixesIndex, parse: (text: string) => T[])
| 130 | const siteFixesCache = new WeakMap<[number, number], any>(); |
| 131 | |
| 132 | export function getSitesFixesFor<T extends SiteProps>(url: string, text: string, index: SiteFixesIndex, parse: (text: string) => T[]): Array<Readonly<T>> { |
| 133 | const matches = getURLMatchesFromIndexedList(url, index); |
| 134 | |
| 135 | const fixes = matches.map((offset) => { |
| 136 | const cache = siteFixesCache.get(offset); |
| 137 | if (cache) { |
| 138 | return cache; |
| 139 | } |
| 140 | const [start, length] = offset; |
| 141 | const block = text.slice(start, start + length); |
| 142 | const fix = parse(block)[0]; |
| 143 | siteFixesCache.set(offset, fix); |
| 144 | return fix; |
| 145 | }); |
| 146 | |
| 147 | if (fixes.length > 0) { |
| 148 | const commonFixIndex = fixes.findIndex((f) => f.url?.[0] === '*'); |
| 149 | if (commonFixIndex > 0) { |
| 150 | const commonFix = fixes[commonFixIndex]; |
| 151 | fixes.splice(commonFixIndex, 1); |
| 152 | fixes.unshift(commonFix); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return fixes; |
| 157 | } |
no test coverage detected