Skip to content

Commit

Permalink
feat: add boolean parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dbstratta committed Sep 11, 2018
1 parent 7af893b commit 8cc10f5
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ to your definition of valid. See [how to use it](#usage).
- [makeEnv(schema: Schema): Env](#makeenvschema-schema-env)
- [Parsers](#parsers)
- [parsers.string(value: string): string](#parsersstringvalue-string-string)
- [parsers.boolean(value: string): boolean](#parsersbooleanvalue-string-boolean)
- [parsers.integer(value: string): number](#parsersintegervalue-string-number)
- [parsers.float(value: string): float](#parsersfloatvalue-string-float)
- [parsers.email(value: string): string](#parsersemailvalue-string-string)
Expand Down Expand Up @@ -137,6 +138,12 @@ Ensures required env variables are present and returns an env object.

Trivial parser. It doesn't do any validation.

#### parsers.boolean(value: string): boolean

Ensures the value is a truthy or falsy value.
Truthy values: 'true', '1', 'yes'.
Falsy values: 'false', '0', 'no'.

#### parsers.integer(value: string): number

Ensures the value is an integer.
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
boolean,
email,
float,
integer,
Expand All @@ -16,6 +17,7 @@ import {

const parsers = {
string,
boolean,
integer,
float,
email,
Expand Down
28 changes: 28 additions & 0 deletions src/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ describe('parsers.string', () => {
});
});

describe('parsers.boolean', () => {
test('parses a truthy value', () => {
const serializedTruthyValues = ['true', '1', 'yes'];
const expectedValue = true;

serializedTruthyValues.forEach(serializedTruthyValue => {
expect(() => parsers.boolean(serializedTruthyValue)).not.toThrow();
expect(parsers.boolean(serializedTruthyValue)).toEqual(expectedValue);
});
});

test('parses a falsy value', () => {
const serializedFalsyValues = ['false', '0', 'no'];
const expectedValue = false;

serializedFalsyValues.forEach(serializedFalsyValue => {
expect(() => parsers.boolean(serializedFalsyValue)).not.toThrow();
expect(parsers.boolean(serializedFalsyValue)).toEqual(expectedValue);
});
});

test('throws when serialized value is not valid', () => {
const serializedValue = 'invalid';

expect(() => parsers.boolean(serializedValue)).toThrow();
});
});

describe('parsers.integer', () => {
test('parses an integer', () => {
const serializedValue = '10';
Expand Down
24 changes: 24 additions & 0 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ export type Parser<TReturn = any> = (serializedValue: string) => TReturn;
*/
export const string: Parser<string> = serializedValue => serializedValue;

/**
* Parses a boolean. parsed values are case insensitive.
* Truthy values: true, 1, yes.
* Falsy values: false, 0, no.
*/
export const boolean: Parser<boolean> = serializedValue => {
const truthyValues = ['true', '1', 'yes'];
const falsyValues = ['false', '0', 'no'];

const lowercaseSerializedValue = serializedValue.toLowerCase();

if (truthyValues.includes(lowercaseSerializedValue)) {
return true;
}

if (falsyValues.includes(lowercaseSerializedValue)) {
return false;
}

const validValuesString = [...truthyValues, ...falsyValues].join(' ');

throw new Error(`value is not valid. Valid values: ${validValuesString}`);
};

/**
* Parses an integer.
*/
Expand Down

0 comments on commit 8cc10f5

Please sign in to comment.