Added function to parse optional boolean to string

This commit is contained in:
Alejandro Celaya 2022-12-28 23:00:55 +01:00
parent a3bd10bc82
commit 1c39e3402b
2 changed files with 21 additions and 1 deletions

View file

@ -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)
);

View file

@ -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);
});
});
});