forked from slab/delta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop.js
139 lines (124 loc) · 3.69 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var equal = require('deep-equal');
var extend = require('extend');
var lib = {
attributes: {
compose: function (a, b, keepNull) {
if (typeof a !== 'object') a = {};
if (typeof b !== 'object') b = {};
var attributes = extend(true, {}, b);
if (!keepNull) {
attributes = Object.keys(attributes).reduce(function (copy, key) {
if (attributes[key] != null) {
copy[key] = attributes[key];
}
return copy;
}, {});
}
for (var key in a) {
if (a[key] !== undefined && b[key] === undefined) {
attributes[key] = a[key];
}
}
return Object.keys(attributes).length > 0 ? attributes : undefined;
},
diff: function(a, b) {
if (typeof a !== 'object') a = {};
if (typeof b !== 'object') b = {};
var attributes = Object.keys(a).concat(Object.keys(b)).reduce(function (attributes, key) {
if (!equal(a[key], b[key])) {
attributes[key] = b[key] === undefined ? null : b[key];
}
return attributes;
}, {});
return Object.keys(attributes).length > 0 ? attributes : undefined;
},
transform: function (a, b, priority) {
if (typeof a !== 'object') return b;
if (typeof b !== 'object') return undefined;
if (!priority) return b; // b simply overwrites us without priority
var attributes = Object.keys(b).reduce(function (attributes, key) {
if (a[key] === undefined) attributes[key] = b[key]; // null is a valid value
return attributes;
}, {});
return Object.keys(attributes).length > 0 ? attributes : undefined;
}
},
iterator: function (ops) {
return new Iterator(ops);
},
length: function (op) {
if (typeof op['delete'] === 'number') {
return op['delete'];
} else if (typeof op.retain === 'number') {
return op.retain;
} else {
return typeof op.insert === 'string' ? op.insert.length : 1;
}
}
};
function Iterator(ops) {
this.ops = ops;
this.index = 0;
this.offset = 0;
};
Iterator.prototype.hasNext = function () {
return this.peekLength() < Infinity;
};
Iterator.prototype.next = function (length) {
if (!length) length = Infinity;
var nextOp = this.ops[this.index];
if (nextOp) {
var offset = this.offset;
var opLength = lib.length(nextOp)
if (length >= opLength - offset) {
length = opLength - offset;
this.index += 1;
this.offset = 0;
} else {
this.offset += length;
}
if (typeof nextOp['delete'] === 'number') {
return { 'delete': length };
} else {
var retOp = {};
if (nextOp.attributes) {
retOp.attributes = nextOp.attributes;
}
if (typeof nextOp.retain === 'number') {
retOp.retain = length;
} else if (typeof nextOp.insert === 'string') {
retOp.insert = nextOp.insert.substr(offset, length);
} else {
// offset should === 0, length should === 1
retOp.insert = nextOp.insert;
}
return retOp;
}
} else {
return { retain: Infinity };
}
};
Iterator.prototype.peek = function () {
return this.ops[this.index];
};
Iterator.prototype.peekLength = function () {
if (this.ops[this.index]) {
// Should never return 0 if our index is being managed correctly
return lib.length(this.ops[this.index]) - this.offset;
} else {
return Infinity;
}
};
Iterator.prototype.peekType = function () {
if (this.ops[this.index]) {
if (typeof this.ops[this.index]['delete'] === 'number') {
return 'delete';
} else if (typeof this.ops[this.index].retain === 'number') {
return 'retain';
} else {
return 'insert';
}
}
return 'retain';
};
module.exports = lib;