Skip to content

Commit

Permalink
Merge branch 'main' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
djcsdy committed Jan 17, 2022
2 parents 415a266 + 8482a41 commit 9d19710
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
or,
partition,
partitionWhile,
prefixMatch,
prepend,
product,
remove,
Expand Down Expand Up @@ -169,6 +170,14 @@ test("equal", t => {
);
});

test("prefixMatch", t => {
t.true(prefixMatch([], []));
t.true(prefixMatch([1, 2, 3], []));
t.true(prefixMatch([1, 2, 3, 4], [1, 2]));
t.false(prefixMatch([1, 3, 4], [1, 2]));
t.false(prefixMatch([], [1]));
});

test("map", t => {
t.deepEqual(
map([1, 2, 3], e => e + 1),
Expand Down
25 changes: 25 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,31 @@ export function notEqualFn<T>(
return a => notEqual(a, b, elementsEqual);
}

export function prefixMatch<T>(
a: ArrayLike<T>,
b: ArrayLike<T>,
elementsEqual: (a: T, b: T) => boolean = defaultEqual
): boolean {
if (a.length < b.length) {
return false;
}

for (let i = 0; i < b.length; ++i) {
if (a[i] !== b[i]) {
return false;
}
}

return true;
}

export function prefixMatchFn<T>(
b: ArrayLike<T>,
elementsEqual: (a: T, b: T) => boolean = defaultEqual
): (a: ArrayLike<T>) => boolean {
return a => prefixMatch(a, b, elementsEqual);
}

function defaultEqual(a: unknown, b: unknown): boolean {
return a === b;
}
Expand Down

0 comments on commit 9d19710

Please sign in to comment.