YT Config
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 = -1posInt = 2keyDecimal = 10.5arrayDecimal[] = -10.5arrayDecimal[] = 12.43switchOne = onswitchTwo = offflag = truefalseFlag = false [default.log]level = DEBUG ; DEVELOPMENT [development]keyDEV = devarray = ["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: 105 arrayDecimal: -105 1243 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: 105 arrayDecimal: -105 1243 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 = ;... { const config = await ;}
Advanced example
If we need to get some values from environment:
[default.db]port = 3636host = some.hostuser = some_userpassword = ENV::DB_PASSWORD
Test
npm test