Skip to content

Commit 5927e6c

Browse files
committed
feat(asyncNoneNullOnce): add asyncNoneNullOnce function
1 parent d295b46 commit 5927e6c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

index.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
asyncMaximumOnce,
3333
asyncMinimumByOnce,
3434
asyncMinimumOnce,
35+
asyncNoneNullOnce,
3536
asyncNotEmptyOnce,
3637
asyncNotEqualOnce,
3738
asyncOnlyOnce,
@@ -581,3 +582,10 @@ test("asyncConcatMapOnce", async t => {
581582
["1", "2", "3", "4", "5", "6"]
582583
);
583584
});
585+
586+
test("asyncNoneNullOnce", async t => {
587+
t.deepEqual(await asyncNoneNullOnce(asyncIterator([1, 2, 3])), [1, 2, 3]);
588+
t.is(await asyncNoneNullOnce(asyncIterator([1, null, 3])), null);
589+
t.is(await asyncNoneNullOnce(asyncIterator([undefined, 2, 3])), null);
590+
t.deepEqual(await asyncNoneNullOnce(asyncIterator([])), []);
591+
});

index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,3 +1054,20 @@ export function asyncConcatMapOnceFn<T, U>(
10541054
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<U> {
10551055
return iterator => asyncConcatMapOnce(iterator, f);
10561056
}
1057+
1058+
export async function asyncNoneNullOnce<T>(
1059+
iterator: AsyncIteratorLike<T | null | undefined>
1060+
): Promise<T[] | null> {
1061+
const array: T[] = [];
1062+
const it = asyncIterator(iterator);
1063+
let element = await it.next();
1064+
while (element.done !== true) {
1065+
if (element.value == null) {
1066+
return null;
1067+
} else {
1068+
array.push(element.value);
1069+
}
1070+
element = await it.next();
1071+
}
1072+
return array;
1073+
}

0 commit comments

Comments
 (0)