From 484baec96622ca023945dbdea36ac5829bdb2aec Mon Sep 17 00:00:00 2001 From: Evyatar Date: Mon, 13 Nov 2023 21:10:13 +0200 Subject: [PATCH] minor(vest-utils): Predicates: Support boolean value --- packages/vest-utils/src/Predicates.ts | 11 ++++++++--- .../vest-utils/src/__tests__/Predicates.test.ts | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/vest-utils/src/Predicates.ts b/packages/vest-utils/src/Predicates.ts index a7f02668e..1ff6dac8b 100644 --- a/packages/vest-utils/src/Predicates.ts +++ b/packages/vest-utils/src/Predicates.ts @@ -1,13 +1,18 @@ import { isEmpty } from 'isEmpty'; +import optionalFunctionValue from 'optionalFunctionValue'; -export type Predicate = (value: T) => boolean; +export type Predicate = boolean | ((value: T) => boolean); export function all(...p: Predicate[]): (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(...p: Predicate[]): (value: T) => boolean { return (value: T) => - isEmpty(p) ? false : p.some(predicate => predicate(value)); + isEmpty(p) + ? false + : p.some(predicate => optionalFunctionValue(predicate, value)); } diff --git a/packages/vest-utils/src/__tests__/Predicates.test.ts b/packages/vest-utils/src/__tests__/Predicates.test.ts index 0f260f42c..7b2b23d3f 100644 --- a/packages/vest-utils/src/__tests__/Predicates.test.ts +++ b/packages/vest-utils/src/__tests__/Predicates.test.ts @@ -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', () => { @@ -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); + }); }); });