Skip to content

Commit 0a5748c

Browse files
committed
refactor(json-crdt-extensions): 💡 rename comparison methods
1 parent ca25fcd commit 0a5748c

File tree

5 files changed

+60
-60
lines changed

5 files changed

+60
-60
lines changed

src/json-crdt-extensions/peritext/point/Point.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class Point<T = string> implements Pick<Stateful, 'refresh'>, Printable {
6161
*
6262
* @todo Rename to `cmp`.
6363
*/
64-
public compare(other: Point<T>): -1 | 0 | 1 {
64+
public cmp(other: Point<T>): -1 | 0 | 1 {
6565
const cmp = compare(this.id, other.id);
6666
if (cmp !== 0) return cmp;
6767
return (this.anchor - other.anchor) as -1 | 0 | 1;
@@ -79,7 +79,7 @@ export class Point<T = string> implements Pick<Stateful, 'refresh'>, Printable {
7979
*
8080
* @todo Rename to `cmpSpatial`.
8181
*/
82-
public compareSpatial(other: Point<T>): number {
82+
public cmpSpatial(other: Point<T>): number {
8383
const thisId = this.id;
8484
const otherId = other.id;
8585
if (this.isAbs()) {

src/json-crdt-extensions/peritext/point/__tests__/Point.spec.ts

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ describe('.set()', () => {
2323
expect(p1.refresh()).not.toBe(p2.refresh());
2424
p1.set(p2);
2525
expect(p1.refresh()).toBe(p2.refresh());
26-
expect(p1.compare(p2)).toBe(0);
27-
expect(p1.compareSpatial(p2)).toBe(0);
26+
expect(p1.cmp(p2)).toBe(0);
27+
expect(p1.cmpSpatial(p2)).toBe(0);
2828
expect(p1.id.sid).toBe(p2.id.sid);
2929
expect(p1.id.time).toBe(p2.id.time);
3030
expect(p1.anchor).toBe(p2.anchor);
@@ -39,22 +39,22 @@ describe('.clone()', () => {
3939
const p1 = peritext.point(id, Anchor.Before);
4040
const p2 = p1.clone();
4141
expect(p1.refresh()).toBe(p2.refresh());
42-
expect(p1.compare(p2)).toBe(0);
43-
expect(p1.compareSpatial(p2)).toBe(0);
42+
expect(p1.cmp(p2)).toBe(0);
43+
expect(p1.cmpSpatial(p2)).toBe(0);
4444
expect(p1.id.sid).toBe(p2.id.sid);
4545
expect(p1.id.time).toBe(p2.id.time);
4646
expect(p1.anchor).toBe(p2.anchor);
4747
});
4848
});
4949

50-
describe('.compare()', () => {
50+
describe('.cmp()', () => {
5151
test('returns 0 for equal points', () => {
5252
const {peritext} = setup();
5353
const chunk = peritext.str.first()!;
5454
const id = chunk.id;
5555
const p1 = peritext.point(id, Anchor.Before);
5656
const p2 = peritext.point(id, Anchor.Before);
57-
expect(p1.compare(p2)).toBe(0);
57+
expect(p1.cmp(p2)).toBe(0);
5858
});
5959

6060
test('compares by ID first, then by anchor', () => {
@@ -75,18 +75,18 @@ describe('.compare()', () => {
7575
const p1 = points[i];
7676
const p2 = points[j];
7777
if (i === j) {
78-
expect(p1.compare(p2)).toBe(0);
78+
expect(p1.cmp(p2)).toBe(0);
7979
} else if (i < j) {
80-
expect(p1.compare(p2)).toBeLessThan(0);
80+
expect(p1.cmp(p2)).toBeLessThan(0);
8181
} else {
82-
expect(p1.compare(p2)).toBeGreaterThan(0);
82+
expect(p1.cmp(p2)).toBeGreaterThan(0);
8383
}
8484
}
8585
}
8686
});
8787
});
8888

89-
describe('.compareSpatial()', () => {
89+
describe('.cmpSpatial()', () => {
9090
test('higher spacial points return positive value', () => {
9191
const {peritext} = setup();
9292
const chunk1 = peritext.str.first()!;
@@ -96,22 +96,22 @@ describe('.compareSpatial()', () => {
9696
const p2 = peritext.point(id1, Anchor.After);
9797
const p3 = peritext.point(id2, Anchor.Before);
9898
const p4 = peritext.point(id2, Anchor.After);
99-
expect(p1.compareSpatial(p1)).toBe(0);
100-
expect(p4.compareSpatial(p4)).toBe(0);
101-
expect(p4.compareSpatial(p4)).toBe(0);
102-
expect(p4.compareSpatial(p4)).toBe(0);
103-
expect(p2.compareSpatial(p1) > 0).toBe(true);
104-
expect(p3.compareSpatial(p1) > 0).toBe(true);
105-
expect(p4.compareSpatial(p1) > 0).toBe(true);
106-
expect(p3.compareSpatial(p2) > 0).toBe(true);
107-
expect(p4.compareSpatial(p2) > 0).toBe(true);
108-
expect(p4.compareSpatial(p3) > 0).toBe(true);
109-
expect(p1.compareSpatial(p2) < 0).toBe(true);
110-
expect(p1.compareSpatial(p3) < 0).toBe(true);
111-
expect(p1.compareSpatial(p4) < 0).toBe(true);
112-
expect(p2.compareSpatial(p3) < 0).toBe(true);
113-
expect(p2.compareSpatial(p4) < 0).toBe(true);
114-
expect(p3.compareSpatial(p4) < 0).toBe(true);
99+
expect(p1.cmpSpatial(p1)).toBe(0);
100+
expect(p4.cmpSpatial(p4)).toBe(0);
101+
expect(p4.cmpSpatial(p4)).toBe(0);
102+
expect(p4.cmpSpatial(p4)).toBe(0);
103+
expect(p2.cmpSpatial(p1) > 0).toBe(true);
104+
expect(p3.cmpSpatial(p1) > 0).toBe(true);
105+
expect(p4.cmpSpatial(p1) > 0).toBe(true);
106+
expect(p3.cmpSpatial(p2) > 0).toBe(true);
107+
expect(p4.cmpSpatial(p2) > 0).toBe(true);
108+
expect(p4.cmpSpatial(p3) > 0).toBe(true);
109+
expect(p1.cmpSpatial(p2) < 0).toBe(true);
110+
expect(p1.cmpSpatial(p3) < 0).toBe(true);
111+
expect(p1.cmpSpatial(p4) < 0).toBe(true);
112+
expect(p2.cmpSpatial(p3) < 0).toBe(true);
113+
expect(p2.cmpSpatial(p4) < 0).toBe(true);
114+
expect(p3.cmpSpatial(p4) < 0).toBe(true);
115115
});
116116

117117
test('correctly orders points when tombstones are present', () => {
@@ -150,15 +150,15 @@ describe('.compareSpatial()', () => {
150150
const p2 = points[j];
151151
try {
152152
if (i === j) {
153-
expect(p1.compareSpatial(p2)).toBe(0);
153+
expect(p1.cmpSpatial(p2)).toBe(0);
154154
} else if (i < j) {
155-
expect(p1.compareSpatial(p2)).toBeLessThan(0);
155+
expect(p1.cmpSpatial(p2)).toBeLessThan(0);
156156
} else {
157-
expect(p1.compareSpatial(p2)).toBeGreaterThan(0);
157+
expect(p1.cmpSpatial(p2)).toBeGreaterThan(0);
158158
}
159159
} catch (error) {
160160
// tslint:disable-next-line:no-console
161-
console.log('i: ', i, 'j: ', j, 'p1: ', p1 + '', 'p2: ', p2 + '', p1.compareSpatial(p2));
161+
console.log('i: ', i, 'j: ', j, 'p1: ', p1 + '', 'p2: ', p2 + '', p1.cmpSpatial(p2));
162162
throw error;
163163
}
164164
}
@@ -181,17 +181,17 @@ describe('.compareSpatial()', () => {
181181
const p6 = peritext.point(id3, Anchor.After);
182182
const points = [p0, p1, p2, p3, p4, p5, p6];
183183
for (const point of points) {
184-
expect(absoluteEnd.compareSpatial(point)).toBe(1);
185-
expect(point.compareSpatial(absoluteEnd)).toBe(-1);
184+
expect(absoluteEnd.cmpSpatial(point)).toBe(1);
185+
expect(point.cmpSpatial(absoluteEnd)).toBe(-1);
186186
}
187187
});
188188

189189
test('two absolute ends are equal', () => {
190190
const {peritext} = setup();
191191
const p1 = peritext.pointAbsEnd();
192192
const p2 = peritext.pointAbsEnd();
193-
expect(p1.compareSpatial(p2)).toBe(0);
194-
expect(p2.compareSpatial(p1)).toBe(0);
193+
expect(p1.cmpSpatial(p2)).toBe(0);
194+
expect(p2.cmpSpatial(p1)).toBe(0);
195195
});
196196

197197
test('absolute start point is always less than any other point', () => {
@@ -210,17 +210,17 @@ describe('.compareSpatial()', () => {
210210
const p6 = peritext.point(id3, Anchor.After);
211211
const points = [p0, p1, p2, p3, p4, p5, p6];
212212
for (const point of points) {
213-
expect(absoluteEnd.compareSpatial(point)).toBe(-1);
214-
expect(point.compareSpatial(absoluteEnd)).toBe(1);
213+
expect(absoluteEnd.cmpSpatial(point)).toBe(-1);
214+
expect(point.cmpSpatial(absoluteEnd)).toBe(1);
215215
}
216216
});
217217

218218
test('two absolute starts are equal', () => {
219219
const {peritext} = setup();
220220
const p1 = peritext.pointAbsStart();
221221
const p2 = peritext.pointAbsStart();
222-
expect(p1.compareSpatial(p2)).toBe(0);
223-
expect(p2.compareSpatial(p1)).toBe(0);
222+
expect(p1.cmpSpatial(p2)).toBe(0);
223+
expect(p2.cmpSpatial(p1)).toBe(0);
224224
});
225225
});
226226

@@ -952,9 +952,9 @@ describe('.refBefore()', () => {
952952
const chunk1 = peritext.str.first()!;
953953
const absoluteStart = peritext.pointAbsStart();
954954
const start = peritext.point(chunk1.id, Anchor.Before);
955-
expect(absoluteStart.compareSpatial(start) < 0).toBe(true);
955+
expect(absoluteStart.cmpSpatial(start) < 0).toBe(true);
956956
absoluteStart.refBefore();
957-
expect(absoluteStart.compareSpatial(start) === 0).toBe(true);
957+
expect(absoluteStart.cmpSpatial(start) === 0).toBe(true);
958958
});
959959
});
960960

@@ -998,9 +998,9 @@ describe('.refAfter()', () => {
998998
const id = tick(chunk1.id, 2);
999999
const absoluteEnd = peritext.pointAbsEnd();
10001000
const end = peritext.point(id, Anchor.After);
1001-
expect(absoluteEnd.compareSpatial(end) > 0).toBe(true);
1001+
expect(absoluteEnd.cmpSpatial(end) > 0).toBe(true);
10021002
absoluteEnd.refAfter();
1003-
expect(absoluteEnd.compareSpatial(end) === 0).toBe(true);
1003+
expect(absoluteEnd.cmpSpatial(end) === 0).toBe(true);
10041004
});
10051005

10061006
test('when absolute end, attaches to last visible char', () => {
@@ -1010,12 +1010,12 @@ describe('.refAfter()', () => {
10101010
const end1 = peritext.point(tick(chunk1.id, 1), Anchor.After);
10111011
const end2 = peritext.point(tick(chunk1.id, 2), Anchor.After);
10121012
peritext.strApi().del(2, 1);
1013-
expect(end1.compareSpatial(end2) < 0).toBe(true);
1014-
expect(absoluteEnd.compareSpatial(end2) > 0).toBe(true);
1013+
expect(end1.cmpSpatial(end2) < 0).toBe(true);
1014+
expect(absoluteEnd.cmpSpatial(end2) > 0).toBe(true);
10151015
end2.refAfter();
10161016
absoluteEnd.refAfter();
1017-
expect(end2.compareSpatial(end1) === 0).toBe(true);
1018-
expect(absoluteEnd.compareSpatial(end1) === 0).toBe(true);
1017+
expect(end2.cmpSpatial(end1) === 0).toBe(true);
1018+
expect(absoluteEnd.cmpSpatial(end1) === 0).toBe(true);
10191019
});
10201020
});
10211021

@@ -1037,12 +1037,12 @@ describe('.refVisible()', () => {
10371037
peritext.strApi().del(3, 3);
10381038
expect(left.leftChar()!.view()).toBe('3');
10391039
expect(right.rightChar()!.view()).toBe('7');
1040-
expect(mid1.compare(left) > 0).toBe(true);
1040+
expect(mid1.cmp(left) > 0).toBe(true);
10411041
mid1.refVisible();
1042-
expect(mid1.compare(left) === 0).toBe(true);
1043-
expect(mid2.compare(right) < 0).toBe(true);
1042+
expect(mid1.cmp(left) === 0).toBe(true);
1043+
expect(mid2.cmp(right) < 0).toBe(true);
10441044
mid2.refVisible();
1045-
expect(mid2.compare(right) === 0).toBe(true);
1045+
expect(mid2.cmp(right) === 0).toBe(true);
10461046
});
10471047
});
10481048

src/json-crdt-extensions/peritext/slice/Cursor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class Cursor extends Range<string> implements Slice {
6666
let focus = this.focus();
6767
if (edge === 0) focus = point;
6868
else anchor = point;
69-
if (focus.compareSpatial(anchor) < 0) {
69+
if (focus.cmpSpatial(anchor) < 0) {
7070
this.base = Anchor.After;
7171
this.start = focus;
7272
this.end = anchor;

src/json-crdt-extensions/peritext/slice/Range.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class Range<T = string> implements Printable {
2020
* @returns Range with points in correct order.
2121
*/
2222
public static from<T = string>(rga: AbstractRga<T>, p1: Point<T>, p2: Point<T>): Range<T> {
23-
return p1.compareSpatial(p2) > 0 ? new Range(rga, p2, p1) : new Range(rga, p1, p2);
23+
return p1.cmpSpatial(p2) > 0 ? new Range(rga, p2, p1) : new Range(rga, p1, p2);
2424
}
2525

2626
/**
@@ -101,12 +101,12 @@ export class Range<T = string> implements Printable {
101101
*/
102102
public isCollapsed(): boolean {
103103
const {start, end} = this;
104-
if (start.compareSpatial(end) === 0) return true;
104+
if (start.cmpSpatial(end) === 0) return true;
105105
const start2 = start.clone();
106106
const end2 = end.clone();
107107
start2.refAfter();
108108
end2.refAfter();
109-
return start2.compare(end2) === 0;
109+
return start2.cmp(end2) === 0;
110110
}
111111

112112
/**
@@ -149,11 +149,11 @@ export class Range<T = string> implements Printable {
149149
}
150150

151151
public contains(range: Range<T>): boolean {
152-
return this.start.compareSpatial(range.start) <= 0 && this.end.compareSpatial(range.end) >= 0;
152+
return this.start.cmpSpatial(range.start) <= 0 && this.end.cmpSpatial(range.end) >= 0;
153153
}
154154

155155
public containsPoint(point: Point<T>): boolean {
156-
return this.start.compareSpatial(point) <= 0 && this.end.compareSpatial(point) >= 0;
156+
return this.start.cmpSpatial(point) <= 0 && this.end.cmpSpatial(point) >= 0;
157157
}
158158

159159
/**

src/json-crdt-extensions/peritext/slice/__tests__/Range.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ describe('.clone()', () => {
193193
expect(range2.end).not.toBe(range1.end);
194194
expect(range2.start.refresh()).toBe(range1.start.refresh());
195195
expect(range2.end.refresh()).toBe(range1.end.refresh());
196-
expect(range2.start.compare(range1.start)).toBe(0);
197-
expect(range2.end.compare(range1.end)).toBe(0);
196+
expect(range2.start.cmp(range1.start)).toBe(0);
197+
expect(range2.end.cmp(range1.end)).toBe(0);
198198
});
199199
});
200200

0 commit comments

Comments
 (0)