diff --git a/src/util.ts b/src/util.ts index 4ecfab7..71163df 100644 --- a/src/util.ts +++ b/src/util.ts @@ -59,14 +59,16 @@ export function* readArray( } } +const sentinel = Symbol(); + export function* ensureSortedSet( iterable: Iterable, comparator: (a: T, b: T) => number, ): Generator { - let previous: T | null = null; + let previous: T | typeof sentinel = sentinel; for (const element of iterable) { - if (previous != null && comparator(element, previous) <= 0) { + if (previous !== sentinel && comparator(previous, element) >= 0) { throw new Error("iterable is not a sorted set."); } @@ -79,10 +81,10 @@ export async function* ensureSortedSetAsync( iterable: AsyncIterable | Iterable, comparator: (a: T, b: T) => number, ): AsyncGenerator { - let previous: T | null = null; + let previous: T | typeof sentinel = sentinel; for await (const element of iterable) { - if (previous != null && comparator(element, previous) <= 0) { + if (previous !== sentinel && comparator(previous, element) >= 0) { throw new Error("async-iterable is not a sorted set."); }