inspect.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = inspect;
  6. var _nodejsCustomInspectSymbol = _interopRequireDefault(require("./nodejsCustomInspectSymbol"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function _typeof(obj) { 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. // $FlowFixMe(>=0.90.0)
  40. var customValue = customInspectFn.call(value); // check for infinite recursion
  41. if (customValue !== value) {
  42. return typeof customValue === 'string' ? customValue : formatValue(customValue, seenValues);
  43. }
  44. } else if (Array.isArray(value)) {
  45. return formatArray(value, seenValues);
  46. }
  47. return formatObject(value, seenValues);
  48. }
  49. function formatObject(object, seenValues) {
  50. var keys = Object.keys(object);
  51. if (keys.length === 0) {
  52. return '{}';
  53. }
  54. if (seenValues.length > MAX_RECURSIVE_DEPTH) {
  55. return '[' + getObjectTag(object) + ']';
  56. }
  57. var properties = keys.map(function (key) {
  58. var value = formatValue(object[key], seenValues);
  59. return key + ': ' + value;
  60. });
  61. return '{ ' + properties.join(', ') + ' }';
  62. }
  63. function formatArray(array, seenValues) {
  64. if (array.length === 0) {
  65. return '[]';
  66. }
  67. if (seenValues.length > MAX_RECURSIVE_DEPTH) {
  68. return '[Array]';
  69. }
  70. var len = Math.min(MAX_ARRAY_LENGTH, array.length);
  71. var remaining = array.length - len;
  72. var items = [];
  73. for (var i = 0; i < len; ++i) {
  74. items.push(formatValue(array[i], seenValues));
  75. }
  76. if (remaining === 1) {
  77. items.push('... 1 more item');
  78. } else if (remaining > 1) {
  79. items.push("... ".concat(remaining, " more items"));
  80. }
  81. return '[' + items.join(', ') + ']';
  82. }
  83. function getCustomFn(object) {
  84. var customInspectFn = object[String(_nodejsCustomInspectSymbol.default)];
  85. if (typeof customInspectFn === 'function') {
  86. return customInspectFn;
  87. }
  88. if (typeof object.inspect === 'function') {
  89. return object.inspect;
  90. }
  91. }
  92. function getObjectTag(object) {
  93. var tag = Object.prototype.toString.call(object).replace(/^\[object /, '').replace(/]$/, '');
  94. if (tag === 'Object' && typeof object.constructor === 'function') {
  95. var name = object.constructor.name;
  96. if (typeof name === 'string' && name !== '') {
  97. return name;
  98. }
  99. }
  100. return tag;
  101. }