/**
* Filters environment variables for debugging purposes.
* Handles potential errors and edge cases gracefully.
*
* @param {Object} env - An object containing environment variables.
* @param {string[]} filterKeywords - An array of keywords to filter by.
* @returns {Object} - An object containing filtered environment variables. Returns an empty object on error.
*/
function filterEnvVariables(env, filterKeywords) {
if (!env || typeof env !== 'object' || Array.isArray(env)) {
console.error("Invalid environment object provided.");
return {}; // Return empty object on error
}
if (!filterKeywords || !Array.isArray(filterKeywords)) {
console.warn("No filter keywords provided. Returning all environment variables.");
return { ...env }; // Return a copy of all env vars
}
const filteredEnv = {};
for (const key in env) {
if (env.hasOwnProperty(key)) {
const value = String(env[key]); // Ensure value is a string for consistent comparison
if (filterKeywords.some(keyword => String(value).toLowerCase().includes(keyword.toLowerCase()))) {
filteredEnv[key] = value;
}
}
}
return filteredEnv;
}
Add your comment