object.js 2.7 KB

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