(patchString, targetString)
| 44 | * @param {string} targetString |
| 45 | */ |
| 46 | export function patch(patchString, targetString) { |
| 47 | // Fix Windows inserting carriage returns |
| 48 | if (process.platform === 'win32') { |
| 49 | targetString = targetString.replace(/\r/g, ''); |
| 50 | patchString = patchString.replace(/\r/g, ''); |
| 51 | } |
| 52 | |
| 53 | const targetArr = targetString.split('\n'); |
| 54 | |
| 55 | for (const { delLines, addLines, deleteCount, start } of parsePatch(patchString)) { |
| 56 | if (delLines && !targetString.includes(delLines.slice(1).replace(/< /g, ''))) { |
| 57 | throw new Error('Patch contains deletion that does not exist in target'); |
| 58 | } |
| 59 | |
| 60 | targetArr.splice(start - 1, deleteCount, ...addLines); |
| 61 | } |
| 62 | |
| 63 | return targetArr.join('\n'); |
| 64 | } |
no test coverage detected