Skip to content

Commit

Permalink
add delta invert and tests, closes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Feb 11, 2019
1 parent a2b9946 commit 9de0632
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Delta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,24 @@ class Delta {
} else if (op.retain && op.attributes == null) {
inverted.retain(op.retain);
return baseIndex + op.retain;
} else if (op.delete) {
base
.slice(baseIndex, baseIndex + op.delete)
.forEach(o => inverted.push(o));
return baseIndex + op.delete;
} else if (op.delete || (op.retain && op.attributes)) {
const length = (op.delete || op.retain) as number;
const slice = base.slice(baseIndex, baseIndex + length);
slice.forEach(baseOp => {
if (op.delete) {
inverted.push(baseOp);
} else if (op.retain && op.attributes) {
inverted.retain(
op.retain,
AttributeMap.invert(op.attributes, baseOp.attributes),
);
}
});
return baseIndex + length;
}
return baseIndex;
}, 0);
return inverted;
return inverted.chop();
}

transform(index: number, priority?: boolean): number;
Expand Down
55 changes: 55 additions & 0 deletions test/delta/invert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var Delta = require('../../dist/Delta');

describe('invert()', function() {
it('insert', function() {
var delta = new Delta().retain(2).insert('A');
var base = new Delta().insert('123456');
var expected = new Delta().retain(2).delete(1);
var inverted = delta.invert(base);
expect(expected).toEqual(inverted);
expect(base.compose(delta).compose(inverted)).toEqual(base);
});

it('delete', function() {
var delta = new Delta().retain(2).delete(3);
var base = new Delta().insert('123456');
var expected = new Delta().retain(2).insert('345');
var inverted = delta.invert(base);
expect(expected).toEqual(inverted);
expect(base.compose(delta).compose(inverted)).toEqual(base);
});

it('retain', function() {
var delta = new Delta().retain(2).retain(3, { bold: true });
var base = new Delta().insert('123456');
var expected = new Delta().retain(2).retain(3, { bold: null });
var inverted = delta.invert(base);
expect(expected).toEqual(inverted);
expect(base.compose(delta).compose(inverted)).toEqual(base);
});

it('combined', function() {
var delta = new Delta()
.retain(2)
.delete(2)
.insert('AB', { italic: true })
.retain(2, { italic: null, bold: true })
.retain(2, { color: 'red' })
.delete(1);
var base = new Delta()
.insert('123', { bold: true })
.insert('456', { italic: true })
.insert('789', { color: 'red', bold: true });
var expected = new Delta()
.retain(2)
.insert('3', { bold: true })
.insert('4', { italic: true })
.delete(2)
.retain(2, { italic: true, bold: null })
.retain(2)
.insert('9', { color: 'red', bold: true });
var inverted = delta.invert(base);
expect(expected).toEqual(inverted);
expect(base.compose(delta).compose(inverted)).toEqual(base);
});
});

0 comments on commit 9de0632

Please sign in to comment.