inspect.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = inspect;
  6. var _nodejsCustomInspectSymbol = _interopRequireDefault(require("./nodejsCustomInspectSymbol.js"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  9. var MAX_ARRAY_LENGTH = 10;
  10. var MAX_RECURSIVE_DEPTH = 2;
  11. /**
  12. * Used to print values in error messages.
  13. */
  14. function inspect(value) {
  15. return formatValue(value, []);
  16. }
  17. function formatValue(value, seenValues) {
  18. switch (_typeof(value)) {
  19. case 'string':
  20. return JSON.stringify(value);
  21. case 'function':
  22. return value.name ? "[function ".concat(value.name, "]") : '[function]';
  23. case 'object':
  24. if (value === null) {
  25. return 'null';
  26. }
  27. return formatObjectValue(value, seenValues);
  28. default:
  29. return String(value);
  30. }
  31. }
  32. function formatObjectValue(value, previouslySeenValues) {
  33. if (previouslySeenValues.indexOf(value) !== -1) {
  34. return '[Circular]';
  35. }
  36. var seenValues = [].concat(previouslySeenValues, [value]);
  37. var customInspectFn = getCustomFn(value);
  38. if (customInspectFn !== undefined) {
  39. var customValue = customInspectFn.call(value); // check for infinite recursion
  40. if (customValue !== value) {
  41. return typeof customValue === 'string' ? customValue : formatValue(customValue, seenValues);
  42. }
  43. } else if (Array.isArray(value)) {
  44. return formatArray(value, seenValues);
  45. }
  46. return formatObject(value, seenValues);
  47. }
  48. function formatObject(object, seenValues) {
  49. var keys = Object.keys(object);
  50. if (keys.length === 0) {
  51. return '{}';
  52. }
  53. if (seenValues.length > MAX_RECURSIVE_DEPTH) {
  54. return '[' + getObjectTag(object) + ']';
  55. }
  56. var properties = keys.map(function (key) {
  57. var value = formatValue(object[key], seenValues);
  58. return key + ': ' + value;
  59. });
  60. return '{ ' + properties.join(', ') + ' }';
  61. }
  62. function formatArray(array, seenValues) {
  63. if (array.length === 0) {
  64. return '[]';
  65. }
  66. if (seenValues.length > MAX_RECURSIVE_DEPTH) {
  67. return '[Array]';
  68. }
  69. var len = Math.min(MAX_ARRAY_LENGTH, array.length);
  70. var remaining = array.length - len;
  71. var items = [];
  72. for (var i = 0; i < len; ++i) {
  73. items.push(formatValue(array[i], seenValues));
  74. }
  75. if (remaining === 1) {
  76. items.push('... 1 more item');
  77. } else if (remaining > 1) {
  78. items.push("... ".concat(remaining, " more items"));
  79. }
  80. return '[' + items.join(', ') + ']';
  81. }
  82. function getCustomFn(object) {
  83. var customInspectFn = object[String(_nodejsCustomInspectSymbol.default)];
  84. if (typeof customInspectFn === 'function') {
  85. return customInspectFn;
  86. }
  87. if (typeof object.inspect === 'function') {
  88. return object.inspect;
  89. }
  90. }
  91. function getObjectTag(object) {
  92. var tag = Object.prototype.toString.call(object).replace(/^\[object /, '').replace(/]$/, '');
  93. if (tag === 'Object' && typeof object.constructor === 'function') {
  94. var name = object.constructor.name;
  95. if (typeof name === 'string' && name !== '') {
  96. return name;
  97. }
  98. }
  99. return tag;
  100. }