index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __spreadArrays = (this && this.__spreadArrays) || function () {
  14. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  15. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  16. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  17. r[k] = a[j];
  18. return r;
  19. };
  20. Object.defineProperty(exports, "__esModule", { value: true });
  21. var seen = [];
  22. /**
  23. * Check if a value is an object or a function. Keep in mind that array, function, regexp, etc, are objects in JavaScript.
  24. *
  25. * @param value the value to check
  26. * @return true if the value is an object or a function
  27. */
  28. function isObj(value) {
  29. var type = typeof value;
  30. return value !== null && (type === 'object' || type === 'function');
  31. }
  32. /**
  33. * Check if a value is a regular expression.
  34. *
  35. * @param value the value to check
  36. * @return true if the value is a regular expression
  37. */
  38. function isRegexp(value) {
  39. return Object.prototype.toString.call(value) === '[object RegExp]';
  40. }
  41. /**
  42. * Get an array of all of the enumerable symbols for an object.
  43. *
  44. * @param object the object to get the enumerable symbols for
  45. */
  46. function getOwnEnumPropSymbols(object) {
  47. return Object.getOwnPropertySymbols(object).filter(function (keySymbol) { return Object.prototype.propertyIsEnumerable.call(object, keySymbol); });
  48. }
  49. /**
  50. * pretty print an object
  51. *
  52. * @param input the object to pretty print
  53. * @param options the formatting options, transforms, and filters
  54. * @param pad the padding string
  55. */
  56. function prettyPrint(input, options, pad) {
  57. if (pad === void 0) { pad = ''; }
  58. // sensible option defaults
  59. var defaultOptions = {
  60. indent: '\t',
  61. singleQuotes: true
  62. };
  63. var combinedOptions = __assign(__assign({}, defaultOptions), options);
  64. var tokens;
  65. if (combinedOptions.inlineCharacterLimit === undefined) {
  66. tokens = {
  67. newLine: '\n',
  68. newLineOrSpace: '\n',
  69. pad: pad,
  70. indent: pad + combinedOptions.indent
  71. };
  72. }
  73. else {
  74. tokens = {
  75. newLine: '@@__PRETTY_PRINT_NEW_LINE__@@',
  76. newLineOrSpace: '@@__PRETTY_PRINT_NEW_LINE_OR_SPACE__@@',
  77. pad: '@@__PRETTY_PRINT_PAD__@@',
  78. indent: '@@__PRETTY_PRINT_INDENT__@@'
  79. };
  80. }
  81. var expandWhiteSpace = function (string) {
  82. if (combinedOptions.inlineCharacterLimit === undefined) {
  83. return string;
  84. }
  85. var oneLined = string
  86. .replace(new RegExp(tokens.newLine, 'g'), '')
  87. .replace(new RegExp(tokens.newLineOrSpace, 'g'), ' ')
  88. .replace(new RegExp(tokens.pad + '|' + tokens.indent, 'g'), '');
  89. if (oneLined.length <= combinedOptions.inlineCharacterLimit) {
  90. return oneLined;
  91. }
  92. return string
  93. .replace(new RegExp(tokens.newLine + '|' + tokens.newLineOrSpace, 'g'), '\n')
  94. .replace(new RegExp(tokens.pad, 'g'), pad)
  95. .replace(new RegExp(tokens.indent, 'g'), pad + combinedOptions.indent);
  96. };
  97. if (seen.indexOf(input) !== -1) {
  98. return '"[Circular]"';
  99. }
  100. if (input === null ||
  101. input === undefined ||
  102. typeof input === 'number' ||
  103. typeof input === 'boolean' ||
  104. typeof input === 'function' ||
  105. typeof input === 'symbol' ||
  106. isRegexp(input)) {
  107. return String(input);
  108. }
  109. if (input instanceof Date) {
  110. return "new Date('" + input.toISOString() + "')";
  111. }
  112. if (Array.isArray(input)) {
  113. if (input.length === 0) {
  114. return '[]';
  115. }
  116. seen.push(input);
  117. var ret = '[' + tokens.newLine + input.map(function (el, i) {
  118. var eol = input.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace;
  119. var value = prettyPrint(el, combinedOptions, pad + combinedOptions.indent);
  120. if (combinedOptions.transform) {
  121. value = combinedOptions.transform(input, i, value);
  122. }
  123. return tokens.indent + value + eol;
  124. }).join('') + tokens.pad + ']';
  125. seen.pop();
  126. return expandWhiteSpace(ret);
  127. }
  128. if (isObj(input)) {
  129. var objKeys_1 = __spreadArrays(Object.keys(input), (getOwnEnumPropSymbols(input)));
  130. if (combinedOptions.filter) {
  131. objKeys_1 = objKeys_1.filter(function (el) { return combinedOptions.filter && combinedOptions.filter(input, el); });
  132. }
  133. if (objKeys_1.length === 0) {
  134. return '{}';
  135. }
  136. seen.push(input);
  137. var ret = '{' + tokens.newLine + objKeys_1.map(function (el, i) {
  138. var eol = objKeys_1.length - 1 === i ? tokens.newLine : ',' + tokens.newLineOrSpace;
  139. var isSymbol = typeof el === 'symbol';
  140. var isClassic = !isSymbol && /^[a-z$_][a-z$_0-9]*$/i.test(el.toString());
  141. var key = isSymbol || isClassic ? el : prettyPrint(el, combinedOptions);
  142. var value = prettyPrint(input[el], combinedOptions, pad + combinedOptions.indent);
  143. if (combinedOptions.transform) {
  144. value = combinedOptions.transform(input, el, value);
  145. }
  146. return tokens.indent + String(key) + ': ' + value + eol;
  147. }).join('') + tokens.pad + '}';
  148. seen.pop();
  149. return expandWhiteSpace(ret);
  150. }
  151. input = String(input).replace(/[\r\n]/g, function (x) { return x === '\n' ? '\\n' : '\\r'; });
  152. if (!combinedOptions.singleQuotes) {
  153. input = input.replace(/"/g, '\\"');
  154. return "\"" + input + "\"";
  155. }
  156. input = input.replace(/\\?'/g, '\\\'');
  157. return "'" + input + "'";
  158. }
  159. exports.prettyPrint = prettyPrint;
  160. //# sourceMappingURL=index.js.map