Skip to content

Commit

Permalink
Replace deepmap with our own implementation
Browse files Browse the repository at this point in the history
The previous module was using all lodash...
  • Loading branch information
geowarin committed Aug 29, 2016
1 parent a4ccfc8 commit fb34b0c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
57 changes: 39 additions & 18 deletions lib/config/loadUserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion test/loadUserConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit fb34b0c

Please sign in to comment.