Skip to content

Commit cda9d78

Browse files
committed
style: 💄 fix linter issues
1 parent 414944c commit cda9d78

File tree

18 files changed

+238
-333
lines changed

18 files changed

+238
-333
lines changed

src/json-crdt-diff/JsonCrdtDiff.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {deepEqual} from '@jsonjoy.com/util/lib/json-equal/deepEqual';
22
import {cmpUint8Array} from '@jsonjoy.com/util/lib/buffers/cmpUint8Array';
3-
import {ITimespanStruct, type ITimestampStruct, Patch, PatchBuilder, Timespan} from '../json-crdt-patch';
3+
import {type ITimespanStruct, type ITimestampStruct, type Patch, PatchBuilder, Timespan} from '../json-crdt-patch';
44
import {ArrNode, BinNode, ConNode, ObjNode, StrNode, ValNode, VecNode, type JsonNode} from '../json-crdt/nodes';
55
import * as str from '../util/diff/str';
66
import * as bin from '../util/diff/bin';
@@ -26,7 +26,9 @@ export class JsonCrdtDiff {
2626
const view = src.view();
2727
if (view === dst) return;
2828
const builder = this.builder;
29-
str.apply(str.diff(view, dst), view.length,
29+
str.apply(
30+
str.diff(view, dst),
31+
view.length,
3032
(pos, txt) => builder.insStr(src.id, !pos ? src.id : src.find(pos - 1)!, txt),
3133
(pos, len) => builder.del(src.id, src.findInterval(pos, len)),
3234
);
@@ -36,15 +38,17 @@ export class JsonCrdtDiff {
3638
const view = src.view();
3739
if (cmpUint8Array(view, dst)) return;
3840
const builder = this.builder;
39-
bin.apply(bin.diff(view, dst), view.length,
41+
bin.apply(
42+
bin.diff(view, dst),
43+
view.length,
4044
(pos, txt) => builder.insBin(src.id, !pos ? src.id : src.find(pos - 1)!, txt),
4145
(pos, len) => builder.del(src.id, src.findInterval(pos, len)),
4246
);
4347
}
4448

4549
protected diffArr(src: ArrNode, dst: unknown[]): void {
4650
const srcLines: string[] = [];
47-
src.children(node => {
51+
src.children((node) => {
4852
srcLines.push(structHashCrdt(node));
4953
});
5054
const dstLines: string[] = [];
@@ -93,7 +97,11 @@ export class JsonCrdtDiff {
9397
const length = inserts.length;
9498
for (let i = 0; i < length; i++) {
9599
const [after, views] = inserts[i];
96-
builder.insArr(src.id, after, views.map(view => builder.json(view)))
100+
builder.insArr(
101+
src.id,
102+
after,
103+
views.map((view) => builder.json(view)),
104+
);
97105
}
98106
if (deletes.length) builder.del(src.id, deletes);
99107
}
@@ -102,6 +110,7 @@ export class JsonCrdtDiff {
102110
const builder = this.builder;
103111
const inserts: [key: string, value: ITimestampStruct][] = [];
104112
const srcKeys = new Set<string>();
113+
// biome-ignore lint: .forEach is fastest here
105114
src.forEach((key) => {
106115
srcKeys.add(key);
107116
const dstValue = dst[key];
@@ -123,8 +132,7 @@ export class JsonCrdtDiff {
123132
}
124133
}
125134
}
126-
inserts.push([key, src.get(key) instanceof ConNode
127-
? builder.const(dstValue) : builder.constOrJson(dstValue)]);
135+
inserts.push([key, src.get(key) instanceof ConNode ? builder.const(dstValue) : builder.constOrJson(dstValue)]);
128136
}
129137
if (inserts.length) builder.insObj(src.id, inserts);
130138
}
@@ -177,7 +185,7 @@ export class JsonCrdtDiff {
177185
public diffAny(src: JsonNode, dst: unknown): void {
178186
if (src instanceof ConNode) {
179187
const val = src.val;
180-
if ((val !== dst) && !deepEqual(src.val, dst)) throw new DiffError();
188+
if (val !== dst && !deepEqual(src.val, dst)) throw new DiffError();
181189
} else if (src instanceof StrNode) {
182190
if (typeof dst !== 'string') throw new DiffError();
183191
this.diffStr(src, dst);

src/json-crdt-diff/__tests__/JsonCrdtDiff.spec.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {JsonCrdtDiff} from '../JsonCrdtDiff';
2-
import {InsStrOp, s} from '../../json-crdt-patch';
2+
import {type InsStrOp, s} from '../../json-crdt-patch';
33
import {Model} from '../../json-crdt/model';
4-
import {JsonNode, ValNode} from '../../json-crdt/nodes';
4+
import {type 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) => {
@@ -25,9 +25,11 @@ const assertDiff2 = (src: unknown, dst: unknown) => {
2525

2626
describe('con', () => {
2727
test('binary in "con"', () => {
28-
const model = Model.create(s.obj({
29-
field: s.con(new Uint8Array([1, 2, 3])),
30-
}));
28+
const model = Model.create(
29+
s.obj({
30+
field: s.con(new Uint8Array([1, 2, 3])),
31+
}),
32+
);
3133
const dst = {
3234
field: new Uint8Array([1, 2, 3, 4]),
3335
};
@@ -376,10 +378,12 @@ describe('arr', () => {
376378

377379
describe('scenarios', () => {
378380
test('link element annotation', () => {
379-
const model = Model.create(s.obj({
380-
href: s.str('http://example.com/page?tab=1'),
381-
title: s.str('example'),
382-
}));
381+
const model = Model.create(
382+
s.obj({
383+
href: s.str('http://example.com/page?tab=1'),
384+
title: s.str('example'),
385+
}),
386+
);
383387
const dst = {
384388
href: 'https://example.com/page-2',
385389
title: 'Example page',

src/json-hash/__tests__/assertStructHash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {structHash as structHash_} from '../structHash';
22
import {structHashCrdt} from '../structHashCrdt';
33
import {Model} from '../../json-crdt';
44

5+
// biome-ignore lint: \x00 character
56
const isASCII = (str: string) => /^[\x00-\x7F]*$/.test(str);
67

78
export const assertStructHash = (json: unknown): string => {

src/json-hash/__tests__/structHash.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {clone} from '@jsonjoy.com/util/lib/json-clone';
22
import {structHash as structHash_} from '../structHash';
33
import {RandomJson} from '@jsonjoy.com/util/lib/json-random';
44

5+
// biome-ignore lint: \x00 character
56
const isASCII = (str: string) => /^[\x00-\x7F]*$/.test(str);
67

78
const structHash = (json: unknown): string => {

src/json-hash/structHash.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {sort} from '@jsonjoy.com/util/lib/sort/insertion';
2-
import {hash} from "./hash";
2+
import {hash} from './hash';
33

44
/**
55
* Produces a *structural hash* of a JSON value.
@@ -9,12 +9,13 @@ import {hash} from "./hash";
99
*
1010
* The hash is guaranteed to contain only printable ASCII characters, excluding
1111
* the newline character.
12-
*
12+
*
1313
* @param val A JSON value to hash.
1414
*/
1515
export const structHash = (val: unknown): string => {
1616
switch (typeof val) {
17-
case 'string': return hash(val).toString(36);
17+
case 'string':
18+
return hash(val).toString(36);
1819
case 'number':
1920
case 'bigint':
2021
return val.toString(36);

src/json-hash/structHashCrdt.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import {sort} from '@jsonjoy.com/util/lib/sort/insertion';
2-
import {ArrNode, BinNode, ConNode, JsonNode, ObjNode, StrNode, ValNode, VecNode} from "../json-crdt";
3-
import {hash} from "./hash";
4-
import {structHash} from "./structHash";
2+
import {ArrNode, BinNode, ConNode, type JsonNode, ObjNode, StrNode, ValNode, VecNode} from '../json-crdt';
3+
import {hash} from './hash';
4+
import {structHash} from './structHash';
55

66
/**
77
* Constructs a structural hash of the view of the node.
88
*
99
* Produces a *structural hash* of a JSON CRDT node. Works the same as
1010
* `structHash, but uses the `JsonNode` interface instead of a generic value.
11-
*
11+
*
1212
* @todo PERF: instead of constructing a "str" and "bin" view, iterate over
1313
* the RGA chunks and hash them directly.
1414
*/

src/json-patch-diff/JsonPatchDiff.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ export class JsonPatchDiff {
99

1010
protected diffVal(path: string, src: unknown, dst: unknown): void {
1111
if (deepEqual(src, dst)) return;
12-
this.patch.push({op: 'replace', path, value: dst})
12+
this.patch.push({op: 'replace', path, value: dst});
1313
}
1414

1515
protected diffStr(path: string, src: string, dst: string): void {
1616
if (src === dst) return;
1717
const patch = this.patch;
18-
str.apply(str.diff(src, dst), src.length,
18+
str.apply(
19+
str.diff(src, dst),
20+
src.length,
1921
(pos, str) => patch.push({op: 'str_ins', path, pos, str}),
2022
(pos, len, str) => patch.push({op: 'str_del', path, pos, len, str}),
2123
);
@@ -59,11 +61,12 @@ export class JsonPatchDiff {
5961
switch (type) {
6062
case line.LINE_PATCH_OP_TYPE.EQL:
6163
break;
62-
case line.LINE_PATCH_OP_TYPE.MIX:
64+
case line.LINE_PATCH_OP_TYPE.MIX: {
6365
const srcValue = src[srcIdx];
6466
const dstValue = dst[dstIdx];
6567
this.diff(pfx + srcIdx, srcValue, dstValue);
6668
break;
69+
}
6770
case line.LINE_PATCH_OP_TYPE.INS:
6871
patch.push({op: 'add', path: pfx + (srcIdx + 1), value: dst[dstIdx]});
6972
break;
@@ -77,7 +80,7 @@ export class JsonPatchDiff {
7780
public diffAny(path: string, src: unknown, dst: unknown): void {
7881
switch (typeof src) {
7982
case 'string': {
80-
if (typeof dst == 'string') this.diffStr(path, src, dst);
83+
if (typeof dst === 'string') this.diffStr(path, src, dst);
8184
else this.diffVal(path, src, dst);
8285
break;
8386
}
@@ -88,7 +91,10 @@ export class JsonPatchDiff {
8891
break;
8992
}
9093
case 'object': {
91-
if (!src || !dst || typeof dst !== 'object') return this.diffVal(path, src, dst);
94+
if (!src || !dst || typeof dst !== 'object') {
95+
this.diffVal(path, src, dst);
96+
return;
97+
}
9298
if (Array.isArray(src)) {
9399
if (Array.isArray(dst)) this.diffArr(path, src, dst);
94100
else this.diffVal(path, src, dst);

src/json-patch-diff/__tests__/JsonPatchDiff.spec.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,15 @@ describe('obj', () => {
9292
nested: {
9393
id: 2,
9494
name: 'world',
95-
description: 'blablabla'
95+
description: 'blablabla',
9696
},
9797
};
9898
const dst = {
9999
id: 3,
100100
name: 'hello!',
101101
nested: {
102102
id: 2,
103-
description: 'Please dont use "blablabla"'
103+
description: 'Please dont use "blablabla"',
104104
},
105105
};
106106
assertDiff(src, dst);
@@ -192,14 +192,8 @@ describe('arr', () => {
192192
});
193193

194194
test('fuzzer - 1', () => {
195-
const src: unknown[] = [
196-
11, 10, 4, 6,
197-
3, 1, 5
198-
];
199-
const dst: unknown[] = [
200-
7, 3, 13, 7, 9,
201-
9, 9, 4, 9
202-
];
195+
const src: unknown[] = [11, 10, 4, 6, 3, 1, 5];
196+
const dst: unknown[] = [7, 3, 13, 7, 9, 9, 9, 4, 9];
203197
assertDiff(src, dst);
204198
});
205199
});
@@ -220,7 +214,7 @@ test('array of objects diff', () => {
220214
id: 'xxxx',
221215
name: 'Music',
222216
description: 'I love music',
223-
}
217+
},
224218
];
225219
const dst = [
226220
{
@@ -259,16 +253,16 @@ test('complex case', () => {
259253
id: 'xxxx',
260254
name: 'Music',
261255
description: 'I love music',
262-
}
256+
},
263257
],
264258
address: {
265259
city: 'New York',
266260
state: 'NY',
267261
zip: '10001',
268262
location: {
269263
lat: 40.7128,
270-
lng: -74.0060,
271-
}
264+
lng: -74.006,
265+
},
272266
},
273267
};
274268
const dst = {
@@ -296,7 +290,7 @@ test('complex case', () => {
296290
location: {
297291
lat: 40.7128,
298292
lng: 123.4567,
299-
}
293+
},
300294
},
301295
};
302296
assertDiff(src, dst);

src/json-patch-diff/__tests__/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const assertDiff = (src: unknown, dst: unknown) => {
1010
const {doc: res} = applyPatch(srcNested, patch1, {mutate: false});
1111
// console.log(res);
1212
expect(res).toEqual({src: dst});
13-
const patch2 = new JsonPatchDiff().diff('/src', (res as any)['src'], dst);
13+
const patch2 = new JsonPatchDiff().diff('/src', (res as any).src, dst);
1414
// console.log(patch2);
1515
expect(patch2.length).toBe(0);
1616
};

src/util/diff/__tests__/bin-fuzz.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import {toBuf} from '@jsonjoy.com/util/lib/buffers/toBuf';
33
import {assertPatch} from './util';
44
import * as bin from '../bin';
55

6-
const str = () => Math.random() > .7
7-
? RandomJson.genString(Math.ceil(Math.random() * 200))
8-
: Math.random().toString(36).slice(2);
6+
const str = () =>
7+
Math.random() > 0.7 ? RandomJson.genString(Math.ceil(Math.random() * 200)) : Math.random().toString(36).slice(2);
98
const iterations = 100;
109

1110
test('fuzzing diff()', () => {

0 commit comments

Comments
 (0)