Last active
September 28, 2021 14:48
-
-
Save PawDevUK/5809f9dc4e4df29cadae5d8c4cf7d9e2 to your computer and use it in GitHub Desktop.
UTILS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| replaceQuotationMarks(content) { | |
| let replacement = { | |
| """:'"', | |
| """:'"', | |
| "'":"'", | |
| "'":"'" | |
| } | |
| if ( Object.keys(replacement).map( item => | |
| content.includes(item) | |
| )){ | |
| return content.replace(/("|'|"|')/gi, function(noe) { | |
| return replacement[noe]; | |
| }); | |
| } | |
| return content | |
| } | |
| /** | |
| * Helper to parse an environment variable as a flag and return a boolean value. | |
| * Useful for normalising boolean environment variable values encoded as strings. | |
| * @param {string} envVarName name of the environment variable to read | |
| * @param {boolean} defaultValue value to use if the environment variable is not set | |
| * @return {boolean} `false` if the env var is a (case-insensitive) match to 'off', 'no', 'false', '0' | |
| * `true` if the env var is a (case-insensitive) match to 'on', 'yes', 'true', '1' | |
| * `defaultValue` if the env var is not set (undefined/null) | |
| * otherwise returns the truthiness of the env var value (and warns of ambiguity) | |
| */ | |
| function parseEnvVarAsFlag(envVarName, defaultValue = false) { | |
| const value = process.env[envVarName]; | |
| if (typeof value === 'undefined' || value === null) { | |
| return defaultValue; | |
| } else if (['off', 'no', 'false', '0'].includes(value.toLowerCase())) { | |
| return false; | |
| } else if (['on', 'yes', 'true', '1'].includes(value.toLowerCase())) { | |
| return true; | |
| } else { | |
| const resolvedValue = Boolean(value); | |
| console.warn(`WARNING: Ambiguous value '${value}' given for flag '${envVarName}', will interpret it as ${resolvedValue}`); | |
| return resolvedValue; | |
| } | |
| } | |
| /** | |
| * Helper to parse an environment variable as a comma-separated list and return an array of those values | |
| * Values in the list cannot contain commas, and empty values will be discarded | |
| * @param {string} envVarName name of the environment variable to read | |
| * @param {boolean} defaultValue value to use if the environment variable is not set | |
| * @param {Object} options | |
| * @param {boolean} options.defaultOnEmpty if true the defaultValue will be returned if the env var is empty string, | |
| * if false the empty array will be returned if the env var is empty string | |
| * @return {boolean} array of values if the env var is set | |
| * `defaultValue` if the env var is not set (undefined/null) | |
| */ | |
| function parseEnvVarAsList(envVarName, defaultValue = [], { defaultOnEmpty = true } = {}) { | |
| const value = process.env[envVarName]; | |
| if (typeof value === 'undefined' || value === null || (value === '' && defaultOnEmpty)) { | |
| return defaultValue; | |
| } else { | |
| // NOTE: Values cannot contain commas as there is no escaping of delimiters | |
| return value.split(',').filter(Boolean); | |
| } | |
| } | |
| module.exports = { | |
| parseEnvVarAsFlag, | |
| parseEnvVarAsList | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment