2.1.4 • Published 3 years ago
dotenv-guards v2.1.4
dotenv guards

guards functions for dotenv package
installation
npm i dotenv dotenv-guardsusage
import { numberGuard } from 'dotenv-guards';
const jobsAsNumber = numberGuard(process.env.jobs, 2); // return 2 if jobs is not a numberAvailable guards
numberGuard- parse a string to a number.enumGuard- parse a string to an enum.booleanGuard- parse a string to a boolean.arrayGuard- parse a string to an array.string- parse string and execute matchers.
Custom guards
In case if you want to define custom guards - you can use define and revoke functions.
Example:
import { define, revoke } from 'dotenv-guards';
// define new guard
const jsonGuard = define((val: string | undefined) => {
return JSON.parse(val);
});
jsonGuard('{"qwe": true}'); // returns { qwe: true }
revoke(jsonGuard); // remove json guard
jsonGuard('{"qwe": true}'); // TypeError. jsonGuard is revokedAPI
number guard
numberGuard(value, fallbackValue, options)Parameters
value-string- string-like variable.options-ObjectthrowOnFinite-boolean- Throws an error if incoming value parsed toInfinity. Default isfalse.throwOnNaN-boolean- Throws an error if incoming value parsed toNaN. Default isfalse.throwOnUndefined-boolean- Iftruethrowing an error if incoming value is undefined. Default isfalse.throwOnSafeInteger-boolean- Throws an error if incoming value is not safe integer. Default isfalse.
Returns
number
Example:
// process.env.jobs = '2'
numberGuard(process.env.jobs); // returns 2 as number
// process.env.jobs = 'not a number string'
numberGuard(process.env.jobs); // returns 0 as number
// process.env.jobs = 'not a number string'
numberGuard(process.env.jobs, 0, {throwOnSafeInteger: true}); //throws an errorboolean guard
booleanGuard(value, options)Parameters
- value -
string- string-like variable. options-Objectfallback- fallback value if incoming value is not a boolean and cannot parse value to boolean.trueSymbols-string- Array of possible values which will be converted astrue. Default is['1', 'true']throwOnUndefinedThrow an error if incoming value is undefined, fallback value is not returned, since it returns an error. Default isfalse.throwOnFail- Throw an error if incoming value is not matching withtrueSymbolsoption. Default isfalse.
Returns
boolean
Example:
// process.env.isDebug = 'true'
booleanGuard(process.env.isDebug); // returns true
// process.env.acceptDownloading = 'yes'
booleanGuard(process.env.acceptDownloading, false, {trueSymbols: ['yes']}); // returns true since 'yes' is in the arrayenum guard
enum(value, arrayOfPossibleValues, fallbackValue)Parameters
value-string- string-like variable.arrayOfPossibleValues-Array<string>- Array of possible values.fallbackValue-string- fallback value.
Returns
string
Example:
// process.env.NODE_ENV = 'test'
enumGuard(process.env.NODE_ENV, ['development', 'production'], 'development'); // returns 'development'
// or define more acceptable values
// process.env.NODE_ENV = 'test'
enumGuard(process.env.NODE_ENV, ['development', 'production', 'test'], 'development'); // returns 'test', since 'test' is in the arrayarray guard
array(value, arrayOfPossibleValues, options)Parameters
value-string- string-like variable.arrayOfPossibleValues-Array<string>- Iftruethrowing an error if at least one element is not matched.options-Object
Returns
string
Example:
// process.env.array = 'val1,val2,val3'
arrayGuard(process.env.array, ['val1', 'val2']); // split string by `,` symbol and returns ['val1', 'val2']
// custom separator
// process.env.array = 'val1;val2;val3'
arrayGuard(process.env.array, ['val1', 'val2', 'val3'], {separator: ';'}); // split string by `;` symbol and returns ['val1', 'val2', 'val3']string guard
string(value, options)Parameters
value- string. Parsing string-like value.optionsfallback- string - fallback value if incoming value is not matched by regex or matcher function.throwOnUndefined- boolean - Throw an error if incoming value is undefined, fallback value is not returned, since it returns an error.regexp- RegExp - Regexp for incoming value. Returns first match.throwOnNullable- boolean - Throw an error if incoming value is null, undefined, empty string or empty array.matcher- boolean - Matcher function for incoming value. Returns boolean(is matched incoming string or not).throwOnMismatch- boolean - Throw an error if incoming value is not matched by regex or matcher function.
Returns
string
Example:
// process.env.token = 'hash123'
stringGuard(process.env.token); // returns hash123
// matcher function
// process.env.token = 'hash123'
arrayGuard(process.env.token, {
matcher: (value) => value.startsWith('hash'),
}); // returns hash123
// miss matching
// process.env.token = 'hash123'
arrayGuard(process.env.token, {
matcher: (value) => value.startsWith('random string'),
fallback: 'qwerty'
}); // returns qwerty, since mismatchdefine(fn)
Parameters
function- Function - First argument should accepts typestring | undefined, otherwise it throws anTypeError
returns
Function
revoke(fn)
Parameters
function- Function - function reference fromdefine. It will throw aReferenceErrorin case of function is not created fromdefine.
Returns
void
2.1.2
3 years ago
2.0.3
3 years ago
2.1.1
3 years ago
2.1.4
3 years ago
2.1.3
3 years ago
2.1.0
3 years ago
1.3.7
3 years ago
1.3.6
3 years ago
1.3.5
3 years ago
1.3.4
3 years ago
1.3.3
3 years ago
1.2.4
3 years ago
1.3.2
3 years ago
1.2.3
3 years ago
1.3.1
3 years ago
1.3.0
3 years ago
2.0.2
3 years ago
2.0.1
3 years ago
2.0.0
3 years ago
1.2.2
3 years ago
1.2.1
3 years ago
1.2.0
3 years ago
1.1.1
3 years ago
1.1.0
3 years ago
1.0.11
3 years ago
1.0.10
3 years ago
1.0.9
3 years ago
1.0.8
3 years ago
1.0.7
3 years ago
1.0.6
3 years ago
1.0.5
3 years ago
1.0.4
3 years ago
1.0.3
3 years ago
1.0.2
3 years ago
1.0.0
3 years ago