diff --git a/lib/config/loadUserConfig.js b/lib/config/loadUserConfig.js index 2c5fe7f..e715426 100644 --- a/lib/config/loadUserConfig.js +++ b/lib/config/loadUserConfig.js @@ -5,7 +5,7 @@ const yaml = require('js-yaml'); const fs = require('fs'); const os = require('os'); const merge = require('lodash.merge'); -const deepMap = require('deep-map'); +const transform = require('lodash.transform'); function getAliases(aliases) { return readList(aliases, {valueKey: 'src', keyKey: 'expose'}) @@ -72,23 +72,6 @@ function readList(list, options = {valueKey: 'value', keyKey: 'key', defaultObje }); } -function isNumeric(n) { - return !isNaN(parseFloat(n)) && isFinite(n); -} - -function toNative(str) { - if (str === 'false') { - return false; - } - if (str === 'true') { - return true; - } - if (isNumeric(str)) { - return Number(str); - } - return str; -} - function replaceEnvVars(str) { if (typeof str !== 'string') { return str; @@ -111,3 +94,41 @@ function replaceEnvVars(str) { }); return toNative(replacement); } + +function mapObject(obj, fn) { + return Object.keys(obj).reduce( + (res, key) => { + res[key] = fn(obj[key]); + return res; + }, + {} + ) +} + +function deepMap(obj, f) { + const deepMapper = val => typeof val === 'object' ? deepMap(val, f) : f(val); + if (Array.isArray(obj)) { + return obj.map(deepMapper); + } + if (typeof obj === 'object') { + return mapObject(obj, deepMapper); + } + return obj; +} + +function isNumeric(n) { + return !isNaN(parseFloat(n)) && isFinite(n); +} + +function toNative(str) { + if (str === 'false') { + return false; + } + if (str === 'true') { + return true; + } + if (isNumeric(str)) { + return Number(str); + } + return str; +} diff --git a/package.json b/package.json index 0ead9db..c42a7a9 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,6 @@ "copy-webpack-plugin": "3.0.1", "css-loader": "0.24.0", "deasync": "0.1.7", - "deep-map": "^1.4.2", "del": "2.2.2", "express": "4.14.0", "extract-text-webpack-plugin": "2.0.0-beta.3", diff --git a/test/loadUserConfig.spec.js b/test/loadUserConfig.spec.js index f4b7b47..f24bc59 100644 --- a/test/loadUserConfig.spec.js +++ b/test/loadUserConfig.spec.js @@ -28,7 +28,7 @@ test('config : should parse config', () => { }); }); -test.only('config: should support environment variables everywhere', t => { +test('config: should support environment variables everywhere', t => { process.env['HAPPY'] = 'false'; process.env['API_URL'] = 'http://localhost:8080';