Created
July 14, 2012 19:25
-
-
Save LeZuse/3112890 to your computer and use it in GitHub Desktop.
OT: apply operation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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