value-to-string.js 844 B

12345678910111213141516171819202122232425262728293031323334
  1. import {isClass} from './utils/index.js';
  2. const BASE_CTOR_NAMES = [
  3. 'String',
  4. 'Number',
  5. 'Boolean',
  6. 'Object',
  7. 'Array',
  8. 'Function',
  9. 'Symbol',
  10. 'Map',
  11. 'Set',
  12. 'Date',
  13. ];
  14. /**
  15. * Value to string.
  16. *
  17. * @param {any} input
  18. * @return {string}
  19. */
  20. export function valueToString(input) {
  21. if (input == null) return String(input);
  22. if (typeof input === 'string') return `"${input}"`;
  23. if (typeof input === 'number' || typeof input === 'boolean')
  24. return String(input);
  25. if (isClass(input)) return input.name ? input.name : 'Class';
  26. if (input.constructor && input.constructor.name)
  27. return BASE_CTOR_NAMES.includes(input.constructor.name)
  28. ? input.constructor.name
  29. : `${input.constructor.name} (instance)`;
  30. if (typeof input === 'object' && input.constructor == null) return 'Object';
  31. return String(input);
  32. }