Skip to content
This repository was archived by the owner on Nov 9, 2025. It is now read-only.

Commit 4930b45

Browse files
authored
Merge 8fcf75b into 3bd64ce
2 parents 3bd64ce + 8fcf75b commit 4930b45

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/sorted-array.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,23 @@ export class SortedArray<T> implements Array<T> {
415415
// INTERFACE METHODS
416416
public toString(): string {
417417
this._sort();
418-
return this.unwrap().toString();
418+
const unwrapped = this.unwrap();
419+
for (let i = 0; i < unwrapped.length; i++) {
420+
if (unwrapped[i] as any === this) {
421+
unwrapped[i] = "[Circular]" as unknown as T;
422+
}
423+
}
424+
return unwrapped.toString();
419425
}
420426
public toLocaleString(): string {
421427
this._sort();
422-
return this.unwrap().toLocaleString();
428+
const unwrapped = this.unwrap();
429+
for (let i = 0; i < unwrapped.length; i++) {
430+
if (unwrapped[i] as any === this) {
431+
unwrapped[i] = "[Circular]" as unknown as T;
432+
}
433+
}
434+
return unwrapped.toLocaleString();
423435
}
424436
public pop(): T {
425437
this._sort();

test/test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,22 @@ describe("SortedArray", function() {
395395
const sa = newsa();
396396
deepStrictEqual(sa.toString(), expected.toString());
397397
});
398+
it("should not error in a circular array", function() {
399+
const sa = new SortedArray<SortedArray<any>>(() => 0);
400+
sa.add(sa);
401+
strictEqual(sa.toString(), "[Circular]");
402+
});
398403
});
399404
describe("#toLocaleString", function() {
400405
it("should mirror the functionality of Array#toLocaleString", function() {
401406
const sa = newsa();
402407
deepStrictEqual(sa.toLocaleString(), expected.toLocaleString());
403408
});
409+
it("should not error in a circular array", function() {
410+
const sa = new SortedArray<SortedArray<any>>(() => 0);
411+
sa.add(sa);
412+
strictEqual(sa.toLocaleString(), "[Circular]");
413+
});
404414
});
405415
describe("#pop", function() {
406416
it("should remove and return the last element", function() {

0 commit comments

Comments
 (0)