stringify.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.toString = void 0;
  4. const quote_1 = require("./quote");
  5. const object_1 = require("./object");
  6. const function_1 = require("./function");
  7. /**
  8. * Stringify primitive values.
  9. */
  10. const PRIMITIVE_TYPES = {
  11. string: quote_1.quoteString,
  12. number: (value) => (Object.is(value, -0) ? "-0" : String(value)),
  13. boolean: String,
  14. symbol: (value, space, next) => {
  15. const key = Symbol.keyFor(value);
  16. if (key !== undefined)
  17. return `Symbol.for(${next(key)})`;
  18. // ES2018 `Symbol.description`.
  19. return `Symbol(${next(value.description)})`;
  20. },
  21. bigint: (value, space, next) => {
  22. return `BigInt(${next(String(value))})`;
  23. },
  24. undefined: String,
  25. object: object_1.objectToString,
  26. function: function_1.functionToString,
  27. };
  28. /**
  29. * Stringify a value recursively.
  30. */
  31. const toString = (value, space, next, key) => {
  32. if (value === null)
  33. return "null";
  34. return PRIMITIVE_TYPES[typeof value](value, space, next, key);
  35. };
  36. exports.toString = toString;
  37. //# sourceMappingURL=stringify.js.map