diff-delta
v1.0.0
Published
Convert an add/remove diff into an add/remove/update delta
Readme
diff-delta
Converts a generic-diff diff into a series of add/update/remove deltas.
Installation
$ npm install --save diff-deltaExample
var diff = require('generic-diff')
var delta = require('diff-delta')
var d = delta(diff('falafel dreams', 'fallacy means'))
console.log(d)The above code logs the following:
[
{
"items": ["a", "f", "e"],
"index": 3,
"added": false,
"removed": true
},
{
"items": ["a", "c", "y"],
"index": 4,
"added": true,
"removed": false
},
{
"items": ["m"],
"index": 8,
"added": true,
"removed": true
},
{
"items": ["r"],
"index": 9,
"added": false,
"removed": true
},
{
"items": ["n"],
"index": 11,
"added": true,
"removed": true
}
]Each item in the array represents a delta, with an index at which to apply the delta assuming all previous deltas have been applied. The above sequences of deltas can thus be interpreted as:
- Start with the original string,
falafel dreams - Remove 3 items,
afe, starting at index 3:fall dreams - Add
acyat index 4:fallacy dreams - Replace 1 item at index 8 with
m:fallacy mreams - Remove 1 item,
m, at index 9:fallacy meams - Replace 1 item at index 11 with
n:fallacy means
License
MIT.
