Skip to content

Commit

Permalink
Add handling for RegExp values (#24)
Browse files Browse the repository at this point in the history
* Allow passing regex flags from env vars

* Add tests

* Address PR comments. Change logic to keep regex flags from config
  • Loading branch information
ChristosLabrou authored Feb 12, 2025
1 parent 392a3da commit ab2cbf3
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 27 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ FROM_DOT_ENV_BOOLEAN_ARRAY=false,true,false
FROM_DOT_ENV_STRING_ARRAY=a,b,c
FROM_DOT_ENV_OBJECT_ARRAY=o,b,j,e,c,t
FROM_DOT_ENV_WITH_DOT=WITH_DOT_ENV
FROM_DOT_ENV_REGEX='some-regex-from-dot-env.*'
12 changes: 8 additions & 4 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module.exports = {
//should be treated as string array
objectArray: [{}, {}],
camelCase: 'camelCase',
'with.dot': 'with.dot'
'with.dot': 'with.dot',
regex: new RegExp('some-regex.*', 'gi')
},
from: {
dot_env: {
Expand All @@ -24,7 +25,8 @@ module.exports = {
//should be treated as string array
objectArray: [{}, {}],
camelCase: 'camelCase',
'with.dot': 'with.dot'
'with.dot': 'with.dot',
regex: new RegExp('some-regex.*', 'gi')
}
},
from_env: {
Expand All @@ -42,7 +44,8 @@ module.exports = {
nestedCamelCase: {
nested: 'nestedCamelCase'
},
'with.dot': 'with.dot'
'with.dot': 'with.dot',
regex: new RegExp('some-regex.*', 'gi')
},
from_test_json: {
number: 0.0,
Expand All @@ -56,6 +59,7 @@ module.exports = {
objectArray: [{}, {}],
camelCase: 'camelCase',
null: null,
'with.dot': 'with.dot'
'with.dot': 'with.dot',
regex: new RegExp('some-regex.*', 'gi')
}
};
3 changes: 2 additions & 1 deletion config/env/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"objectArray": ["1"],
"camelCase": "test",
"null": "null",
"with.dot": "from_test_json"
"with.dot": "from_test_json",
"regex": "some-regex-from-test-json.*"
}
}
9 changes: 7 additions & 2 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const getEnvValue = (path, key) => process.env[`${path}${key}`.toUpperCase()];

const isObject = val => val && Object === val.constructor;

const castValue = (value, type) => {
const castValue = (value, type, configValue) => {
const defaultArrayRegExp = new RegExp('.*?\\[\\]');
const defaultArray = type => defaultArrayRegExp.test(type);
switch (type) {
Expand All @@ -23,6 +23,8 @@ const castValue = (value, type) => {
return Number(value);
case 'boolean':
return value === 'true';
case 'RegExp':
return RegExp(value, configValue.flags);
case 'string':
default:
if (defaultArray(type)) {
Expand All @@ -38,6 +40,9 @@ function getType(config) {
// returns number[], boolean[], string[]
return `${typeof firstElement}[]`;
}
if (config instanceof RegExp) {
return 'RegExp';
}
return typeof config;
}

Expand All @@ -52,7 +57,7 @@ const replace = (config, path, key) => {
return;
}

config[key] = castValue(envValue, getType(config[key]));
config[key] = castValue(envValue, getType(config[key]), config[key]);
};

const replaceConfigFromEnv = (config, path = '') => {
Expand Down
15 changes: 10 additions & 5 deletions test/scenarios/es6default.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = {
FROM_ENV_STRING_ARRAY: 'c,d,e',
FROM_ENV_OBJECT_ARRAY: 'c,d,e',
FROM_ENV_CAMEL_CASE: 'snake_case',
FROM_ENV_WITH_DOT: 'WITH_DOT'
FROM_ENV_WITH_DOT: 'WITH_DOT',
FROM_ENV_REGEX: 'some-regex-from-env.*'
},
expected: {
nested: {
Expand All @@ -27,7 +28,8 @@ module.exports = {
stringArray: ['a', 'b', 'c'],
objectArray: [{}, {}],
camelCase: 'camelCase',
'with.dot': 'with.dot'
'with.dot': 'with.dot',
regex: new RegExp('some-regex.*', 'gi')
},
from: {
dot_env: {
Expand All @@ -40,7 +42,8 @@ module.exports = {
stringArray: ['a', 'b', 'c'],
objectArray: [{}, {}],
camelCase: 'camelCase',
'with.dot': 'with.dot'
'with.dot': 'with.dot',
regex: new RegExp('some-regex.*', 'gi')
}
},
from_env: {
Expand All @@ -57,7 +60,8 @@ module.exports = {
nestedCamelCase: {
nested: 'nestedCamelCase'
},
'with.dot': 'WITH_DOT'
'with.dot': 'WITH_DOT',
regex: new RegExp('some-regex-from-env.*', 'gi')
},
from_test_json: {
number: 1000.0,
Expand All @@ -70,7 +74,8 @@ module.exports = {
objectArray: ['1'],
camelCase: 'test',
null: 'null',
'with.dot': 'from_test_json'
'with.dot': 'from_test_json',
regex: 'some-regex-from-test-json.*'
}
}
};
15 changes: 10 additions & 5 deletions test/scenarios/notDotEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
FROM_ENV_OBJECT_ARRAY: 'c,d,e',
FROM_ENV_CAMEL_CASE: 'snake_case',
FROM_ENV_NESTEDCAMELCASE_NESTED: 'nested snake case',
FROM_ENV_WITH_DOT: 'WITH_DOT'
FROM_ENV_WITH_DOT: 'WITH_DOT',
FROM_ENV_REGEX: 'some-regex-from-env.*'
},
expected: {
nested: {
Expand All @@ -26,7 +27,8 @@ module.exports = {
stringArray: ['a', 'b', 'c'],
objectArray: [{}, {}],
camelCase: 'camelCase',
'with.dot': 'with.dot'
'with.dot': 'with.dot',
regex: new RegExp('some-regex.*', 'gi')
},
from: {
dot_env: {
Expand All @@ -39,7 +41,8 @@ module.exports = {
stringArray: ['a', 'b', 'c'],
objectArray: [{}, {}],
camelCase: 'camelCase',
'with.dot': 'with.dot'
'with.dot': 'with.dot',
regex: new RegExp('some-regex.*', 'gi')
}
},
from_env: {
Expand All @@ -56,7 +59,8 @@ module.exports = {
nestedCamelCase: {
nested: 'nested snake case'
},
'with.dot': 'WITH_DOT'
'with.dot': 'WITH_DOT',
regex: new RegExp('some-regex-from-env.*', 'gi')
},
from_test_json: {
number: 1000.0,
Expand All @@ -69,7 +73,8 @@ module.exports = {
objectArray: ['1'],
camelCase: 'test',
null: 'null',
'with.dot': 'from_test_json'
'with.dot': 'from_test_json',
regex: 'some-regex-from-test-json.*'
}
}
};
15 changes: 10 additions & 5 deletions test/scenarios/notTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ module.exports = {
FROM_ENV_OBJECT_ARRAY: 'c,d,e',
FROM_ENV_CAMELCASE: 'snake_case',
FROM_ENV_NESTEDCAMELCASE_NESTED: 'nested snake case',
FROM_ENV_WITH_DOT: 'WITH_DOT'
FROM_ENV_WITH_DOT: 'WITH_DOT',
FROM_ENV_REGEX: 'some-regex-from-env.*'
},
expected: {
nested: {
Expand All @@ -27,7 +28,8 @@ module.exports = {
//should be treated as string array
objectArray: [{}, {}],
camelCase: 'camelCase',
'with.dot': 'with.dot'
'with.dot': 'with.dot',
regex: new RegExp('some-regex.*', 'gi')
},
from: {
dot_env: {
Expand All @@ -40,7 +42,8 @@ module.exports = {
stringArray: ['a', 'b', 'c'],
objectArray: ['o', 'b', 'j', 'e', 'c', 't'],
camelCase: 'snake_case',
'with.dot': 'WITH_DOT_ENV'
'with.dot': 'WITH_DOT_ENV',
regex: new RegExp('some-regex-from-dot-env.*', 'gi')
}
},
from_env: {
Expand All @@ -57,7 +60,8 @@ module.exports = {
nestedCamelCase: {
nested: 'nested snake case'
},
'with.dot': 'WITH_DOT'
'with.dot': 'WITH_DOT',
regex: new RegExp('some-regex-from-env.*', 'gi')
},
from_test_json: {
number: 0.0,
Expand All @@ -70,7 +74,8 @@ module.exports = {
objectArray: [{}, {}],
camelCase: 'camelCase',
null: null,
'with.dot': 'with.dot'
'with.dot': 'with.dot',
regex: new RegExp('some-regex.*', 'gi')
}
}
};
15 changes: 10 additions & 5 deletions test/scenarios/replaceAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module.exports = {
FROM_ENV_ARRAY_TWO: '',
FROM_ENV_CAMELCASE: 'snake_case',
FROM_ENV_NESTED_CAMEL_CASE_NESTED: 'nested snake case',
FROM_ENV_WITH_DOT: 'WITH_DOT'
FROM_ENV_WITH_DOT: 'WITH_DOT',
FROM_ENV_REGEX: 'some-regex-from-env.*'
},
expected: {
nested: {
Expand All @@ -28,7 +29,8 @@ module.exports = {
//should be treated as string array
objectArray: [{}, {}],
camelCase: 'camelCase',
'with.dot': 'with.dot'
'with.dot': 'with.dot',
regex: new RegExp('some-regex.*', 'gi')
},
from: {
dot_env: {
Expand All @@ -41,7 +43,8 @@ module.exports = {
stringArray: ['a', 'b', 'c'],
objectArray: ['o', 'b', 'j', 'e', 'c', 't'],
camelCase: 'snake_case',
'with.dot': 'WITH_DOT_ENV'
'with.dot': 'WITH_DOT_ENV',
regex: new RegExp('some-regex-from-dot-env.*', 'gi')
}
},
from_env: {
Expand All @@ -58,7 +61,8 @@ module.exports = {
nestedCamelCase: {
nested: 'nested snake case'
},
'with.dot': 'WITH_DOT'
'with.dot': 'WITH_DOT',
regex: new RegExp('some-regex-from-env.*', 'gi')
},
from_test_json: {
number: 1000.0,
Expand All @@ -71,7 +75,8 @@ module.exports = {
objectArray: ['1'],
camelCase: 'test',
null: 'null',
'with.dot': 'from_test_json'
'with.dot': 'from_test_json',
regex: 'some-regex-from-test-json.*'
}
}
};

0 comments on commit ab2cbf3

Please sign in to comment.