From ab2cbf374d112e2c5c21d58a82ad8319195e0079 Mon Sep 17 00:00:00 2001 From: Christos Labrou <146816904+ChristosLabrou@users.noreply.github.com> Date: Wed, 12 Feb 2025 17:38:20 +0200 Subject: [PATCH] Add handling for RegExp values (#24) * Allow passing regex flags from env vars * Add tests * Address PR comments. Change logic to keep regex flags from config --- .env | 1 + config/config.js | 12 ++++++++---- config/env/test.json | 3 ++- lib/helpers.js | 9 +++++++-- test/scenarios/es6default.js | 15 ++++++++++----- test/scenarios/notDotEnv.js | 15 ++++++++++----- test/scenarios/notTest.js | 15 ++++++++++----- test/scenarios/replaceAll.js | 15 ++++++++++----- 8 files changed, 58 insertions(+), 27 deletions(-) diff --git a/.env b/.env index 0da1be9..cb670af 100644 --- a/.env +++ b/.env @@ -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.*' diff --git a/config/config.js b/config/config.js index cbc37ca..9875d2d 100644 --- a/config/config.js +++ b/config/config.js @@ -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: { @@ -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: { @@ -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, @@ -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') } }; diff --git a/config/env/test.json b/config/env/test.json index 7dd92c2..ded5bcc 100644 --- a/config/env/test.json +++ b/config/env/test.json @@ -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.*" } } diff --git a/lib/helpers.js b/lib/helpers.js index 6117806..6d9a224 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -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) { @@ -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)) { @@ -38,6 +40,9 @@ function getType(config) { // returns number[], boolean[], string[] return `${typeof firstElement}[]`; } + if (config instanceof RegExp) { + return 'RegExp'; + } return typeof config; } @@ -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 = '') => { diff --git a/test/scenarios/es6default.js b/test/scenarios/es6default.js index 0669d27..a9c9302 100644 --- a/test/scenarios/es6default.js +++ b/test/scenarios/es6default.js @@ -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: { @@ -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: { @@ -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: { @@ -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, @@ -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.*' } } }; diff --git a/test/scenarios/notDotEnv.js b/test/scenarios/notDotEnv.js index a45f48d..1570036 100644 --- a/test/scenarios/notDotEnv.js +++ b/test/scenarios/notDotEnv.js @@ -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: { @@ -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: { @@ -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: { @@ -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, @@ -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.*' } } }; diff --git a/test/scenarios/notTest.js b/test/scenarios/notTest.js index 76ff682..9e23ec5 100644 --- a/test/scenarios/notTest.js +++ b/test/scenarios/notTest.js @@ -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: { @@ -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: { @@ -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: { @@ -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, @@ -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') } } }; diff --git a/test/scenarios/replaceAll.js b/test/scenarios/replaceAll.js index 8fdbe5b..70e2ed4 100644 --- a/test/scenarios/replaceAll.js +++ b/test/scenarios/replaceAll.js @@ -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: { @@ -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: { @@ -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: { @@ -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, @@ -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.*' } } };