inspect.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.inspect = inspect;
  6. const MAX_ARRAY_LENGTH = 10;
  7. const MAX_RECURSIVE_DEPTH = 2;
  8. /**
  9. * Used to print values in error messages.
  10. */
  11. function inspect(value) {
  12. return formatValue(value, []);
  13. }
  14. function formatValue(value, seenValues) {
  15. switch (typeof value) {
  16. case 'string':
  17. return JSON.stringify(value);
  18. case 'function':
  19. return value.name ? `[function ${value.name}]` : '[function]';
  20. case 'object':
  21. return formatObjectValue(value, seenValues);
  22. default:
  23. return String(value);
  24. }
  25. }
  26. function formatObjectValue(value, previouslySeenValues) {
  27. if (value === null) {
  28. return 'null';
  29. }
  30. if (previouslySeenValues.includes(value)) {
  31. return '[Circular]';
  32. }
  33. const seenValues = [...previouslySeenValues, value];
  34. if (isJSONable(value)) {
  35. const jsonValue = value.toJSON(); // check for infinite recursion
  36. if (jsonValue !== value) {
  37. return typeof jsonValue === 'string'
  38. ? jsonValue
  39. : formatValue(jsonValue, seenValues);
  40. }
  41. } else if (Array.isArray(value)) {
  42. return formatArray(value, seenValues);
  43. }
  44. return formatObject(value, seenValues);
  45. }
  46. function isJSONable(value) {
  47. return typeof value.toJSON === 'function';
  48. }
  49. function formatObject(object, seenValues) {
  50. const entries = Object.entries(object);
  51. if (entries.length === 0) {
  52. return '{}';
  53. }
  54. if (seenValues.length > MAX_RECURSIVE_DEPTH) {
  55. return '[' + getObjectTag(object) + ']';
  56. }
  57. const properties = entries.map(
  58. ([key, value]) => key + ': ' + formatValue(value, seenValues),
  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. const len = Math.min(MAX_ARRAY_LENGTH, array.length);
  70. const remaining = array.length - len;
  71. const items = [];
  72. for (let 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(`... ${remaining} more items`);
  79. }
  80. return '[' + items.join(', ') + ']';
  81. }
  82. function getObjectTag(object) {
  83. const tag = Object.prototype.toString
  84. .call(object)
  85. .replace(/^\[object /, '')
  86. .replace(/]$/, '');
  87. if (tag === 'Object' && typeof object.constructor === 'function') {
  88. const name = object.constructor.name;
  89. if (typeof name === 'string' && name !== '') {
  90. return name;
  91. }
  92. }
  93. return tag;
  94. }