1 | /* global process */
|
2 | /** @module io */
|
3 |
|
4 | const { fnOrValue } = require('../logic');
|
5 | /** Returns and parses an environment var and if does not exists returns the default value
|
6 | * @argument {String} key key to get from our environment variables
|
7 | * @argument {*} defaultValue default value to be returned if key not found
|
8 | * @argument {Function} postProcessor process the value once getted.
|
9 | * @argument {Object} env io source to read environment vars from
|
10 | * @example
|
11 | * //No environment var
|
12 | * envOr('db','localhost'); //-> localhost
|
13 | * envOr('DB','localhost'); //-> localhost
|
14 | * //PORT=3000
|
15 | * envOr('port',8080); //-> 3000
|
16 | * envOr('port',8080,parseFloat); //-> 3000 (float)
|
17 | * @returns {*}
|
18 | * @method
|
19 | */
|
20 | const envOr = (key, defaultValue, postProcessor = x => x, { env } = process) => postProcessor(typeof key === 'string' && env && env[key.toUpperCase()]) || fnOrValue(defaultValue, key);
|
21 |
|
22 | module.exports = { envOr };
|