Skip to content

Commit 3583a14

Browse files
authored
Merge pull request #467 from streamich/bin-to-str
Better octet textual display
2 parents b2ac900 + 7381b50 commit 3583a14

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

src/json-crdt/__demos__/docs-bin.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,52 @@ console.clear();
1313
const model = Model.withLogicalClock(1234); // 1234 is session ID
1414

1515
model.api.root({
16-
blob: new Uint8Array([1, 2, 3]),
16+
blob: new Uint8Array([1, 2, 3, 14, 15, 16, 17]),
1717
});
1818

1919
console.log(model.view());
20-
// { blob: Uint8Array(3) [ 1, 2, 3 ] }
20+
// {
21+
// blob: Uint8Array(7) [
22+
// 1, 2, 3, 14,
23+
// 15, 16, 17
24+
// ]
25+
// }
2126

2227
console.log(model.root + '');
23-
// RootNode 0.0
24-
// └─ ObjNode 1234.1
28+
// root 0.0
29+
// └─ obj 1234.1
2530
// └─ "blob"
26-
// └─ BinNode 1234.2 { 1, 2, 3 }
27-
// └─ BinChunk 1234.3!3 len:3 { 1, 2, 3 }
31+
// └─ bin 1234.2 { 01 02 03 0e 0f 10 11 }
32+
// └─ BinChunk 1234.3!7 len:7 { 01 02 03 0e 0f 10 11 }
2833

2934
// Retrieve node at path ['blob'] as "bin" type.
3035
const blob = model.api.bin(['blob']);
3136
console.log(blob + '');
3237
// BinApi
33-
// └─ BinNode 1234.2 { 1, 2, 3 }
34-
// └─ BinChunk 1234.3!3 len:3 { 1, 2, 3 }
38+
// └─ bin 1234.2 { 01 02 03 0e 0f 10 11 }
39+
// └─ BinChunk 1234.3!7 len:7 { 01 02 03 0e 0f 10 11 }
3540

3641
blob.ins(3, new Uint8Array([4, 5]));
3742
console.log(blob + '');
3843
// BinApi
39-
// └─ BinNode 1234.2 { 1, 2, 3, 4, 5 }
40-
// └─ BinChunk 1234.8!2 len:5 { 4, 5 }
41-
// ← BinChunk 1234.3!3 len:3 { 1, 2, 3 }
44+
// └─ bin 1234.2 { 01 02 03 04 05 0e 0f 10 11 }
45+
// └─ BinChunk 1234.12!2 len:9 { 04 05 }
46+
// ← BinChunk 1234.3!3 len:3 { 01 02 03 }
47+
// → BinChunk 1234.6!4 len:4 { 0e 0f 10 11 }
4248

4349
blob.del(2, 1);
4450
console.log(blob + '');
4551
// BinApi
46-
// └─ BinNode 1234.2 { 1, 2, 4, 5 }
47-
// └─ BinChunk 1234.8!2 len:4 { 4, 5 }
48-
// ← BinChunk 1234.3!2 len:2 { 1, 2 }
52+
// └─ bin 1234.2 { 01 02 04 05 0e 0f 10 11 }
53+
// └─ BinChunk 1234.12!2 len:8 { 04 05 }
54+
// ← BinChunk 1234.3!2 len:2 { 01 02 }
4955
// → BinChunk 1234.5!1 len:0 [1]
56+
// → BinChunk 1234.6!4 len:4 { 0e 0f 10 11 }
5057

5158
console.log(model.view());
52-
// { blob: Uint8Array(4) [ 1, 2, 4, 5 ] }
59+
// {
60+
// blob: Uint8Array(8) [
61+
// 1, 2, 4, 5,
62+
// 14, 15, 16, 17
63+
// ]
64+
// }

src/json-crdt/nodes/rga/AbstractRga.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ import {isUint8Array} from '../../../util/buffers/isUint8Array';
1212
import {rSplay, lSplay, llSplay, rrSplay, lrSplay, rlSplay} from '../../../util/trees/splay/util';
1313
import {splay2} from '../../../util/trees/splay/util2';
1414
import {insert2, remove2} from '../../../util/trees/util2';
15-
import {printBinary} from '../../../util/print/printBinary';
16-
import {printTree} from '../../../util/print/printTree';
1715
import {ORIGIN} from '../../../json-crdt-patch/constants';
16+
import {printTree} from '../../../util/print/printTree';
17+
import {printBinary} from '../../../util/print/printBinary';
18+
import {printOctets} from '../../../util/buffers/printOctets';
1819

1920
/**
2021
* @category CRDT Node
@@ -876,7 +877,7 @@ export abstract class AbstractRga<T> {
876877
public toString(tab: string = ''): string {
877878
const view = this.view();
878879
let value = '';
879-
if (isUint8Array(view)) value += ` { ${('' + view).replaceAll(',', ', ')} }`;
880+
if (isUint8Array(view)) value += ` { ${printOctets(view) || '∅'} }`;
880881
else if (typeof view === 'string')
881882
value += `{ ${view.length > 32 ? JSON.stringify(view.substring(0, 32)) + ' …' : JSON.stringify(view)} }`;
882883
const header = `${this.toStringName()} ${toDisplayString(this.id)} ${value}`;
@@ -898,7 +899,7 @@ export abstract class AbstractRga<T> {
898899
let str = `${chunk.constructor.name} ${id}!${chunk.span} len:${chunk.len}`;
899900
if (chunk.del) str += ` [${chunk.span}]`;
900901
else {
901-
if (isUint8Array(chunk.data)) str += ` { ${chunk.data.toString().replaceAll(',', ', ')} }`;
902+
if (isUint8Array(chunk.data)) str += ` { ${printOctets(chunk.data) || '∅'} }`;
902903
else if (typeof chunk.data === 'string') {
903904
const data =
904905
chunk.data.length > 32 ? JSON.stringify(chunk.data.substring(0, 32)) + ' …' : JSON.stringify(chunk.data);

src/util/buffers/printOctets.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const printOctets = (octets: Uint8Array, max: number = 16): string => {
2+
let str = '';
3+
if (!octets.length) return str;
4+
if (octets[0] < 16) str += '0';
5+
str += octets[0].toString(16);
6+
for (let i = 1; i < octets.length && i < max; i++) {
7+
const n = octets[i];
8+
str += ' ';
9+
if (n < 16) str += '0';
10+
str += n.toString(16);
11+
}
12+
if (octets.length > max) str += `… (${octets.length - max} more)`;
13+
return str;
14+
};

0 commit comments

Comments
 (0)