Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/myxogastria0808/dotenv-caster
This is a npm package. The package name is dotenv-caster.
https://github.com/myxogastria0808/dotenv-caster
Last synced: about 1 month ago
JSON representation
This is a npm package. The package name is dotenv-caster.
- Host: GitHub
- URL: https://github.com/myxogastria0808/dotenv-caster
- Owner: Myxogastria0808
- License: mit
- Created: 2024-06-04T03:40:48.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-06-07T16:36:48.000Z (7 months ago)
- Last Synced: 2024-10-12T20:35:45.052Z (2 months ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/dotenv-caster?activeTab=readme
- Size: 49.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# dotenv-caster
dotenv-caster is a tiny package that converts `string | undefined` to certain `primitive types`.
> [!IMPORTANT]
> Be sure to use the latest version.## How to Use
This is an example of use if the following is written in `.env`.
```.env
STRING_SAMPLE=Hello
NUMBER_SAMPLE=0
BIGINT_SAMPLE=123456789
SYMBOL_SAMPLE=SYMBOL
BOOLEAN_SAMPLE=true
NULL_SAMPLE=null```
The following is an example of use. `dotenv-caster` is intended for use in projects that use `dotenv`.
```typescript
import 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);
//string | undefined -> number
const numberSample: number = dotenvCaster.castNumber(process.env.NUMBER_SAMPLE);
//string | undefined -> bigint
const bigIntSample: bigint = dotenvCaster.castBigInt(process.env.BIGINT_SAMPLE);
//string | undefined -> symbol
const symbolSample: symbol = dotenvCaster.castSymbol(process.env.SYMBOL_SAMPLE);
//string | undefined -> boolean
const booleanSample: boolean = dotenvCaster.castBoolean(process.env.BOOLEAN_SAMPLE);
//string | undefined -> null
const nullSample: null = dotenvCaster.castNull(process.env.NULL_SAMPLE);
```### examples showing the use
[dotenv-caster-sample](https://github.com/Myxogastria0808/dotenv-caster-sample)
## Detail
### Create an instance
When using `dotenv-caster`, first import `dotenv-caster` and create an instance as follows.
```typescript
//import dotenv-caster
import { DotEnvCaster } from 'dotenv-caster';//Create an instance
const dotenvCaster = new DotEnvCaster();
```### string | undefined → string
```typescript
//string | undefined -> string
const stringSample: string = dotenvCaster.castString(process.env.STRING_SAMPLE);
```### string | undefined → number
```typescript
//string | undefined -> number
const numberSample: number = dotenvCaster.castNumber(process.env.NUMBER_SAMPLE);
```### string | undefined → bigint
```typescript
//string | undefined -> bigint
const bigIntSample: bigint = dotenvCaster.castBigInt(process.env.BIGINT_SAMPLE);
```### string | undefined → symbol
```typescript
//string | undefined -> symbol
const symbolSample: symbol = dotenvCaster.castSymbol(process.env.SYMBOL_SAMPLE);
```### string | undefined → boolean
```typescript
//string | undefined -> boolean
const booleanSample: boolean = dotenvCaster.castBoolean(process.env.BOOLEAN_SAMPLE);
```### string | undefined → null
```typescript
//string | undefined -> null
const nullSample: null = dotenvCaster.castNull(process.env.NULL_SAMPLE);
```## HTML documentation generated by typedoc
https://myxogastria0808.github.io/dotenv-caster/