inspect.js.flow 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // @flow strict
  2. import nodejsCustomInspectSymbol from './nodejsCustomInspectSymbol';
  3. const MAX_ARRAY_LENGTH = 10;
  4. const MAX_RECURSIVE_DEPTH = 2;
  5. /**
  6. * Used to print values in error messages.
  7. */
  8. export default function inspect(value: mixed): string {
  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 ${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. const seenValues = [...previouslySeenValues, value];
  31. const customInspectFn = getCustomFn(value);
  32. if (customInspectFn !== undefined) {
  33. // $FlowFixMe(>=0.90.0)
  34. const customValue = customInspectFn.call(value);
  35. // check for infinite recursion
  36. if (customValue !== value) {
  37. return typeof customValue === 'string'
  38. ? customValue
  39. : formatValue(customValue, seenValues);
  40. }
  41. } else if (Array.isArray(value)) {
  42. return formatArray(value, seenValues);
  43. }
  44. return formatObject(value, seenValues);
  45. }
  46. function formatObject(object, seenValues) {
  47. const keys = Object.keys(object);
  48. if (keys.length === 0) {
  49. return '{}';
  50. }
  51. if (seenValues.length > MAX_RECURSIVE_DEPTH) {
  52. return '[' + getObjectTag(object) + ']';
  53. }
  54. const properties = keys.map(key => {
  55. const value = formatValue(object[key], seenValues);
  56. return key + ': ' + value;
  57. });
  58. return '{ ' + properties.join(', ') + ' }';
  59. }
  60. function formatArray(array, seenValues) {
  61. if (array.length === 0) {
  62. return '[]';
  63. }
  64. if (seenValues.length > MAX_RECURSIVE_DEPTH) {
  65. return '[Array]';
  66. }
  67. const len = Math.min(MAX_ARRAY_LENGTH, array.length);
  68. const remaining = array.length - len;
  69. const items = [];
  70. for (let i = 0; i < len; ++i) {
  71. items.push(formatValue(array[i], seenValues));
  72. }
  73. if (remaining === 1) {
  74. items.push('... 1 more item');
  75. } else if (remaining > 1) {
  76. items.push(`... ${remaining} more items`);
  77. }
  78. return '[' + items.join(', ') + ']';
  79. }
  80. function getCustomFn(object) {
  81. const customInspectFn = object[String(nodejsCustomInspectSymbol)];
  82. if (typeof customInspectFn === 'function') {
  83. return customInspectFn;
  84. }
  85. if (typeof object.inspect === 'function') {
  86. return object.inspect;
  87. }
  88. }
  89. function getObjectTag(object) {
  90. const tag = Object.prototype.toString
  91. .call(object)
  92. .replace(/^\[object /, '')
  93. .replace(/]$/, '');
  94. if (tag === 'Object' && typeof object.constructor === 'function') {
  95. const name = object.constructor.name;
  96. if (typeof name === 'string' && name !== '') {
  97. return name;
  98. }
  99. }
  100. return tag;
  101. }