Skip to content

Commit 04b09c9

Browse files
authored
Merge pull request #670 from streamich/patch-print
Patch to-text print fixes
2 parents 83bfcc8 + 42d175c commit 04b09c9

File tree

3 files changed

+69
-39
lines changed

3 files changed

+69
-39
lines changed

src/json-crdt-patch/Patch.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as operations from './operations';
22
import {ITimestampStruct, ts, printTs} from './clock';
33
import {SESSION} from './constants';
4+
import {printTree} from 'tree-dump/lib/printTree';
45
import {encode, decode} from './codec/binary';
56
import type {Printable} from 'tree-dump/lib/types';
67

@@ -205,11 +206,13 @@ export class Patch implements Printable {
205206
*/
206207
public toString(tab: string = ''): string {
207208
const id = this.getId();
208-
let out = `${this.constructor.name} ${id ? printTs(id) : '(nil)'}!${this.span()}`;
209-
for (let i = 0; i < this.ops.length; i++) {
210-
const isLast = i === this.ops.length - 1;
211-
out += `\n${tab}${isLast ? '└─' : '├─'} ${this.ops[i].toString(tab + (isLast ? ' ' : '│ '))}`;
212-
}
213-
return out;
209+
const header = `${this.constructor.name} ${id ? printTs(id) : '(nil)'}!${this.span()}`;
210+
return (
211+
header +
212+
printTree(
213+
tab,
214+
this.ops.map((op) => (tab) => op.toString(tab)),
215+
)
216+
);
214217
}
215218
}

src/json-crdt-patch/__demos__/PatchBuilder-json.ts

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* npx nodemon -q -x ts-node src/json-crdt-patch/__demos__/PatchBuilder-json.ts
77
*/
88

9-
import {PatchBuilder, Patch} from '..';
9+
import {PatchBuilder, Patch, schema} from '..';
1010
import {LogicalClock} from '../clock';
1111

1212
console.clear();
@@ -18,14 +18,15 @@ const json = {
1818
foo: 'bar',
1919
baz: 123,
2020
bools: [true, false],
21+
tuple: schema.vec(schema.con(123), schema.con(true)),
2122
};
2223

2324
builder.root(builder.json(json));
2425

2526
const patch = builder.flush();
2627
console.log(patch.toString());
2728

28-
// Patch 123.456!17
29+
// Patch 123.456!21
2930
// ├─ new_obj 123.456
3031
// ├─ new_str 123.457
3132
// ├─ ins_str 123.458!3, obj = 123.457 { 123.457 ← "bar" }
@@ -38,24 +39,37 @@ console.log(patch.toString());
3839
// ├─ new_con 123.467 { false }
3940
// ├─ ins_val 123.468!1, obj = 123.466, val = 123.467
4041
// ├─ ins_arr 123.469!2, obj = 123.462 { 123.462 ← 123.463, 123.466 }
41-
// ├─ ins_obj 123.471!1, obj = 123.456
42-
// │ ├─ "foo": 123.457
43-
// │ ├─ "baz": 123.461
44-
// │ └─ "bools": 123.462
45-
// └─ ins_val 123.472!1, obj = 0.0, val = 123.456
42+
// ├─ new_vec 123.471
43+
// ├─ new_con 123.472 { 123 }
44+
// ├─ new_con 123.473 { true }
45+
// ├─ ins_vec 123.474!1, obj = 123.471
46+
// │ ├─ 0: 123.472
47+
// │ └─ 1: 123.473
48+
// ├─ ins_obj 123.475!1, obj = 123.456
49+
// │ ├─ "foo": 123.457
50+
// │ ├─ "baz": 123.461
51+
// │ ├─ "bools": 123.462
52+
// │ └─ "tuple": 123.471
53+
// └─ ins_val 123.476!1, obj = 0.0, val = 123.456
4654

4755
const buf = patch.toBinary();
4856
console.log(buf);
49-
// Uint8Array(38) [
50-
// 123, 1, 200, 3, 246, 2, 4, 108, 1, 1,
51-
// 1, 1, 98, 97, 114, 0, 24, 123, 74, 1,
52-
// 0, 99, 102, 111, 111, 1, 1, 99, 98, 97,
53-
// 122, 1, 5, 9, 0, 0, 1, 0
57+
58+
// Uint8Array(95) [
59+
// 123, 200, 3, 247, 18, 16, 32, 99, 73, 7, 73, 7,
60+
// 98, 97, 114, 0, 24, 123, 48, 8, 0, 245, 72, 79,
61+
// 7, 80, 7, 8, 0, 244, 72, 82, 7, 83, 7, 114,
62+
// 78, 7, 78, 7, 79, 7, 82, 7, 24, 0, 24, 123,
63+
// 0, 245, 90, 87, 7, 0, 88, 7, 1, 89, 7, 84,
64+
// 72, 7, 99, 102, 111, 111, 73, 7, 99, 98, 97, 122,
65+
// 77, 7, 101, 98, 111, 111, 108, 115, 78, 7, 101, 116,
66+
// 117, 112, 108, 101, 87, 7, 72, 128, 0, 72, 7
5467
// ]
5568

5669
const patch2 = Patch.fromBinary(buf);
5770
console.log(patch2.toString());
58-
// Patch 123.456!17
71+
72+
// Patch 123.456!21
5973
// ├─ new_obj 123.456
6074
// ├─ new_str 123.457
6175
// ├─ ins_str 123.458!3, obj = 123.457 { 123.457 ← "bar" }
@@ -68,8 +82,15 @@ console.log(patch2.toString());
6882
// ├─ new_con 123.467 { false }
6983
// ├─ ins_val 123.468!1, obj = 123.466, val = 123.467
7084
// ├─ ins_arr 123.469!2, obj = 123.462 { 123.462 ← 123.463, 123.466 }
71-
// ├─ ins_obj 123.471!1, obj = 123.456
72-
// │ ├─ "foo": 123.457
73-
// │ ├─ "baz": 123.461
74-
// │ └─ "bools": 123.462
75-
// └─ ins_val 123.472!1, obj = 0.0, val = 123.456
85+
// ├─ new_vec 123.471
86+
// ├─ new_con 123.472 { 123 }
87+
// ├─ new_con 123.473 { true }
88+
// ├─ ins_vec 123.474!1, obj = 123.471
89+
// │ ├─ 0: 123.472
90+
// │ └─ 1: 123.473
91+
// ├─ ins_obj 123.475!1, obj = 123.456
92+
// │ ├─ "foo": 123.457
93+
// │ ├─ "baz": 123.461
94+
// │ ├─ "bools": 123.462
95+
// │ └─ "tuple": 123.471
96+
// └─ ins_val 123.476!1, obj = 0.0, val = 123.456

src/json-crdt-patch/operations.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type {IJsonCrdtPatchEditOperation, IJsonCrdtPatchOperation} from './types';
1+
import {printTree} from 'tree-dump/lib/printTree';
22
import {type ITimestampStruct, type ITimespanStruct, Timestamp, printTs} from './clock';
3+
import type {IJsonCrdtPatchEditOperation, IJsonCrdtPatchOperation} from './types';
34

45
/**
56
* Operation which creates a constant "con" data type.
@@ -22,13 +23,14 @@ export class NewConOp implements IJsonCrdtPatchOperation {
2223

2324
public toString(tab: string = ''): string {
2425
const val = this.val;
26+
const klass = 'Uint8Array';
2527
const valFormatted =
2628
val instanceof Timestamp
2729
? `{ ${printTs(val)} }`
2830
: val instanceof Uint8Array
2931
? val.length < 13
30-
? `Uint8Array { ${('' + val).replaceAll(',', ', ')} }`
31-
: `Uint8Array(${val.length})`
32+
? `${klass} { ${('' + val).replaceAll(',', ', ')} }`
33+
: `${klass}(${val.length})`
3234
: `{ ${JSON.stringify(val)} }`;
3335
return `${this.name()} ${printTs(this.id)} ${valFormatted}`;
3436
}
@@ -207,12 +209,14 @@ export class InsObjOp implements IJsonCrdtPatchEditOperation {
207209
}
208210

209211
public toString(tab: string = ''): string {
210-
let out = `${this.name()} ${printTs(this.id)}!${this.span()}, obj = ${printTs(this.obj)}`;
211-
for (let i = 0; i < this.data.length; i++) {
212-
const isLast = i === this.data.length - 1;
213-
out += `\n${tab} ${isLast ? '└─' : '├─'} ${JSON.stringify(this.data[i][0])}: ${printTs(this.data[i][1])}`;
214-
}
215-
return out;
212+
const header = `${this.name()} ${printTs(this.id)}!${this.span()}, obj = ${printTs(this.obj)}`;
213+
return (
214+
header +
215+
printTree(
216+
tab,
217+
this.data.map((item) => (tab) => `${JSON.stringify(item[0])}: ${printTs(item[1])}`),
218+
)
219+
);
216220
}
217221
}
218222

@@ -237,12 +241,14 @@ export class InsVecOp implements IJsonCrdtPatchEditOperation {
237241
}
238242

239243
public toString(tab: string = ''): string {
240-
let out = `${this.name()} ${printTs(this.id)}!${this.span()}, obj = ${printTs(this.obj)}`;
241-
for (let i = 0; i < this.data.length; i++) {
242-
const isLast = i === this.data.length - 1;
243-
out += `\n${tab} ${isLast ? '└─' : '├─'} ${JSON.stringify(this.data[i][0])}: ${printTs(this.data[i][1])}`;
244-
}
245-
return out;
244+
const header = `${this.name()} ${printTs(this.id)}!${this.span()}, obj = ${printTs(this.obj)}`;
245+
return (
246+
header +
247+
printTree(
248+
tab,
249+
this.data.map((item) => (tab) => `${item[0]}: ${printTs(item[1])}`),
250+
)
251+
);
246252
}
247253
}
248254

0 commit comments

Comments
 (0)