inspect.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // This is (almost) directly from Node.js utils
  2. // https://github.com/joyent/node/blob/f8c335d0caf47f16d31413f89aa28eda3878e3aa/lib/util.js
  3. var getName = require('./getName');
  4. var getProperties = require('./getProperties');
  5. var getEnumerableProperties = require('./getEnumerableProperties');
  6. module.exports = inspect;
  7. /**
  8. * Echos the value of a value. Trys to print the value out
  9. * in the best way possible given the different types.
  10. *
  11. * @param {Object} obj The object to print out.
  12. * @param {Boolean} showHidden Flag that shows hidden (not enumerable)
  13. * properties of objects.
  14. * @param {Number} depth Depth in which to descend in object. Default is 2.
  15. * @param {Boolean} colors Flag to turn on ANSI escape codes to color the
  16. * output. Default is false (no coloring).
  17. */
  18. function inspect(obj, showHidden, depth, colors) {
  19. var ctx = {
  20. showHidden: showHidden,
  21. seen: [],
  22. stylize: function (str) { return str; }
  23. };
  24. return formatValue(ctx, obj, (typeof depth === 'undefined' ? 2 : depth));
  25. }
  26. // Returns true if object is a DOM element.
  27. var isDOMElement = function (object) {
  28. if (typeof HTMLElement === 'object') {
  29. return object instanceof HTMLElement;
  30. } else {
  31. return object &&
  32. typeof object === 'object' &&
  33. object.nodeType === 1 &&
  34. typeof object.nodeName === 'string';
  35. }
  36. };
  37. function formatValue(ctx, value, recurseTimes) {
  38. // Provide a hook for user-specified inspect functions.
  39. // Check that value is an object with an inspect function on it
  40. if (value && typeof value.inspect === 'function' &&
  41. // Filter out the util module, it's inspect function is special
  42. value.inspect !== exports.inspect &&
  43. // Also filter out any prototype objects using the circular check.
  44. !(value.constructor && value.constructor.prototype === value)) {
  45. var ret = value.inspect(recurseTimes);
  46. if (typeof ret !== 'string') {
  47. ret = formatValue(ctx, ret, recurseTimes);
  48. }
  49. return ret;
  50. }
  51. // Primitive types cannot have properties
  52. var primitive = formatPrimitive(ctx, value);
  53. if (primitive) {
  54. return primitive;
  55. }
  56. // If this is a DOM element, try to get the outer HTML.
  57. if (isDOMElement(value)) {
  58. if ('outerHTML' in value) {
  59. return value.outerHTML;
  60. // This value does not have an outerHTML attribute,
  61. // it could still be an XML element
  62. } else {
  63. // Attempt to serialize it
  64. try {
  65. if (document.xmlVersion) {
  66. var xmlSerializer = new XMLSerializer();
  67. return xmlSerializer.serializeToString(value);
  68. } else {
  69. // Firefox 11- do not support outerHTML
  70. // It does, however, support innerHTML
  71. // Use the following to render the element
  72. var ns = "http://www.w3.org/1999/xhtml";
  73. var container = document.createElementNS(ns, '_');
  74. container.appendChild(value.cloneNode(false));
  75. html = container.innerHTML
  76. .replace('><', '>' + value.innerHTML + '<');
  77. container.innerHTML = '';
  78. return html;
  79. }
  80. } catch (err) {
  81. // This could be a non-native DOM implementation,
  82. // continue with the normal flow:
  83. // printing the element as if it is an object.
  84. }
  85. }
  86. }
  87. // Look up the keys of the object.
  88. var visibleKeys = getEnumerableProperties(value);
  89. var keys = ctx.showHidden ? getProperties(value) : visibleKeys;
  90. // Some type of object without properties can be shortcutted.
  91. // In IE, errors have a single `stack` property, or if they are vanilla `Error`,
  92. // a `stack` plus `description` property; ignore those for consistency.
  93. if (keys.length === 0 || (isError(value) && (
  94. (keys.length === 1 && keys[0] === 'stack') ||
  95. (keys.length === 2 && keys[0] === 'description' && keys[1] === 'stack')
  96. ))) {
  97. if (typeof value === 'function') {
  98. var name = getName(value);
  99. var nameSuffix = name ? ': ' + name : '';
  100. return ctx.stylize('[Function' + nameSuffix + ']', 'special');
  101. }
  102. if (isRegExp(value)) {
  103. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  104. }
  105. if (isDate(value)) {
  106. return ctx.stylize(Date.prototype.toUTCString.call(value), 'date');
  107. }
  108. if (isError(value)) {
  109. return formatError(value);
  110. }
  111. }
  112. var base = '', array = false, braces = ['{', '}'];
  113. // Make Array say that they are Array
  114. if (isArray(value)) {
  115. array = true;
  116. braces = ['[', ']'];
  117. }
  118. // Make functions say that they are functions
  119. if (typeof value === 'function') {
  120. var name = getName(value);
  121. var nameSuffix = name ? ': ' + name : '';
  122. base = ' [Function' + nameSuffix + ']';
  123. }
  124. // Make RegExps say that they are RegExps
  125. if (isRegExp(value)) {
  126. base = ' ' + RegExp.prototype.toString.call(value);
  127. }
  128. // Make dates with properties first say the date
  129. if (isDate(value)) {
  130. base = ' ' + Date.prototype.toUTCString.call(value);
  131. }
  132. // Make error with message first say the error
  133. if (isError(value)) {
  134. return formatError(value);
  135. }
  136. if (keys.length === 0 && (!array || value.length == 0)) {
  137. return braces[0] + base + braces[1];
  138. }
  139. if (recurseTimes < 0) {
  140. if (isRegExp(value)) {
  141. return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
  142. } else {
  143. return ctx.stylize('[Object]', 'special');
  144. }
  145. }
  146. ctx.seen.push(value);
  147. var output;
  148. if (array) {
  149. output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
  150. } else {
  151. output = keys.map(function(key) {
  152. return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
  153. });
  154. }
  155. ctx.seen.pop();
  156. return reduceToSingleString(output, base, braces);
  157. }
  158. function formatPrimitive(ctx, value) {
  159. switch (typeof value) {
  160. case 'undefined':
  161. return ctx.stylize('undefined', 'undefined');
  162. case 'string':
  163. var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
  164. .replace(/'/g, "\\'")
  165. .replace(/\\"/g, '"') + '\'';
  166. return ctx.stylize(simple, 'string');
  167. case 'number':
  168. if (value === 0 && (1/value) === -Infinity) {
  169. return ctx.stylize('-0', 'number');
  170. }
  171. return ctx.stylize('' + value, 'number');
  172. case 'boolean':
  173. return ctx.stylize('' + value, 'boolean');
  174. }
  175. // For some reason typeof null is "object", so special case here.
  176. if (value === null) {
  177. return ctx.stylize('null', 'null');
  178. }
  179. }
  180. function formatError(value) {
  181. return '[' + Error.prototype.toString.call(value) + ']';
  182. }
  183. function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
  184. var output = [];
  185. for (var i = 0, l = value.length; i < l; ++i) {
  186. if (Object.prototype.hasOwnProperty.call(value, String(i))) {
  187. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  188. String(i), true));
  189. } else {
  190. output.push('');
  191. }
  192. }
  193. keys.forEach(function(key) {
  194. if (!key.match(/^\d+$/)) {
  195. output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
  196. key, true));
  197. }
  198. });
  199. return output;
  200. }
  201. function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
  202. var name, str;
  203. if (value.__lookupGetter__) {
  204. if (value.__lookupGetter__(key)) {
  205. if (value.__lookupSetter__(key)) {
  206. str = ctx.stylize('[Getter/Setter]', 'special');
  207. } else {
  208. str = ctx.stylize('[Getter]', 'special');
  209. }
  210. } else {
  211. if (value.__lookupSetter__(key)) {
  212. str = ctx.stylize('[Setter]', 'special');
  213. }
  214. }
  215. }
  216. if (visibleKeys.indexOf(key) < 0) {
  217. name = '[' + key + ']';
  218. }
  219. if (!str) {
  220. if (ctx.seen.indexOf(value[key]) < 0) {
  221. if (recurseTimes === null) {
  222. str = formatValue(ctx, value[key], null);
  223. } else {
  224. str = formatValue(ctx, value[key], recurseTimes - 1);
  225. }
  226. if (str.indexOf('\n') > -1) {
  227. if (array) {
  228. str = str.split('\n').map(function(line) {
  229. return ' ' + line;
  230. }).join('\n').substr(2);
  231. } else {
  232. str = '\n' + str.split('\n').map(function(line) {
  233. return ' ' + line;
  234. }).join('\n');
  235. }
  236. }
  237. } else {
  238. str = ctx.stylize('[Circular]', 'special');
  239. }
  240. }
  241. if (typeof name === 'undefined') {
  242. if (array && key.match(/^\d+$/)) {
  243. return str;
  244. }
  245. name = JSON.stringify('' + key);
  246. if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
  247. name = name.substr(1, name.length - 2);
  248. name = ctx.stylize(name, 'name');
  249. } else {
  250. name = name.replace(/'/g, "\\'")
  251. .replace(/\\"/g, '"')
  252. .replace(/(^"|"$)/g, "'");
  253. name = ctx.stylize(name, 'string');
  254. }
  255. }
  256. return name + ': ' + str;
  257. }
  258. function reduceToSingleString(output, base, braces) {
  259. var numLinesEst = 0;
  260. var length = output.reduce(function(prev, cur) {
  261. numLinesEst++;
  262. if (cur.indexOf('\n') >= 0) numLinesEst++;
  263. return prev + cur.length + 1;
  264. }, 0);
  265. if (length > 60) {
  266. return braces[0] +
  267. (base === '' ? '' : base + '\n ') +
  268. ' ' +
  269. output.join(',\n ') +
  270. ' ' +
  271. braces[1];
  272. }
  273. return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
  274. }
  275. function isArray(ar) {
  276. return Array.isArray(ar) ||
  277. (typeof ar === 'object' && objectToString(ar) === '[object Array]');
  278. }
  279. function isRegExp(re) {
  280. return typeof re === 'object' && objectToString(re) === '[object RegExp]';
  281. }
  282. function isDate(d) {
  283. return typeof d === 'object' && objectToString(d) === '[object Date]';
  284. }
  285. function isError(e) {
  286. return typeof e === 'object' && objectToString(e) === '[object Error]';
  287. }
  288. function objectToString(o) {
  289. return Object.prototype.toString.call(o);
  290. }