Skip to content

Commit 84d464c

Browse files
committed
feat(asyncPrefixMatchOnce): add asyncPrefixMatchOnce function
1 parent 101390c commit 84d464c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
asyncNotEmptyOnce,
1313
asyncNotEqualOnce,
1414
asyncOnlyOnce,
15+
asyncPrefixMatchOnce,
1516
asyncPushOnce,
1617
asyncSliceOnce,
1718
asyncTailOnce,
@@ -326,3 +327,11 @@ test("asyncNotEqualOnce", async t => {
326327
)
327328
);
328329
});
330+
331+
test("asyncPrefixMatchOnce", async t => {
332+
t.true(await asyncPrefixMatchOnce(asyncIterator([]), asyncIterator([])));
333+
t.true(await asyncPrefixMatchOnce(asyncIterator([1, 2, 3]), asyncIterator([])));
334+
t.true(await asyncPrefixMatchOnce(asyncIterator([1, 2, 3, 4]), asyncIterator([1, 2])));
335+
t.false(await asyncPrefixMatchOnce(asyncIterator([1, 3, 4]), asyncIterator([1, 2])));
336+
t.false(await asyncPrefixMatchOnce(asyncIterator([]), asyncIterator([1])));
337+
});

index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,3 +461,21 @@ export function asyncNotEqualOnceFn<T>(
461461
): (a: AsyncIteratorLike<T>) => Promise<boolean> {
462462
return async a => !(await asyncEqualOnce(a, b, elementsEqual));
463463
}
464+
465+
export async function asyncPrefixMatchOnce<T>(
466+
a: AsyncIteratorLike<T>,
467+
b: AsyncIteratorLike<T>,
468+
elementsEqual: (a: T, b: T) => boolean | Promise<boolean> = defaultEqual
469+
): Promise<boolean> {
470+
const ait = asyncIterator(a);
471+
const bit = asyncIterator(b);
472+
let [aElement, bElement] = await Promise.all([ait.next(), bit.next()] as const);
473+
while (
474+
aElement.done !== true &&
475+
bElement.done !== true &&
476+
(await elementsEqual(aElement.value, bElement.value))
477+
) {
478+
[aElement, bElement] = await Promise.all([ait.next(), bit.next()] as const);
479+
}
480+
return bElement.done === true;
481+
}

0 commit comments

Comments
 (0)