Skip to content

Commit 32c8288

Browse files
committed
feat(noneNull): add noneNull function
1 parent 03af30c commit 32c8288

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

index.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
maximumBy,
3131
minimum,
3232
minimumBy,
33+
noneNull,
3334
notEmpty,
3435
only,
3536
or,
@@ -352,3 +353,10 @@ test("concatMap", async t => {
352353
"6"
353354
]);
354355
});
356+
357+
test("noneNull", async t => {
358+
t.deepEqual(await noneNull(asyncIterable([1, 2, 3])), [1, 2, 3]);
359+
t.is(await noneNull(asyncIterable([1, null, 3])), null);
360+
t.is(await noneNull(asyncIterable([undefined, 2, 3])), null);
361+
t.deepEqual(await noneNull(asyncIterable([])), []);
362+
});

index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,3 +921,18 @@ export function concatMapFn<T, U>(
921921
}
922922

923923
export const asyncConcatMapFn = concatMapFn;
924+
925+
export async function noneNull<T>(
926+
iterable: AsyncIterableLike<T | null | undefined>
927+
): Promise<T[] | null> {
928+
const result: T[] = [];
929+
930+
for await (const element of await iterable) {
931+
if (element == null) {
932+
return null;
933+
}
934+
result.push(element);
935+
}
936+
937+
return result;
938+
}

0 commit comments

Comments
 (0)