forked from ottypes/json0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
48 lines (39 loc) · 1.03 KB
/
utils.js
File metadata and controls
48 lines (39 loc) · 1.03 KB
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
// helper functions to convert old string ops to and from subtype ops
function convertFromText(c) {
c.t = 'text0';
var o = {p: c.p.pop()};
if (c.si != null) o.i = c.si;
if (c.sd != null) o.d = c.sd;
c.o = [o];
}
function convertToText(c) {
c.p.push(c.o[0].p);
if (c.o[0].i != null) c.si = c.o[0].i;
if (c.o[0].d != null) c.sd = c.o[0].d;
delete c.t;
delete c.o;
}
// Returns the common length of the paths of ops a and b
function commonLengthForOps(a, b) {
var alen = a.p.length;
var blen = b.p.length;
if (a.na != null || a.t)
alen++;
if (b.na != null || b.t)
blen++;
if (alen === 0) return -1;
if (blen === 0) return null;
alen--;
blen--;
for (var i = 0; i < alen; i++) {
var p = a.p[i];
if (i >= blen || p !== b.p[i])
return null;
}
return alen;
};
// Returns true if an op can affect the given path
function canOpAffectPath(op, path) {
return commonLengthForOps({p:path}, op) != null;
};
module.exports = { convertFromText, convertToText, commonLengthForOps, canOpAffectPath };