1. function formatConfig(config) {
  2. let output = "";
  3. for (const key in config) {
  4. if (config.hasOwnProperty(key)) {
  5. const value = config[key];
  6. let formattedValue;
  7. if (typeof value === 'string') {
  8. formattedValue = `"${value}"`; // Wrap strings in double quotes
  9. } else if (typeof value === 'number') {
  10. formattedValue = value.toFixed(2); // Format numbers to 2 decimal places
  11. } else if (typeof value === 'boolean') {
  12. formattedValue = value ? "true" : "false"; //Represent boolean as string
  13. } else if (value === null) {
  14. formattedValue = "null"; // Represent null as string
  15. } else if (typeof value === 'object') {
  16. formattedValue = JSON.stringify(value, null, 2); //Pretty print objects
  17. }
  18. else {
  19. formattedValue = String(value); //Convert other types to string
  20. }
  21. output += ` ${key}: ${formattedValue}\n`; // Add key-value pair to output
  22. }
  23. }
  24. return output;
  25. }

Add your comment