object.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.objectToString = void 0;
  4. const quote_1 = require("./quote");
  5. const function_1 = require("./function");
  6. const array_1 = require("./array");
  7. /**
  8. * Transform an object into a string.
  9. */
  10. const objectToString = (value, space, next, key) => {
  11. // Support buffer in all environments.
  12. if (typeof Buffer === "function" && Buffer.isBuffer(value)) {
  13. return `Buffer.from(${next(value.toString("base64"))}, 'base64')`;
  14. }
  15. // Support `global` under test environments that don't print `[object global]`.
  16. if (typeof global === "object" && value === global) {
  17. return globalToString(value, space, next, key);
  18. }
  19. // Use the internal object string to select stringify method.
  20. const toString = OBJECT_TYPES[Object.prototype.toString.call(value)];
  21. return toString ? toString(value, space, next, key) : undefined;
  22. };
  23. exports.objectToString = objectToString;
  24. /**
  25. * Stringify an object of keys and values.
  26. */
  27. const rawObjectToString = (obj, indent, next, key) => {
  28. const eol = indent ? "\n" : "";
  29. const space = indent ? " " : "";
  30. // Iterate over object keys and concat string together.
  31. const values = Object.keys(obj)
  32. .reduce(function (values, key) {
  33. const fn = obj[key];
  34. const result = next(fn, key);
  35. // Omit `undefined` object entries.
  36. if (result === undefined)
  37. return values;
  38. // String format the value data.
  39. const value = result.split("\n").join(`\n${indent}`);
  40. // Skip `key` prefix for function parser.
  41. if (function_1.USED_METHOD_KEY.has(fn)) {
  42. values.push(`${indent}${value}`);
  43. return values;
  44. }
  45. values.push(`${indent}${quote_1.quoteKey(key, next)}:${space}${value}`);
  46. return values;
  47. }, [])
  48. .join(`,${eol}`);
  49. // Avoid new lines in an empty object.
  50. if (values === "")
  51. return "{}";
  52. return `{${eol}${values}${eol}}`;
  53. };
  54. /**
  55. * Stringify global variable access.
  56. */
  57. const globalToString = (value, space, next) => {
  58. return `Function(${next("return this")})()`;
  59. };
  60. /**
  61. * Convert JavaScript objects into strings.
  62. */
  63. const OBJECT_TYPES = {
  64. "[object Array]": array_1.arrayToString,
  65. "[object Object]": rawObjectToString,
  66. "[object Error]": (error, space, next) => {
  67. return `new Error(${next(error.message)})`;
  68. },
  69. "[object Date]": (date) => {
  70. return `new Date(${date.getTime()})`;
  71. },
  72. "[object String]": (str, space, next) => {
  73. return `new String(${next(str.toString())})`;
  74. },
  75. "[object Number]": (num) => {
  76. return `new Number(${num})`;
  77. },
  78. "[object Boolean]": (bool) => {
  79. return `new Boolean(${bool})`;
  80. },
  81. "[object Set]": (set, space, next) => {
  82. return `new Set(${next(Array.from(set))})`;
  83. },
  84. "[object Map]": (map, space, next) => {
  85. return `new Map(${next(Array.from(map))})`;
  86. },
  87. "[object RegExp]": String,
  88. "[object global]": globalToString,
  89. "[object Window]": globalToString,
  90. };
  91. //# sourceMappingURL=object.js.map