Skip to content

Commit 8ef4d35

Browse files
committed
feat(excludeFirst): add excludeFirst function
1 parent 7e1c9f1 commit 8ef4d35

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-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
empty,
77
equal,
88
exclude,
9+
excludeFirst,
910
filter,
1011
initial,
1112
last,
@@ -158,3 +159,10 @@ test("exclude", async t => {
158159
[3, 4, 3]
159160
);
160161
});
162+
163+
test("excludeFirst", async t => {
164+
t.deepEqual(
165+
await toArray(excludeFirst(asyncIterable([1, 2, 3, 4, 3, 2, 1]), n => n > 2)),
166+
[1, 2, 4, 3, 2, 1]
167+
);
168+
});

index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,38 @@ export function excludeFn<T>(
453453
}
454454

455455
export const asyncExcludeFn = exclude;
456+
457+
export async function* excludeFirst<T>(
458+
iterable: AsyncIterableLike<T>,
459+
predicate: (element: T, index: number) => boolean | Promise<boolean>
460+
): AsyncIterable<T> {
461+
const iterator = asyncIterator(iterable);
462+
let result = await iterator.next();
463+
464+
for (let i = 0; result.done !== true; ++i) {
465+
if (await predicate(result.value, i)) {
466+
break;
467+
}
468+
yield result.value;
469+
result = await iterator.next();
470+
}
471+
472+
if (result.done !== true) {
473+
result = await iterator.next();
474+
}
475+
476+
while (result.done !== true) {
477+
yield result.value;
478+
result = await iterator.next();
479+
}
480+
}
481+
482+
export const asyncExcludeFirst = excludeFirst;
483+
484+
export function excludeFirstFn<T>(
485+
predicate: (element: T, index: number) => boolean | Promise<boolean>
486+
): (iterable: AsyncIterableLike<T>) => AsyncIterable<T> {
487+
return iterable => excludeFirst(iterable, predicate);
488+
}
489+
490+
export const asyncExcludeFirstFn = excludeFirstFn;

0 commit comments

Comments
 (0)