Skip to content

Commit d16828f

Browse files
authored
fix: user Array.isArray() for more reliable array check
Fix critical bugs and anti-patterns: parameter reassignment, unsafe non-null assertions, and unclear type checks
2 parents 3671ea9 + 7cb89fc commit d16828f

File tree

5 files changed

+6
-6
lines changed

5 files changed

+6
-6
lines changed

src/json-crdt-patch/codec/binary/Decoder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export class Decoder extends CborDecoder<CrdtReader> {
4949
this.patchSid = clock.sid;
5050
const builder = (this.builder = new PatchBuilder(clock));
5151
const map = this.val();
52-
if (map instanceof Array) builder.patch.meta = map[0];
52+
if (Array.isArray(map)) builder.patch.meta = map[0];
5353
this.decodeOperations();
5454
return builder.patch;
5555
}

src/json-crdt-patch/codec/compact/encode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const timestamp = (sid: number, ts: ITimestampStruct): types.CompactCodecTimesta
1212

1313
const timespan = (sid: number, span: ITimespanStruct): types.CompactCodecTimespan => {
1414
const ts = timestamp(sid, span);
15-
if (ts instanceof Array) {
15+
if (Array.isArray(ts)) {
1616
ts.push(span.span);
1717
return ts;
1818
}

src/json-crdt/json-patch/JsonPatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class JsonPatch<N extends JsonNode = JsonNode<any>> {
160160
if (!(node instanceof StrNode)) throw new Error('NOT_FOUND');
161161
const length = node.length();
162162
if (length <= pos) return;
163-
const deletionLength = Math.min(len ?? str!.length, length - pos);
163+
const deletionLength = Math.min(len ?? str.length, length - pos);
164164
const range = node.findInterval(pos, deletionLength);
165165
if (!range) throw new Error('OUT_OF_BOUNDS');
166166
this.builder().del(node.id, range);

src/json-size/msgpackSizeFast.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const msgpackSizeFast = (value: unknown): number => {
3434
case 'boolean':
3535
return 1;
3636
}
37-
if (value instanceof Array) return arraySize(value);
37+
if (Array.isArray(value)) return arraySize(value);
3838
if (isUint8Array(value)) return 5 + value.length;
3939
if (value instanceof JsonPackValue) return (value as JsonPackValue).val.length;
4040
if (value instanceof JsonPackExtension) return 6 + (value as JsonPackExtension).val.length;

src/json-text/toTree.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {stringify} from './stringify';
44
const isPrimitive = (value: unknown): boolean => typeof value !== 'object' || value === null;
55
const isOneLineValue = (value: unknown): boolean => {
66
if (isPrimitive(value)) return true;
7-
if (value instanceof Array && !value.length) return true;
7+
if (Array.isArray(value) && !value.length) return true;
88
if (value && typeof value === 'object' && !Object.keys(value).length) return true;
99
return false;
1010
};
1111
const isSimpleString = (str: string) => /^[a-z0-9]+$/i.test(str);
1212

1313
export const toTree = (value: unknown, tab: string = ''): string => {
14-
if (value instanceof Array) {
14+
if (Array.isArray(value)) {
1515
if (value.length === 0) return '[]';
1616
return printTree(
1717
tab,

0 commit comments

Comments
 (0)