-
Notifications
You must be signed in to change notification settings - Fork 499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No function to apply diffs from diffChars
, diffWords
, etc.
#95
Labels
Comments
+1 I created a very crude solution with a basic test. If the owner approves I can make a pull request for this functionality: var oldStr = "please authenticate";
var newStr = "now you may authenticate";
var delta = [ { count: 3, added: undefined, removed: true, value: 'ple' },
{ count: 9, added: true, removed: undefined, value: 'now you m' },
{ count: 1, value: 'a' },
{ count: 2, added: undefined, removed: true, value: 'se' },
{ count: 1, added: true, removed: undefined, value: 'y' },
{ count: 13, value: ' authenticate' } ];
function insertAt(str, index, add) {
return [str.slice(0, index), add, str.slice(index)].join('');
}
function removeAt(str, index, count){
return str.slice(0, index) + str.slice(index+count);
};
function applyPatch(delta, str){
var result = str;
var pos = 0;
for(var i =0; i<delta.length; i++){
if(delta[i].added){
result = insertAt(result,pos,delta[i].value);
pos+=delta[i].count;
}
else if(delta[i].removed){
result = removeAt(result,pos,delta[i].count);
}else{
pos += delta[i].count;
}
}
return result;
}
console.log("Does it work?",newStr === applyPatch(delta, oldStr)); Output: Further tests are needed but I think this is the basic solution, it may not be the best performance wise though. |
+1 This would be a great addition and is exactly what I'm trying to use this library for. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
While you can create a unified diff with
createPatch
and then apply that to another string usingapplyPatch
, there doesn't seem to be a way to apply Change Objects.I think this would be helpful to apply diffs to data where line-context is not sufficient to match the diff.
e.g., something like:
The text was updated successfully, but these errors were encountered: