Skip to content

Commit 8e1b06a

Browse files
Copilotstreamich
andcommitted
Replace obscure ~~ operators with clearer Math.trunc for better code readability
Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
1 parent b4cebee commit 8e1b06a

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/json-crdt/model/api/nodes.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export class NodeApi<N extends JsonNode = JsonNode> implements Printable {
234234
if (typeof key === 'number') index = key;
235235
else if (key === '-') index = length;
236236
else {
237-
index = ~~key;
237+
index = Math.trunc(Number(key));
238238
if (index + '' !== key) break ADD;
239239
}
240240
if (Number.isNaN(index)) break ADD;
@@ -247,7 +247,7 @@ export class NodeApi<N extends JsonNode = JsonNode> implements Printable {
247247
node.ins(index, value);
248248
}
249249
} else if (node instanceof VecApi) {
250-
node.set([[~~key, value]]);
250+
node.set([[Math.trunc(Number(key)), value]]);
251251
} else break ADD;
252252
return true;
253253
} catch {}
@@ -267,13 +267,13 @@ export class NodeApi<N extends JsonNode = JsonNode> implements Printable {
267267
let index: number = 0;
268268
if (typeof key === 'number') index = key;
269269
else {
270-
index = ~~key;
270+
index = Math.trunc(Number(key));
271271
if (index + '' !== key) break REPLACE;
272272
}
273273
if (Number.isNaN(index) || index < 0 || index > length) break REPLACE;
274274
if (index === length) node.ins(index, [value]);
275275
else node.upd(index, value);
276-
} else if (node instanceof VecApi) node.set([[~~key, value]]);
276+
} else if (node instanceof VecApi) node.set([[Math.trunc(Number(key)), value]]);
277277
else break REPLACE;
278278
return true;
279279
} catch {}
@@ -294,13 +294,13 @@ export class NodeApi<N extends JsonNode = JsonNode> implements Printable {
294294
if (typeof key === 'number') index = key;
295295
else if (key === '-') index = length;
296296
else {
297-
index = ~~key;
297+
index = Math.trunc(Number(key));
298298
if (index + '' !== key) break REMOVE;
299299
}
300300
if (Number.isNaN(index) || index < 0 || index > len) break REMOVE;
301301
node.del(index, Math.min(length, len - index));
302302
} else if (node instanceof VecApi) {
303-
node.set([[~~key, void 0]]);
303+
node.set([[Math.trunc(Number(key)), void 0]]);
304304
} else break REMOVE;
305305
return true;
306306
} catch {}

0 commit comments

Comments
 (0)