forked from slab/delta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop.js
122 lines (108 loc) · 3.39 KB
/
op.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var Delta = require('../dist/Delta');
var Op = require('../dist/Delta').Op;
describe('Op', function() {
describe('length()', function() {
it('delete', function() {
expect(Op.length({ delete: 5 })).toEqual(5);
});
it('retain', function() {
expect(Op.length({ retain: 2 })).toEqual(2);
});
it('insert text', function() {
expect(Op.length({ insert: 'text' })).toEqual(4);
});
it('insert embed', 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([]);
});
});
});