inspect.mjs 2.5 KB

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