Skip to content

Instantly share code, notes, and snippets.

@LeZuse
Created July 14, 2012 19:25
Show Gist options
  • Select an option

  • Save LeZuse/3112890 to your computer and use it in GitHub Desktop.

Select an option

Save LeZuse/3112890 to your computer and use it in GitHub Desktop.
OT: apply operation
var apply_operation = function(content, op) {
var buffer = content.split('');
var cursor = 0;
op.forEach(function (action) {
if (action.retain) {
cursor += action.retain;
// console.log('retain', '+'+action.retain, buffer.join(''), cursor);
}
else if (action.insert) {
var args = [cursor, 0];
args = args.concat(action.insert.split(''));
buffer.splice.apply(buffer, args);
cursor += action.insert.length;
// console.log('insert', action.insert, buffer.join(''), cursor);
}
else if (action['delete']) {
buffer.splice(cursor, action['delete']);
// console.log('delete', action['delete'], buffer.join(''), cursor);
}
});
return buffer.join('');
};
module.exports = apply_operation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment