Skip to content

Commit

Permalink
Merge pull request #749 from screendriver/readonly-find
Browse files Browse the repository at this point in the history
Allow readonly Arrays in find()
  • Loading branch information
chriskrycho committed May 15, 2024
2 parents 4dc94b0 + f61b991 commit 84dad05
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/maybe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,8 @@ export function mapOr<T, U>(
return mapFn === undefined
? partialOp
: maybe === undefined
? partialOp(mapFn)
: partialOp(mapFn, maybe);
? partialOp(mapFn)
: partialOp(mapFn, maybe);
}

/**
Expand Down Expand Up @@ -1083,12 +1083,12 @@ export function isInstance<T>(item: unknown): item is Maybe<T> {
return item instanceof Maybe;
}

export type Predicate<T> = (element: T, index: number, array: T[]) => boolean;
export type Predicate<T> = (element: T, index: number, array: T[] | readonly T[]) => boolean;

export type NarrowingPredicate<T, U extends T> = (
element: T,
index: number,
array: T[]
array: T[] | readonly T[]
) => element is U;

// NOTE: documentation is lightly adapted from the MDN and TypeScript docs for
Expand Down Expand Up @@ -1147,15 +1147,20 @@ export type NarrowingPredicate<T, U extends T> = (
returns `Nothing`.
* @param array The array to search using the predicate.
*/
export function find<T, U extends T>(predicate: NarrowingPredicate<T, U>, array: T[]): Maybe<U>;
export function find<T, U extends T>(predicate: NarrowingPredicate<T, U>): (array: T[]) => Maybe<U>;
export function find<T>(predicate: Predicate<T>, array: T[]): Maybe<T>;
export function find<T>(predicate: Predicate<T>): (array: T[]) => Maybe<T>;
export function find<T, U extends T>(
predicate: NarrowingPredicate<T, U>,
array: T[] | readonly T[]
): Maybe<U>;
export function find<T, U extends T>(
predicate: NarrowingPredicate<T, U>
): (array: T[] | readonly T[]) => Maybe<U>;
export function find<T>(predicate: Predicate<T>, array: T[] | readonly T[]): Maybe<T>;
export function find<T>(predicate: Predicate<T>): (array: T[] | readonly T[]) => Maybe<T>;
export function find<T, U extends T>(
predicate: NarrowingPredicate<T, U> | Predicate<T>,
array?: T[]
): Maybe<T> | ((array: T[]) => Maybe<T>) {
const op = (a: T[]) => Maybe.of(a.find(predicate));
array?: T[] | readonly T[]
): Maybe<T> | ((array: T[] | readonly T[]) => Maybe<T>) {
const op = (a: T[] | readonly T[]) => Maybe.of(a.find(predicate));
return curry1(op, array);
}

Expand Down
4 changes: 4 additions & 0 deletions test/maybe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ describe('`Maybe` pure functions', () => {
expect(waffles.variant).toBe(MaybeNS.Variant.Just);
expect((waffles as Just<Item>).value).toEqual(array[1]);
expectTypeOf(waffles).toMatchTypeOf<Maybe<{ name: 'waffles' }>>();

const readonlyEmpty: readonly number[] = [];
const foundReadonly = MaybeNS.find(pred, readonlyEmpty);
expectTypeOf(foundReadonly).toMatchTypeOf<Maybe<number>>();
});

test('`first`', () => {
Expand Down

0 comments on commit 84dad05

Please sign in to comment.