Skip to content

Commit 8482a41

Browse files
committed
feat(prefixmatch): add prefixMatch function
1 parent 920f2c1 commit 8482a41

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

index.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
or,
4040
partition,
4141
partitionWhile,
42+
prefixMatch,
4243
prepend,
4344
product,
4445
remove,
@@ -169,6 +170,14 @@ test("equal", t => {
169170
);
170171
});
171172

173+
test("prefixMatch", t => {
174+
t.true(prefixMatch([], []));
175+
t.true(prefixMatch([1, 2, 3], []));
176+
t.true(prefixMatch([1, 2, 3, 4], [1, 2]));
177+
t.false(prefixMatch([1, 3, 4], [1, 2]));
178+
t.false(prefixMatch([], [1]));
179+
});
180+
172181
test("map", t => {
173182
t.deepEqual(
174183
map([1, 2, 3], e => e + 1),

index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,31 @@ export function notEqualFn<T>(
254254
return a => notEqual(a, b, elementsEqual);
255255
}
256256

257+
export function prefixMatch<T>(
258+
a: ArrayLike<T>,
259+
b: ArrayLike<T>,
260+
elementsEqual: (a: T, b: T) => boolean = defaultEqual
261+
): boolean {
262+
if (a.length < b.length) {
263+
return false;
264+
}
265+
266+
for (let i = 0; i < b.length; ++i) {
267+
if (a[i] !== b[i]) {
268+
return false;
269+
}
270+
}
271+
272+
return true;
273+
}
274+
275+
export function prefixMatchFn<T>(
276+
b: ArrayLike<T>,
277+
elementsEqual: (a: T, b: T) => boolean = defaultEqual
278+
): (a: ArrayLike<T>) => boolean {
279+
return a => prefixMatch(a, b, elementsEqual);
280+
}
281+
257282
function defaultEqual(a: unknown, b: unknown): boolean {
258283
return a === b;
259284
}

0 commit comments

Comments
 (0)