4.0.4 • Published 4 years ago

yt-config v4.0.4

Weekly downloads
26
License
UNLICENSED
Repository
github
Last release
4 years ago

YT Config

Build Status Coverage Status dependencies Status Known Vulnerabilities

This module has been designed to substitute existing ini-config parsers out there. Some of which lack typecasting, some have too many dependencies, none of them merges configs according to environment.

Main features:

  • environment sections (merged with default)
  • basic typecasting (values that look like numbers are returned as numbers)
  • valid JSON strings are parsed
  • no dependencies other than lodash.merge

Apart from parsing ini format, it achieves two things:

  • never repeat config for different environments - you have 'default' section and you override values that differ for other environments
  • have all of the config in one file

Example

This ini:

[default]
negInt = -1
posInt = 2
keyDecimal = 10.5
arrayDecimal[] = -10.5
arrayDecimal[] = 12.43
switchOne = on
switchTwo = off
flag = true
falseFlag = false

[default.log]
level = DEBUG


; DEVELOPMENT

[development]
keyDEV = dev
array = ["one", "two", "three", 3]

[development.log]
level = INFO


; STAGING

[staging]
keySTG = stagingValue

[staging.log]
level = ERROR

will result in the following config object:

{ negInt: -1,
  posInt: 2,
  keyDecimal: 10.5,
  arrayDecimal: [ -10.5, 12.43 ],
  switchOne: true,
  switchTwo: false,
  flag: true,
  falseFlag: false,
  log: { level: 'INFO' },
  keyDEV: 'dev',
  array: [ 'one', 'two', 'three', 3 ],
  environment: 'development' }

(development is the default environment)

If we set NODE_ENV to staging:

{ negInt: -1,
  posInt: 2,
  keyDecimal: 10.5,
  arrayDecimal: [ -10.5, 12.43 ],
  switchOne: true,
  switchTwo: false,
  flag: true,
  falseFlag: false,
  null: null,
  log: { level: 'ERROR' },
  keySTG: 'stagingValue',
  environment: 'staging' }

For a working example see example.js

Install

npm i yt-config

Usage

const configLoader = require('yt-config');
...

async function someFunc()  {
    const config = await configLoader('config.ini');
}

Advanced example

If we need to get some values from environment:

[default.db]
port = 3636
host = some.host
user = some_user
password = ENV::DB_PASSWORD

Test

npm test
4.0.4

4 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

3.1.0

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.2

6 years ago

2.0.0

6 years ago