From 91f99603d306a09e67551060a3d9d3ae7f53ab02 Mon Sep 17 00:00:00 2001 From: Tomas Zijdemans Date: Tue, 8 Sep 2026 19:54:09 +0200 Subject: [PATCH 1/2] fix(data-structures): support array-like inputs in RedBlackTree.from() --- data_structures/red_black_tree.ts | 2 +- data_structures/red_black_tree_test.ts | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/data_structures/red_black_tree.ts b/data_structures/red_black_tree.ts index c853d52399d3..c9c2b964409b 100644 --- a/data_structures/red_black_tree.ts +++ b/data_structures/red_black_tree.ts @@ -267,7 +267,7 @@ export class RedBlackTree extends BinarySearchTree { } const values: Iterable = options?.map ? Array.from(unmappedValues, options.map, options.thisArg) - : unmappedValues as U[]; + : Array.from(unmappedValues as ArrayLike | Iterable); for (const value of values) result.insert(value); return result; } diff --git a/data_structures/red_black_tree_test.ts b/data_structures/red_black_tree_test.ts index 0e07f2c4f14b..00ebb9b7dcde 100644 --- a/data_structures/red_black_tree_test.ts +++ b/data_structures/red_black_tree_test.ts @@ -312,6 +312,26 @@ Deno.test("RedBlackTree works with object items", () => { assertEquals(tree.isEmpty(), true); }); +Deno.test("RedBlackTree.from() handles empty array-like objects", () => { + const tree = RedBlackTree.from({ length: 0 }); + assertEquals([...tree], []); + assertEquals(tree.size, 0); +}); + +Deno.test("RedBlackTree.from() handles array-like objects", () => { + const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 }; + const tree = RedBlackTree.from(values); + assertEquals([...tree], [1, 2, 3]); + assertEquals(tree.size, 3); +}); + +Deno.test("RedBlackTree.from() handles array-like objects with a custom comparator", () => { + const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 }; + const tree = RedBlackTree.from(values, { compare: descend }); + assertEquals([...tree], [3, 2, 1]); + assertEquals(tree.size, 3); +}); + Deno.test("RedBlackTree.from() handles Iterable", () => { const values: number[] = [-10, 9, -1, 100, 9, 1, 0, 9, -100, 10, -9]; const originalValues: number[] = Array.from(values); From 6c5286a97db135d2ecd75e407ceb14d881caa0b7 Mon Sep 17 00:00:00 2001 From: Tomas Zijdemans Date: Tue, 8 Sep 2026 20:04:53 +0200 Subject: [PATCH 2/2] fix(data-structures): apply array-like fix to BinarySearchTree.from() and pass Deno 1.x type-check --- data_structures/binary_search_tree.ts | 2 +- data_structures/binary_search_tree_test.ts | 20 ++++++++++++++++++++ data_structures/red_black_tree.ts | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/data_structures/binary_search_tree.ts b/data_structures/binary_search_tree.ts index a3fedffa8bf6..dccf0e39f546 100644 --- a/data_structures/binary_search_tree.ts +++ b/data_structures/binary_search_tree.ts @@ -295,7 +295,7 @@ export class BinarySearchTree implements Iterable { } const values: Iterable = options?.map ? Array.from(unmappedValues, options.map, options.thisArg) - : unmappedValues as U[]; + : Array.from(unmappedValues) as unknown as U[]; for (const value of values) result.insert(value); return result; } diff --git a/data_structures/binary_search_tree_test.ts b/data_structures/binary_search_tree_test.ts index 887fda5c32f7..ce7b78beaf44 100644 --- a/data_structures/binary_search_tree_test.ts +++ b/data_structures/binary_search_tree_test.ts @@ -317,6 +317,26 @@ Deno.test("BinarySearchTree contains objects", () => { assertEquals(tree.isEmpty(), true); }); +Deno.test("BinarySearchTree.from() handles empty array-like objects", () => { + const tree = BinarySearchTree.from({ length: 0 }); + assertEquals([...tree], []); + assertEquals(tree.size, 0); +}); + +Deno.test("BinarySearchTree.from() handles array-like objects", () => { + const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 }; + const tree = BinarySearchTree.from(values); + assertEquals([...tree], [1, 2, 3]); + assertEquals(tree.size, 3); +}); + +Deno.test("BinarySearchTree.from() handles array-like objects with a custom comparator", () => { + const values = { 0: 3, 1: 1, 2: 3, 3: 2, length: 4 }; + const tree = BinarySearchTree.from(values, { compare: descend }); + assertEquals([...tree], [3, 2, 1]); + assertEquals(tree.size, 3); +}); + Deno.test("BinarySearchTree.from() handles iterable", () => { const values: number[] = [-10, 9, -1, 100, 9, 1, 0, 9, -100, 10, -9]; const originalValues: number[] = Array.from(values); diff --git a/data_structures/red_black_tree.ts b/data_structures/red_black_tree.ts index c9c2b964409b..e8a971290915 100644 --- a/data_structures/red_black_tree.ts +++ b/data_structures/red_black_tree.ts @@ -267,7 +267,7 @@ export class RedBlackTree extends BinarySearchTree { } const values: Iterable = options?.map ? Array.from(unmappedValues, options.map, options.thisArg) - : Array.from(unmappedValues as ArrayLike | Iterable); + : Array.from(unmappedValues) as unknown as U[]; for (const value of values) result.insert(value); return result; }