2.2.0 • Published 11 months ago

@aegenet/belt-env-to-obj v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

npm version

@aegenet/belt-env-to-obj

Combine multiple environment variables into one JS object.

💾 Installation

yarn add @aegenet/belt-env-to-obj@^2.0.0
# or
npm i @aegenet/belt-env-to-obj@^2.0.0

📝 Usage

Common

import { env } from 'node:process';
import { envToObject } from '@aegenet/belt-env-to-obj';

// { NAME: 'John', AGE: '25', IS_OK: 'true' }
const result = envToObject(env);
//=> { NAME: 'John', AGE: 25, IS_OK: true }

Properties type

We automatically convert the value to number or boolean if possible.

TypeEnv valueResult TypeResult value
stringtruebooleantrue
stringfalsebooleanfalse
string25number25
stringSomethingstringSomething
string'25'string25
string'true'stringtrue
string"25"string"25"
string"true"string"true"

Nested object

// Default delimiter is '__' (double underscore)
const config = envToObject(
  {
    BELT__CONTACT__NAME: 'John',
    BELT__CONTACT__AGE: '25',
    BELT__CONTACT__IS_OK: 'true',
  },
  {
    convertKey: key => key.toLowerCase(),
  }
);
// config => { belt: { contact: { name: 'John', age: 25, is_ok: true } } }
// With a custom delimiter '.'
const config = envToObject(
  {
    'BELT.CONTACT.NAME': 'John',
    'BELT.CONTACT.AGE': '25',
  },
  {
    convertKey: key => key.toLowerCase(),
    nestedDelimiter: '.',
  }
);
// config => { belt: { contact: { name: 'John', age: 25 } } }
// With a custom delimiter RegExp
const config = envToObject(
  {
    'BELT.CONTACT_NAME': 'John',
    'BELT.CONTACT@AGE': '25',
  },
  {
    convertKey: key => key.toLowerCase(),
    // Every non-alphanumeric character
    nestedDelimiter: /[^a-zA-Z0-9]/,
  }
);
// config => { belt: { contact: { name: 'John', age: 25 } } }
2.2.0

11 months ago

2.1.0

1 year ago

2.0.0

1 year ago

1.7.0

1 year ago