inspect.mjs 3.2 KB

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