Skip to content

Commit aec9537

Browse files
committed
feat(asyncExcludeOnce): add asyncExcludeOnce function
1 parent 25bcb37 commit aec9537

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

index.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
asyncDropWhileOnce,
77
asyncEmptyOnce,
88
asyncEqualOnce,
9+
asyncExcludeOnce,
910
asyncFilterOnce,
1011
asyncInitialOnce,
1112
asyncIterator,
@@ -363,3 +364,10 @@ test("asyncFilterOnce", async t => {
363364
[1, 2, 5]
364365
);
365366
});
367+
368+
test("asyncExcludeOnce", async t => {
369+
t.deepEqual(
370+
await asyncToArrayOnce(asyncExcludeOnce(asyncIterator([1, 2, 3, 4, 3, 2, 1]), n => n < 3)),
371+
[3, 4, 3]
372+
);
373+
});

index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,26 @@ export function asyncFilterOnceFn<T>(
543543
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
544544
return iterator => asyncFilterOnce(iterator, predicate);
545545
}
546+
547+
export function asyncExcludeOnce<T>(
548+
iterator: AsyncIteratorLike<T>,
549+
predicate: (element: T, index: number) => boolean | Promise<boolean>
550+
): AsyncIterator<T> {
551+
const it = asyncIterator(iterator);
552+
let i = 0;
553+
return {
554+
next: async () => {
555+
let element = await it.next();
556+
while (element.done !== true && (await predicate(element.value, i++))) {
557+
element = await it.next();
558+
}
559+
return element;
560+
}
561+
};
562+
}
563+
564+
export function asyncExcludeOnceFn<T>(
565+
predicate: (element: T, index: number) => boolean | Promise<boolean>
566+
): (iterator: AsyncIteratorLike<T>) => AsyncIterator<T> {
567+
return iterator => asyncExcludeOnce(iterator, predicate);
568+
}

0 commit comments

Comments
 (0)