Skip to content

Commit

Permalink
minor(vest-utils): Predicates.any
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 13, 2023
1 parent da2cce3 commit 7083e3a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/vest-utils/src/Predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ export function all<T = any>(...p: Predicate<T>[]): (value: T) => boolean {
return (value: T) =>
isEmpty(p) ? false : p.every(predicate => predicate(value));
}

export function any<T = any>(...p: Predicate<T>[]): (value: T) => boolean {
return (value: T) =>
isEmpty(p) ? false : p.some(predicate => predicate(value));
}
47 changes: 46 additions & 1 deletion packages/vest-utils/src/__tests__/Predicates.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { all } from 'Predicates';
import { all, any } from 'Predicates';

describe('Predicates', () => {
describe('all', () => {
Expand Down Expand Up @@ -53,4 +53,49 @@ describe('Predicates', () => {
expect(spy2).toHaveBeenCalledWith(5);
});
});

describe('any', () => {
it('Shold return a predicate function', () => {
expect(typeof any()).toBe('function');
});

it('Should return true if any predicate returns true', () => {
expect(
any(
value => value > 0,
value => value === 10
)(5)
).toBe(true);
expect(
any(
value => value === 10,
value => value > 0
)(5)
).toBe(true);
});

it('Should return true if all predicates return true', () => {
const predicate = any(
value => value > 0,
value => value === 10
);

expect(predicate(10)).toBe(true);
});

it('Should return false if all predicates return false', () => {
const predicate = any(
value => value > 0,
value => value === 10
);

expect(predicate(-5)).toBe(false);
});

it('Should return false if no predicates are passed', () => {
const predicate = any();

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

2 comments on commit 7083e3a

@vercel
Copy link

@vercel vercel bot commented on 7083e3a 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
vest-git-latest-ealush.vercel.app
vestjs.dev
www.vestjs.dev

@vercel
Copy link

@vercel vercel bot commented on 7083e3a 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-website.vercel.app
vest-next-ealush.vercel.app
vest-next.vercel.app
vest-next-git-latest-ealush.vercel.app

Please sign in to comment.