/**
 * Get environment variables,
 * which name starts with capitalized prefix,
 * followed by '_'. The prefix will be
 * stripped from keys of retrned object.
 *
 * @example
 * getEnv({
 *     PATH: 'x;y;z',
 *     AFCTL_LOG_LEVEL: '5'
 * }, 'afctl');
 * // returns { "LOG_LEVEL": "5" }
 *
 * @param {Object} env - map of all environment variables
 * @param {string} prefix - relevant environment variables name prefix
 * @returns {Object} map of matching environment variables, with prefix stripped from name
 */
export const getEnv = (env, prefix) => {
    prefix = prefix.toUpperCase() + '_';
    const prefixLength = prefix.length;
    return Object.keys(env).reduce((acc, cur) => {
        if (cur.startsWith(prefix)) {
            acc[cur.slice(prefixLength)] = env[cur];
        }
        return acc;
    }, {});
};

/**
 * Checks whether provided value of environment variable
 * explicitly set to one of the falsy values.
 *
 * Empty string or undefined is considered as
 * not set value, which is not a falsy value.
 * Therefore, function will return false.
 *
 * @param {string} val - environment variable value
 * @returns {boolean} true, if value is falsy string; false otherwise
 */
export const isFalsy = (val = '') => ['false', 'off', 'no', '0'].includes(val.toLowerCase());
