( originalContent: string, oldString: string, newString: string, replaceAll: boolean = false, )
| 204 | * @returns Array of edits with replace_all guaranteed to be boolean |
| 205 | */ |
| 206 | export function applyEditToFile( |
| 207 | originalContent: string, |
| 208 | oldString: string, |
| 209 | newString: string, |
| 210 | replaceAll: boolean = false, |
| 211 | ): string { |
| 212 | const f = replaceAll |
| 213 | ? (content: string, search: string, replace: string) => |
| 214 | content.replaceAll(search, () => replace) |
| 215 | : (content: string, search: string, replace: string) => |
| 216 | content.replace(search, () => replace) |
| 217 | |
| 218 | if (newString !== '') { |
| 219 | return f(originalContent, oldString, newString) |
| 220 | } |
| 221 | |
| 222 | const stripTrailingNewline = |
| 223 | !oldString.endsWith('\n') && originalContent.includes(oldString + '\n') |
| 224 | |
| 225 | return stripTrailingNewline |
| 226 | ? f(originalContent, oldString + '\n', newString) |
| 227 | : f(originalContent, oldString, newString) |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Applies an edit to a file and returns the patch and updated file. |
no test coverage detected