Skip to content

Commit 4f8a645

Browse files
committed
feat(asyncMaximumOnce): add asyncMaximumOnce function
1 parent acf5e82 commit 4f8a645

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
asyncIterator,
2222
asyncLastOnce,
2323
asyncMapOnce,
24+
asyncMaximumOnce,
2425
asyncNotEmptyOnce,
2526
asyncNotEqualOnce,
2627
asyncOnlyOnce,
@@ -443,3 +444,9 @@ test("asyncFindIndexOnce", async t => {
443444
test("asyncFindOnce", async t => {
444445
t.is(await asyncFindOnce(asyncIterator([1, 2, 3, 4, 3, 2, 1]), n => n >= 3), 3);
445446
});
447+
448+
test("asyncMaximumOnce", async t => {
449+
t.is(await asyncMaximumOnce(asyncIterator([1, 2, 3])), 3);
450+
t.is(await asyncMaximumOnce(asyncIterator([1, 2, 3, 4, 3, 2, 1])), 4);
451+
t.is(await asyncMaximumOnce(asyncIterator([])), null);
452+
});

index.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import {equal as defaultEqual} from "@softwareventures/ordered";
1+
import type {Comparator} from "@softwareventures/ordered";
2+
import {compare as defaultCompare, equal as defaultEqual} from "@softwareventures/ordered";
23
import type {AsyncIterableLike} from "@softwareventures/async-iterable";
34
import {asyncIterable} from "@softwareventures/async-iterable";
45
import {hasProperty} from "unknown";
@@ -801,3 +802,49 @@ export function asyncFindOnceFn<T>(
801802
): (iterator: AsyncIteratorLike<T>) => Promise<T | null> {
802803
return async iterator => asyncFindOnce(iterator, predicate);
803804
}
805+
806+
export async function asyncMaximumOnce<T extends string | number | boolean>(
807+
iterator: AsyncIteratorLike<T>
808+
): Promise<T | null>;
809+
export async function asyncMaximumOnce<T>(
810+
iterator: AsyncIteratorLike<T>,
811+
compare: Comparator<T>
812+
): Promise<T | null>;
813+
export async function asyncMaximumOnce<T>(
814+
iterator: AsyncIteratorLike<T>,
815+
compare?: Comparator<T>
816+
): Promise<T | null> {
817+
return internalAsyncMaximumOnce(
818+
iterator,
819+
compare ?? (defaultCompare as unknown as Comparator<T>)
820+
);
821+
}
822+
823+
export function asyncMaximumOnceFn<T>(
824+
compare: Comparator<T>
825+
): (iterator: AsyncIteratorLike<T>) => Promise<T | null> {
826+
return async iterator => internalAsyncMaximumOnce(iterator, compare);
827+
}
828+
829+
async function internalAsyncMaximumOnce<T>(
830+
iterator: AsyncIteratorLike<T>,
831+
compare: Comparator<T>
832+
): Promise<T | null> {
833+
const it = asyncIterator(iterator);
834+
let element = await it.next();
835+
836+
if (element.done === true) {
837+
return null;
838+
}
839+
840+
let max = element.value;
841+
element = await it.next();
842+
while (element.done !== true) {
843+
if (compare(element.value, max) > 0) {
844+
max = element.value;
845+
}
846+
element = await it.next();
847+
}
848+
849+
return max;
850+
}

0 commit comments

Comments
 (0)