UNPKG

patch-text

Version:

Make multiple changes to a block of text by providing start and end indices and replacement text

13 lines (11 loc) 388 B
'use strict' module.exports = function replaceCode (text, replacements) { var offset = 0 return replacements.reduce(function (text, update) { var start = update.start + offset var end = update.end + offset var replacement = update.replacement offset += (replacement.length - (end - start)) return text.slice(0, start) + replacement + text.slice(end) }, text) }