strong-trace
Version:
StrongTrace Node.js Tracer
32 lines (23 loc) • 732 B
JavaScript
;
var inherits = require("util").inherits
module.exports = applyChangelist
function applyChangelist(content, changelist) {
if (changelist.length === 0) {
return content
}
changelist = changelist.sort(function (a, b) {
// Sort by position, then pri (priority) in case of tie.
return (a.pos == b.pos) ? b.pri - a.pri : b.pos - a.pos
})
// console.log(changelist)
for (var i = 0; i < changelist.length; i++) {
content = spliceIn(content, changelist[i])
}
return content
}
function spliceIn(content, change) {
var prelude = content.slice(0, change.pos)
var remove = change.remove || 0
var after = content.slice(change.pos + remove)
return prelude + change.insertion + after
}