Skip to content

Commit 5926307

Browse files
committed
fix(util): ensureSortedSet handles null iterable
1 parent 95597cf commit 5926307

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/util.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ export function* readArray<T>(
5959
}
6060
}
6161

62+
const sentinel = Symbol();
63+
6264
export function* ensureSortedSet<T>(
6365
iterable: Iterable<T>,
6466
comparator: (a: T, b: T) => number,
6567
): Generator<T> {
66-
let previous: T | null = null;
68+
let previous: T | typeof sentinel = sentinel;
6769

6870
for (const element of iterable) {
69-
if (previous != null && comparator(element, previous) <= 0) {
71+
if (previous !== sentinel && comparator(previous, element) >= 0) {
7072
throw new Error("iterable is not a sorted set.");
7173
}
7274

@@ -79,10 +81,10 @@ export async function* ensureSortedSetAsync<T>(
7981
iterable: AsyncIterable<T> | Iterable<T>,
8082
comparator: (a: T, b: T) => number,
8183
): AsyncGenerator<T> {
82-
let previous: T | null = null;
84+
let previous: T | typeof sentinel = sentinel;
8385

8486
for await (const element of iterable) {
85-
if (previous != null && comparator(element, previous) <= 0) {
87+
if (previous !== sentinel && comparator(previous, element) >= 0) {
8688
throw new Error("async-iterable is not a sorted set.");
8789
}
8890

0 commit comments

Comments
 (0)