Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | 4x 3x 1x 8x 7x 7x 3x 4x 1x 9x 8x 8x 3x 1x 4x 3x 1x 8x 7x 4x 3x 3x 1x 7x 6x 3x 3x 1x | /**
* The DotEnvCaster class is a utility for casting environment variables to specific types.
*
* @author Yuki Osada <r.rstudio.c@gmail.com>
*/
export class DotEnvCaster {
/**
* Creates the instance of DotEnvCaster.
*
* @example
* ```ts
* import * as dotenv from 'dotenv';
* // Import dotenv-caster
* import { DotEnvCaster } from 'dotenv-caster';
*
* dotenv.config();
*
* // Create an instance
* const dotenvCaster = new DotEnvCaster();
* ```
*/
constructor() {}
/**
* Casts a `string | undefined` constant to a `string`.
*
* @param {string | undefined} constant - The constant to be cast.
* @returns {string} - The casted string.
* @throws {Error} - If the constant is undefined, the error is thrown.
*
* @example
* ```ts
* import * as dotenv from 'dotenv';
* // Import dotenv-caster
* import { DotEnvCaster } from 'dotenv-caster';
*
* dotenv.config();
*
* // Create an instance
* const dotenvCaster = new DotEnvCaster();
*
* // string | undefined -> string
* const stringSample: string = dotenvCaster.castString(process.env.STRING_SAMPLE);
* ```
*/
castString(constant: string | undefined): string {
if (typeof constant === 'string') {
return constant;
} else {
throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
}
}
/**
* Casts a `number | undefined` constant to a `number`.
*
* @param {number | undefined} constant - The constant to be cast.
* @returns {number} - The casted number.
* @throws {Error} - If the constant is undefined, the error is thrown.
*
* @example
* ```ts
* import * as dotenv from 'dotenv';
* // Import dotenv-caster
* import { DotEnvCaster } from 'dotenv-caster';
*
* dotenv.config();
*
* // Create an instance
* const dotenvCaster = new DotEnvCaster();
*
* // string | undefined -> number
* const numberSample: number = dotenvCaster.castNumber(process.env.NUMBER_SAMPLE);
* ```
*/
castNumber(constant: string | undefined): number {
if (typeof constant === 'string') {
const result = Number(constant);
if (isNaN(result) || !isFinite(result)) {
throw new Error('This constant can not convert to number.');
}
return result;
} else {
throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
}
}
/**
* Casts a `bigint | undefined` constant to a `bigint`.
*
* @param {bigint | undefined} constant - The constant to be cast.
* @returns {bigint} - The casted bigint.
* @throws {Error} - If the constant is undefined, the error is thrown.
*
* @example
* ```ts
* import * as dotenv from 'dotenv';
* // Import dotenv-caster
* import { DotEnvCaster } from 'dotenv-caster';
*
* dotenv.config();
*
* // Create an instance
* const dotenvCaster = new DotEnvCaster();
*
* // string | undefined -> bigint
* const bigIntSample: bigint = dotenvCaster.castBigInt(process.env.BIGINT_SAMPLE);
* ```
*/
castBigInt(constant: string | undefined): bigint {
if (typeof constant === 'string') {
try {
return BigInt(constant);
} catch {
throw new Error('This constant can not convert to bigint.');
}
} else {
throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
}
}
/**
* Casts a `symbol | undefined` constant to a `symbol`.
*
* @param {symbol | undefined} constant - The constant to be cast.
* @returns {symbol} - The casted symbol.
* @throws {Error} - If the constant is undefined, the error is thrown.
*
* @example
* ```ts
* import * as dotenv from 'dotenv';
* // Import dotenv-caster
* import { DotEnvCaster } from 'dotenv-caster';
*
* dotenv.config();
*
* // Create an instance
* const dotenvCaster = new DotEnvCaster();
*
* // string | undefined -> symbol
* const symbolSample: symbol = dotenvCaster.castSymbol(process.env.SYMBOL_SAMPLE);
* ```
*/
castSymbol(constant: string | undefined): symbol {
if (typeof constant === 'string') {
return Symbol(constant);
} else {
throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
}
}
/**
* Casts a `boolean | undefined` constant to a `boolean`.
*
* @param {boolean | undefined} constant - The constant to be cast.
* @returns {boolean} - The casted boolean.
* @throws {Error} - If the constant is undefined, the error is thrown.
*
* @example
* ```ts
* import * as dotenv from 'dotenv';
* // Import dotenv-caster
* import { DotEnvCaster } from 'dotenv-caster';
*
* dotenv.config();
*
* // Create an instance
* const dotenvCaster = new DotEnvCaster();
*
* // string | undefined -> boolean
* const booleanSample: boolean = dotenvCaster.castBoolean(process.env.BOOLEAN_SAMPLE);
* ```
*/
castBoolean(constant: string | undefined): boolean {
if (typeof constant === 'string') {
if (constant === 'true') {
return true;
I} else if (constant === 'false') {
return false;
} else {
throw new Error('This constant can not convert to boolean.');
}
} else {
throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
}
}
/**
* Casts a `null | undefined` constant to a `null`.
*
* @param {null | undefined} constant - The constant to be cast.
* @returns {null} - The casted null.
* @throws {Error} - If the constant is undefined, the error is thrown.
*
* @example
* ```ts
* import * as dotenv from 'dotenv';
* // Import dotenv-caster
* import { DotEnvCaster } from 'dotenv-caster';
*
* dotenv.config();
*
* // Create an instance
* const dotenvCaster = new DotEnvCaster();
*
* // string | undefined -> null
* const nullSample: null = dotenvCaster.castNull(process.env.NULL_SAMPLE);
* ```
*/
castNull(constant: string | undefined): null {
if (typeof constant === 'string') {
if (constant === 'null') {
return null;
} else {
throw new Error('This constant can not convert to null.');
}
} else {
throw new Error('This type of constant is undefined. You have to set the string value in this constant.');
}
}
}
|