Skip to content
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

Preventing cyclic dependency by inlining Op.iterator #58

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/Delta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import cloneDeep from 'lodash.clonedeep';
import isEqual from 'lodash.isequal';
import AttributeMap from './AttributeMap';
import Op from './Op';
import Iterator from './Iterator';

const NULL_CHARACTER = String.fromCharCode(0); // Placeholder char for embed in diff()

class Delta {
static Op = Op;
static Iterator = Iterator;
static AttributeMap = AttributeMap;

ops: Op[];
Expand Down Expand Up @@ -168,7 +170,7 @@ class Delta {

slice(start = 0, end = Infinity): Delta {
const ops = [];
const iter = Op.iterator(this.ops);
const iter = new Iterator(this.ops);
let index = 0;
while (index < end && iter.hasNext()) {
let nextOp;
Expand All @@ -184,8 +186,8 @@ class Delta {
}

compose(other: Delta): Delta {
const thisIter = Op.iterator(this.ops);
const otherIter = Op.iterator(other.ops);
const thisIter = new Iterator(this.ops);
const otherIter = new Iterator(other.ops);
const ops = [];
const firstOther = otherIter.peek();
if (
Expand Down Expand Up @@ -281,8 +283,8 @@ class Delta {
});
const retDelta = new Delta();
const diffResult = diff(strings[0], strings[1], cursor);
const thisIter = Op.iterator(this.ops);
const otherIter = Op.iterator(other.ops);
const thisIter = new Iterator(this.ops);
const otherIter = new Iterator(other.ops);
diffResult.forEach((component: diff.Diff) => {
let length = component[1].length;
while (length > 0) {
Expand Down Expand Up @@ -329,7 +331,7 @@ class Delta {
) => boolean | void,
newline = '\n',
): void {
const iter = Op.iterator(this.ops);
const iter = new Iterator(this.ops);
let line = new Delta();
let i = 0;
while (iter.hasNext()) {
Expand Down Expand Up @@ -395,8 +397,8 @@ class Delta {
return this.transformPosition(arg, priority);
}
const other: Delta = arg;
const thisIter = Op.iterator(this.ops);
const otherIter = Op.iterator(other.ops);
const thisIter = new Iterator(this.ops);
const otherIter = new Iterator(other.ops);
const delta = new Delta();
while (thisIter.hasNext() || otherIter.hasNext()) {
if (
Expand Down Expand Up @@ -433,7 +435,7 @@ class Delta {

transformPosition(index: number, priority = false): number {
priority = !!priority;
const thisIter = Op.iterator(this.ops);
const thisIter = new Iterator(this.ops);
let offset = 0;
while (thisIter.hasNext() && offset <= index) {
const length = thisIter.peekLength();
Expand Down
5 changes: 0 additions & 5 deletions src/Op.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import AttributeMap from './AttributeMap';
import Iterator from './Iterator';

interface Op {
// only one property out of {insert, delete, retain} will be present
Expand All @@ -11,10 +10,6 @@ interface Op {
}

namespace Op {
export function iterator(ops: Op[]): Iterator {
return new Iterator(ops);
}

export function length(op: Op): number {
if (typeof op.delete === 'number') {
return op.delete;
Expand Down
102 changes: 102 additions & 0 deletions test/iterator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
var Delta = require('../dist/Delta');
var Iterator = require('../dist/Delta').Iterator;

describe('Iterator', function() {
beforeEach(function() {
this.delta = new Delta()
.insert('Hello', { bold: true })
.retain(3)
.insert(2, { src: 'http://quilljs.com/' })
.delete(4);
});

it('hasNext() true', function() {
var iter = new Iterator(this.delta.ops);
expect(iter.hasNext()).toEqual(true);
});

it('hasNext() false', function() {
var iter = new Iterator([]);
expect(iter.hasNext()).toEqual(false);
});

it('peekLength() offset === 0', function() {
var iter = new Iterator(this.delta.ops);
expect(iter.peekLength()).toEqual(5);
iter.next();
expect(iter.peekLength()).toEqual(3);
iter.next();
expect(iter.peekLength()).toEqual(1);
iter.next();
expect(iter.peekLength()).toEqual(4);
});

it('peekLength() offset > 0', function() {
var iter = new Iterator(this.delta.ops);
iter.next(2);
expect(iter.peekLength()).toEqual(5 - 2);
});

it('peekLength() no ops left', function() {
var iter = new Iterator([]);
expect(iter.peekLength()).toEqual(Infinity);
});

it('peekType()', function() {
var iter = new Iterator(this.delta.ops);
expect(iter.peekType()).toEqual('insert');
iter.next();
expect(iter.peekType()).toEqual('retain');
iter.next();
expect(iter.peekType()).toEqual('insert');
iter.next();
expect(iter.peekType()).toEqual('delete');
iter.next();
expect(iter.peekType()).toEqual('retain');
});

it('next()', function() {
var iter = new Iterator(this.delta.ops);
for (var i = 0; i < this.delta.ops.length; i += 1) {
expect(iter.next()).toEqual(this.delta.ops[i]);
}
expect(iter.next()).toEqual({ retain: Infinity });
expect(iter.next(4)).toEqual({ retain: Infinity });
expect(iter.next()).toEqual({ retain: Infinity });
});

it('next(length)', function() {
var iter = new Iterator(this.delta.ops);
expect(iter.next(2)).toEqual({
insert: 'He',
attributes: { bold: true },
});
expect(iter.next(10)).toEqual({
insert: 'llo',
attributes: { bold: true },
});
expect(iter.next(1)).toEqual({ retain: 1 });
expect(iter.next(2)).toEqual({ retain: 2 });
});

it('rest()', function() {
var iter = new Iterator(this.delta.ops);
iter.next(2);
expect(iter.rest()).toEqual([
{ insert: 'llo', attributes: { bold: true } },
{ retain: 3 },
{ insert: 2, attributes: { src: 'http://quilljs.com/' } },
{ delete: 4 },
]);
iter.next(3);
expect(iter.rest()).toEqual([
{ retain: 3 },
{ insert: 2, attributes: { src: 'http://quilljs.com/' } },
{ delete: 4 },
]);
iter.next(3);
iter.next(2);
iter.next(4);
expect(iter.rest()).toEqual([]);
});
});
100 changes: 0 additions & 100 deletions test/op.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,104 +19,4 @@ describe('Op', function() {
expect(Op.length({ insert: 2 })).toEqual(1);
});
});

describe('iterator()', function() {
beforeEach(function() {
this.delta = new Delta()
.insert('Hello', { bold: true })
.retain(3)
.insert(2, { src: 'http://quilljs.com/' })
.delete(4);
});

it('hasNext() true', function() {
var iter = Op.iterator(this.delta.ops);
expect(iter.hasNext()).toEqual(true);
});

it('hasNext() false', function() {
var iter = Op.iterator([]);
expect(iter.hasNext()).toEqual(false);
});

it('peekLength() offset === 0', function() {
var iter = Op.iterator(this.delta.ops);
expect(iter.peekLength()).toEqual(5);
iter.next();
expect(iter.peekLength()).toEqual(3);
iter.next();
expect(iter.peekLength()).toEqual(1);
iter.next();
expect(iter.peekLength()).toEqual(4);
});

it('peekLength() offset > 0', function() {
var iter = Op.iterator(this.delta.ops);
iter.next(2);
expect(iter.peekLength()).toEqual(5 - 2);
});

it('peekLength() no ops left', function() {
var iter = Op.iterator([]);
expect(iter.peekLength()).toEqual(Infinity);
});

it('peekType()', function() {
var iter = Op.iterator(this.delta.ops);
expect(iter.peekType()).toEqual('insert');
iter.next();
expect(iter.peekType()).toEqual('retain');
iter.next();
expect(iter.peekType()).toEqual('insert');
iter.next();
expect(iter.peekType()).toEqual('delete');
iter.next();
expect(iter.peekType()).toEqual('retain');
});

it('next()', function() {
var iter = Op.iterator(this.delta.ops);
for (var i = 0; i < this.delta.ops.length; i += 1) {
expect(iter.next()).toEqual(this.delta.ops[i]);
}
expect(iter.next()).toEqual({ retain: Infinity });
expect(iter.next(4)).toEqual({ retain: Infinity });
expect(iter.next()).toEqual({ retain: Infinity });
});

it('next(length)', function() {
var iter = Op.iterator(this.delta.ops);
expect(iter.next(2)).toEqual({
insert: 'He',
attributes: { bold: true },
});
expect(iter.next(10)).toEqual({
insert: 'llo',
attributes: { bold: true },
});
expect(iter.next(1)).toEqual({ retain: 1 });
expect(iter.next(2)).toEqual({ retain: 2 });
});

it('rest()', function() {
var iter = Op.iterator(this.delta.ops);
iter.next(2);
expect(iter.rest()).toEqual([
{ insert: 'llo', attributes: { bold: true } },
{ retain: 3 },
{ insert: 2, attributes: { src: 'http://quilljs.com/' } },
{ delete: 4 },
]);
iter.next(3);
expect(iter.rest()).toEqual([
{ retain: 3 },
{ insert: 2, attributes: { src: 'http://quilljs.com/' } },
{ delete: 4 },
]);
iter.next(3);
iter.next(2);
iter.next(4);
expect(iter.rest()).toEqual([]);
});
});
});