diff --git a/packages/vest-utils/src/Predicates.ts b/packages/vest-utils/src/Predicates.ts index 8784a0652..a7f02668e 100644 --- a/packages/vest-utils/src/Predicates.ts +++ b/packages/vest-utils/src/Predicates.ts @@ -6,3 +6,8 @@ export function all(...p: Predicate[]): (value: T) => boolean { return (value: T) => isEmpty(p) ? false : p.every(predicate => predicate(value)); } + +export function any(...p: Predicate[]): (value: T) => boolean { + return (value: T) => + isEmpty(p) ? false : p.some(predicate => predicate(value)); +} diff --git a/packages/vest-utils/src/__tests__/Predicates.test.ts b/packages/vest-utils/src/__tests__/Predicates.test.ts index a9e5a3cc0..0f260f42c 100644 --- a/packages/vest-utils/src/__tests__/Predicates.test.ts +++ b/packages/vest-utils/src/__tests__/Predicates.test.ts @@ -1,4 +1,4 @@ -import { all } from 'Predicates'; +import { all, any } from 'Predicates'; describe('Predicates', () => { describe('all', () => { @@ -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); + }); + }); });