1. /**
  2. * Filters environment variables for debugging purposes.
  3. * Handles potential errors and edge cases gracefully.
  4. *
  5. * @param {Object} env - An object containing environment variables.
  6. * @param {string[]} filterKeywords - An array of keywords to filter by.
  7. * @returns {Object} - An object containing filtered environment variables. Returns an empty object on error.
  8. */
  9. function filterEnvVariables(env, filterKeywords) {
  10. if (!env || typeof env !== 'object' || Array.isArray(env)) {
  11. console.error("Invalid environment object provided.");
  12. return {}; // Return empty object on error
  13. }
  14. if (!filterKeywords || !Array.isArray(filterKeywords)) {
  15. console.warn("No filter keywords provided. Returning all environment variables.");
  16. return { ...env }; // Return a copy of all env vars
  17. }
  18. const filteredEnv = {};
  19. for (const key in env) {
  20. if (env.hasOwnProperty(key)) {
  21. const value = String(env[key]); // Ensure value is a string for consistent comparison
  22. if (filterKeywords.some(keyword => String(value).toLowerCase().includes(keyword.toLowerCase()))) {
  23. filteredEnv[key] = value;
  24. }
  25. }
  26. }
  27. return filteredEnv;
  28. }

Add your comment