An open API service indexing awesome lists of open source software.

https://github.com/myxogastria0808/dotenv-caster

This package is a tiny package that converts `string | undefined` to certain `primitive types`.
https://github.com/myxogastria0808/dotenv-caster

cast dotenv npm-package primitive-types type typescript

Last synced: 6 months ago
JSON representation

This package is a tiny package that converts `string | undefined` to certain `primitive types`.

Awesome Lists containing this project

README

          

# dotenv-caster

[![Test](https://github.com/Myxogastria0808/dotenv-caster/actions/workflows/test.yaml/badge.svg)](https://github.com/Myxogastria0808/dotenv-caster/actions/workflows/test.yaml)
[![Docs](https://github.com/Myxogastria0808/dotenv-caster/actions/workflows/docs.yaml/badge.svg)](https://github.com/Myxogastria0808/dotenv-caster/actions/workflows/docs.yaml)
[![NPM Version](https://img.shields.io/npm/v/dotenv-caster.svg)](https://www.npmjs.com/package/dotenv-caster)
![GitHub Release](https://img.shields.io/github/v/release/Myxogastria0808/dotenv-caster)
![NPM Type Definitions](https://img.shields.io/npm/types/dotenv-caster)
[![Download NPM](https://img.shields.io/npm/dm/dotenv-caster.svg?style=flat)](https://www.npmjs.com/package/dotenv-caster/)
[![NPM Downloads All Time](https://badgen.net/npm/dt/dotenv-caster?label=downloads%20all%20time&color=cyan)](https://www.npmjs.com/package/dotenv-caster/)
![GitHub License](https://img.shields.io/github/license/Myxogastria0808/dotenv-caster)
![Vitest](https://img.shields.io/badge/-vitest-6e9f18?style=flat&logo=vitest&logoColor=ffffff)
![Typedoc](https://img.shields.io/badge/docs-typedoc-blue?style=flat-square&logo=typescript&logoColor=white)
[![RenovateBot](https://img.shields.io/badge/RenovateBot-1A1F6C?logo=renovate&logoColor=fff)](#)

dotenv-caster is a tiny package that converts `string | undefined` to certain `primitive types`.

> [!WARNING]
> A critical bug has been discovered in this package in versions 2.2.0 and below.
>
> Please ensure that you are using the latest version (at least v2.2.1 or newer) when using this package.

> [!IMPORTANT]
> Be sure to use the latest version.

## HTML Documentation Generated by typedoc

https://myxogastria0808.github.io/dotenv-caster/

## Test Result Generated by @vitest/coverage-v8

https://myxogastria0808.github.io/dotenv-caster/coverage/

## Test Coverage Generated by @vitest/ui

https://myxogastria0808.github.io/dotenv-caster/html/

## DeepWiki

> [!WARNING]
> The accuracy of the contents of generated deepwiki has not been verified by me.
>
> I recommend that you look at the documentation at [typedoc](https://myxogastria0808.github.io/dotenv-caster/).

https://deepwiki.com/Myxogastria0808/dotenv-caster/

## 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=1234567890123456789012345678901234567890
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 * 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);
// 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);
```

## 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);
```