Skip to content

Commit

Permalink
minor(vest-utils): Predicates: Support boolean value
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 13, 2023
1 parent 7083e3a commit 484baec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/vest-utils/src/Predicates.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { isEmpty } from 'isEmpty';
import optionalFunctionValue from 'optionalFunctionValue';

export type Predicate<T> = (value: T) => boolean;
export type Predicate<T> = boolean | ((value: T) => boolean);

export function all<T = any>(...p: Predicate<T>[]): (value: T) => boolean {
return (value: T) =>
isEmpty(p) ? false : p.every(predicate => predicate(value));
isEmpty(p)
? false
: p.every(predicate => optionalFunctionValue(predicate, value));
}

export function any<T = any>(...p: Predicate<T>[]): (value: T) => boolean {
return (value: T) =>
isEmpty(p) ? false : p.some(predicate => predicate(value));
isEmpty(p)
? false
: p.some(predicate => optionalFunctionValue(predicate, value));
}
16 changes: 16 additions & 0 deletions packages/vest-utils/src/__tests__/Predicates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ describe('Predicates', () => {
expect(spy1).toHaveBeenCalledWith(5);
expect(spy2).toHaveBeenCalledWith(5);
});

it('When passing explicit true as a predicate, should return true', () => {
expect(all(true, true, true)(5)).toBe(true);
});

it('When passing explicit false as a predicate, should return false', () => {
expect(all(true, false, false)(5)).toBe(false);
});
});

describe('any', () => {
Expand Down Expand Up @@ -97,5 +105,13 @@ describe('Predicates', () => {

expect(predicate(15)).toBe(false);
});

it('When passing explicit true as a predicate, should return true', () => {
expect(any(true, false, false)(5)).toBe(true);
});

it('When passing explicit false as a predicate, should return false', () => {
expect(any(false, false, false)(5)).toBe(false);
});
});
});

2 comments on commit 484baec

@vercel
Copy link

@vercel vercel bot commented on 484baec Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest – ./website

vest-ealush.vercel.app
vest.vercel.app
vestjs.dev
vest-git-latest-ealush.vercel.app
www.vestjs.dev

@vercel
Copy link

@vercel vercel bot commented on 484baec Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vest-next – ./website

vest-next-ealush.vercel.app
vest-next-git-latest-ealush.vercel.app
vest-website.vercel.app
vest-next.vercel.app

Please sign in to comment.