From 1c39e3402bdc6be81b3cce8cb3a268ab594e46a8 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 28 Dec 2022 23:00:55 +0100 Subject: [PATCH] Added function to parse optional boolean to string --- src/utils/utils.ts | 4 ++++ test/utils/utils.test.ts | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 504d450e..4ddef89d 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -30,3 +30,7 @@ export const equals = (value: any) => (otherValue: any) => value === otherValue; export type BooleanString = 'true' | 'false'; export const parseBooleanToString = (value: boolean): BooleanString => (value ? 'true' : 'false'); + +export const parseOptionalBooleanToString = (value?: boolean): BooleanString | undefined => ( + value === undefined ? undefined : parseBooleanToString(value) +); diff --git a/test/utils/utils.test.ts b/test/utils/utils.test.ts index d08a5fee..aa2d25c3 100644 --- a/test/utils/utils.test.ts +++ b/test/utils/utils.test.ts @@ -1,4 +1,10 @@ -import { capitalize, nonEmptyValueOrNull, parseBooleanToString, rangeOf } from '../../src/utils/utils'; +import { + capitalize, + nonEmptyValueOrNull, + parseBooleanToString, + parseOptionalBooleanToString, + rangeOf, +} from '../../src/utils/utils'; describe('utils', () => { describe('rangeOf', () => { @@ -58,4 +64,14 @@ describe('utils', () => { expect(parseBooleanToString(value)).toEqual(expectedResult); }); }); + + describe('parseOptionalBooleanToString', () => { + it.each([ + [undefined, undefined], + [true, 'true'], + [false, 'false'], + ])('parses value as expected', (value, expectedResult) => { + expect(parseOptionalBooleanToString(value)).toEqual(expectedResult); + }); + }); });