UNPKG

3.48 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = inspect;
7
8var _nodejsCustomInspectSymbol = _interopRequireDefault(require("./nodejsCustomInspectSymbol.js"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12function _typeof(obj) { "@babel/helpers - typeof"; 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); }
13
14var MAX_ARRAY_LENGTH = 10;
15var MAX_RECURSIVE_DEPTH = 2;
16/**
17 * Used to print values in error messages.
18 */
19
20function inspect(value) {
21 return formatValue(value, []);
22}
23
24function formatValue(value, seenValues) {
25 switch (_typeof(value)) {
26 case 'string':
27 return JSON.stringify(value);
28
29 case 'function':
30 return value.name ? "[function ".concat(value.name, "]") : '[function]';
31
32 case 'object':
33 if (value === null) {
34 return 'null';
35 }
36
37 return formatObjectValue(value, seenValues);
38
39 default:
40 return String(value);
41 }
42}
43
44function formatObjectValue(value, previouslySeenValues) {
45 if (previouslySeenValues.indexOf(value) !== -1) {
46 return '[Circular]';
47 }
48
49 var seenValues = [].concat(previouslySeenValues, [value]);
50 var customInspectFn = getCustomFn(value);
51
52 if (customInspectFn !== undefined) {
53 var customValue = customInspectFn.call(value); // check for infinite recursion
54
55 if (customValue !== value) {
56 return typeof customValue === 'string' ? customValue : formatValue(customValue, seenValues);
57 }
58 } else if (Array.isArray(value)) {
59 return formatArray(value, seenValues);
60 }
61
62 return formatObject(value, seenValues);
63}
64
65function formatObject(object, seenValues) {
66 var keys = Object.keys(object);
67
68 if (keys.length === 0) {
69 return '{}';
70 }
71
72 if (seenValues.length > MAX_RECURSIVE_DEPTH) {
73 return '[' + getObjectTag(object) + ']';
74 }
75
76 var properties = keys.map(function (key) {
77 var value = formatValue(object[key], seenValues);
78 return key + ': ' + value;
79 });
80 return '{ ' + properties.join(', ') + ' }';
81}
82
83function formatArray(array, seenValues) {
84 if (array.length === 0) {
85 return '[]';
86 }
87
88 if (seenValues.length > MAX_RECURSIVE_DEPTH) {
89 return '[Array]';
90 }
91
92 var len = Math.min(MAX_ARRAY_LENGTH, array.length);
93 var remaining = array.length - len;
94 var items = [];
95
96 for (var i = 0; i < len; ++i) {
97 items.push(formatValue(array[i], seenValues));
98 }
99
100 if (remaining === 1) {
101 items.push('... 1 more item');
102 } else if (remaining > 1) {
103 items.push("... ".concat(remaining, " more items"));
104 }
105
106 return '[' + items.join(', ') + ']';
107}
108
109function getCustomFn(object) {
110 var customInspectFn = object[String(_nodejsCustomInspectSymbol.default)];
111
112 if (typeof customInspectFn === 'function') {
113 return customInspectFn;
114 }
115
116 if (typeof object.inspect === 'function') {
117 return object.inspect;
118 }
119}
120
121function getObjectTag(object) {
122 var tag = Object.prototype.toString.call(object).replace(/^\[object /, '').replace(/]$/, '');
123
124 if (tag === 'Object' && typeof object.constructor === 'function') {
125 var name = object.constructor.name;
126
127 if (typeof name === 'string' && name !== '') {
128 return name;
129 }
130 }
131
132 return tag;
133}