Skip to content

Commit 414944c

Browse files
committed
refactor(json-crdt-diff): 💡 rename Diff to JsonCrdtDiff
1 parent 12998ea commit 414944c

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

src/json-crdt-diff/Diff.ts renamed to src/json-crdt-diff/JsonCrdtDiff.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class DiffError extends Error {
1515
}
1616
}
1717

18-
export class Diff {
18+
export class JsonCrdtDiff {
1919
protected builder: PatchBuilder;
2020

2121
public constructor(protected readonly model: Model<any>) {

src/json-crdt-diff/__tests__/Diff-fuzzing.spec.ts renamed to src/json-crdt-diff/__tests__/JsonCrdtDiff-fuzzing.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import {Diff} from '../Diff';
1+
import {JsonCrdtDiff} from '../JsonCrdtDiff';
22
import {Model} from '../../json-crdt/model';
33
import {RandomJson} from '@jsonjoy.com/util/lib/json-random';
44

55
const assertDiff = (src: unknown, dst: unknown) => {
66
const model = Model.create();
77
model.api.root(src);
8-
const patch1 = new Diff(model).diff(model.root, dst);
8+
const patch1 = new JsonCrdtDiff(model).diff(model.root, dst);
99
// console.log(model + '');
1010
// console.log(patch1 + '');
1111
model.applyPatch(patch1);
1212
// console.log(model + '');
1313
expect(model.view()).toEqual(dst);
14-
const patch2 = new Diff(model).diff(model.root, dst);
14+
const patch2 = new JsonCrdtDiff(model).diff(model.root, dst);
1515
expect(patch2.ops.length).toBe(0);
1616
};
1717

src/json-crdt-diff/__tests__/Diff.spec.ts renamed to src/json-crdt-diff/__tests__/JsonCrdtDiff.spec.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
import {Diff} from '../Diff';
1+
import {JsonCrdtDiff} from '../JsonCrdtDiff';
22
import {InsStrOp, s} from '../../json-crdt-patch';
33
import {Model} from '../../json-crdt/model';
44
import {JsonNode, ValNode} from '../../json-crdt/nodes';
55
import {b} from '@jsonjoy.com/util/lib/buffers/b';
66

77
const assertDiff = (model: Model<any>, src: JsonNode, dst: unknown) => {
8-
const patch1 = new Diff(model).diff(src, dst);
8+
const patch1 = new JsonCrdtDiff(model).diff(src, dst);
99
// console.log(model + '');
1010
// console.log(dst);
1111
// console.log(patch1 + '');
1212
model.applyPatch(patch1);
1313
// console.log(model + '');
1414
expect(src.view()).toEqual(dst);
15-
const patch2 = new Diff(model).diff(src, dst);
15+
const patch2 = new JsonCrdtDiff(model).diff(src, dst);
1616
// console.log(patch2 + '');
1717
expect(patch2.ops.length).toBe(0);
1818
};
1919

20+
const assertDiff2 = (src: unknown, dst: unknown) => {
21+
const model = Model.create();
22+
model.api.root(src);
23+
assertDiff(model, model.root.child(), dst);
24+
};
25+
2026
describe('con', () => {
2127
test('binary in "con"', () => {
2228
const model = Model.create(s.obj({
@@ -36,7 +42,7 @@ describe('str', () => {
3642
model.api.root({str: src});
3743
const str = model.api.str(['str']);
3844
const dst = 'hello world!';
39-
const patch = new Diff(model).diff(str.node, dst);
45+
const patch = new JsonCrdtDiff(model).diff(str.node, dst);
4046
expect(patch.ops.length).toBe(1);
4147
expect(patch.ops[0].name()).toBe('ins_str');
4248
expect((patch.ops[0] as InsStrOp).data).toBe('!');
@@ -51,7 +57,7 @@ describe('str', () => {
5157
model.api.root({str: src});
5258
const str = model.api.str(['str']);
5359
const dst = 'hello world';
54-
const patch = new Diff(model).diff(str.node, dst);
60+
const patch = new JsonCrdtDiff(model).diff(str.node, dst);
5561
expect(patch.ops.length).toBe(1);
5662
expect(patch.ops[0].name()).toBe('del');
5763
expect(str.view()).toBe(src);
@@ -65,7 +71,7 @@ describe('str', () => {
6571
model.api.root({str: src});
6672
const str = model.api.str(['str']);
6773
const dst = '2x3y';
68-
const patch = new Diff(model).diff(str.node, dst);
74+
const patch = new JsonCrdtDiff(model).diff(str.node, dst);
6975
expect(str.view()).toBe(src);
7076
model.applyPatch(patch);
7177
expect(str.view()).toBe(dst);
@@ -77,7 +83,7 @@ describe('str', () => {
7783
model.api.root({str: src});
7884
const str = model.api.str(['str']);
7985
const dst = 'Hello world!';
80-
const patch = new Diff(model).diff(str.node, dst);
86+
const patch = new JsonCrdtDiff(model).diff(str.node, dst);
8187
expect(str.view()).toBe(src);
8288
model.applyPatch(patch);
8389
expect(str.view()).toBe(dst);
@@ -91,7 +97,7 @@ describe('bin', () => {
9197
model.api.root({bin});
9298
const str = model.api.bin(['bin']);
9399
const dst = b(1, 2, 3, 4, 123, 5);
94-
const patch = new Diff(model).diff(str.node, dst);
100+
const patch = new JsonCrdtDiff(model).diff(str.node, dst);
95101
expect(patch.ops.length).toBe(1);
96102
expect(patch.ops[0].name()).toBe('ins_bin');
97103
expect((patch.ops[0] as InsStrOp).data).toEqual(b(123));
@@ -106,7 +112,7 @@ describe('bin', () => {
106112
model.api.root({bin});
107113
const str = model.api.bin(['bin']);
108114
const dst = b(1, 2, 3, 4, 5);
109-
const patch = new Diff(model).diff(str.node, dst);
115+
const patch = new JsonCrdtDiff(model).diff(str.node, dst);
110116
expect(patch.ops.length).toBe(0);
111117
});
112118

@@ -116,7 +122,7 @@ describe('bin', () => {
116122
model.api.root({bin: src});
117123
const bin = model.api.bin(['bin']);
118124
const dst = b(1, 2, 3, 4);
119-
const patch = new Diff(model).diff(bin.node, dst);
125+
const patch = new JsonCrdtDiff(model).diff(bin.node, dst);
120126
expect(patch.ops.length).toBe(1);
121127
expect(patch.ops[0].name()).toBe('del');
122128
expect(bin.view()).toEqual(src);
@@ -130,7 +136,7 @@ describe('bin', () => {
130136
model.api.root({bin: src});
131137
const bin = model.api.bin(['bin']);
132138
const dst = b(2, 3, 4, 5, 6);
133-
const patch = new Diff(model).diff(bin.node, dst);
139+
const patch = new JsonCrdtDiff(model).diff(bin.node, dst);
134140
expect(bin.view()).toEqual(src);
135141
model.applyPatch(patch);
136142
expect(bin.view()).toEqual(dst);
@@ -187,6 +193,11 @@ describe('obj', () => {
187193
model.api.root(src);
188194
assertDiff(model, model.root, dst);
189195
});
196+
197+
test('can change "str" key to number or back', () => {
198+
assertDiff2({foo: 'abc'}, {foo: 123});
199+
assertDiff2({foo: 123}, {foo: 'abc'});
200+
});
190201
});
191202

192203
describe('vec', () => {

0 commit comments

Comments
 (0)