eyes.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // Eyes.js - a customizable value inspector for Node.js
  3. //
  4. // usage:
  5. //
  6. // var inspect = require('eyes').inspector({styles: {all: 'magenta'}});
  7. // inspect(something); // inspect with the settings passed to `inspector`
  8. //
  9. // or
  10. //
  11. // var eyes = require('eyes');
  12. // eyes.inspect(something); // inspect with the default settings
  13. //
  14. var eyes = exports,
  15. stack = [];
  16. eyes.defaults = {
  17. styles: { // Styles applied to stdout
  18. all: 'cyan', // Overall style applied to everything
  19. label: 'underline', // Inspection labels, like 'array' in `array: [1, 2, 3]`
  20. other: 'inverted', // Objects which don't have a literal representation, such as functions
  21. key: 'bold', // The keys in object literals, like 'a' in `{a: 1}`
  22. special: 'grey', // null, undefined...
  23. string: 'green',
  24. number: 'magenta',
  25. bool: 'blue', // true false
  26. regexp: 'green', // /\d+/
  27. },
  28. pretty: true, // Indent object literals
  29. hideFunctions: false,
  30. showHidden: false,
  31. stream: process.stdout,
  32. maxLength: 2048 // Truncate output if longer
  33. };
  34. // Return a curried inspect() function, with the `options` argument filled in.
  35. eyes.inspector = function (options) {
  36. var that = this;
  37. return function (obj, label, opts) {
  38. return that.inspect.call(that, obj, label,
  39. merge(options || {}, opts || {}));
  40. };
  41. };
  42. // If we have a `stream` defined, use it to print a styled string,
  43. // if not, we just return the stringified object.
  44. eyes.inspect = function (obj, label, options) {
  45. options = merge(this.defaults, options || {});
  46. if (options.stream) {
  47. return this.print(stringify(obj, options), label, options);
  48. } else {
  49. return stringify(obj, options) + (options.styles ? '\033[39m' : '');
  50. }
  51. };
  52. // Output using the 'stream', and an optional label
  53. // Loop through `str`, and truncate it after `options.maxLength` has been reached.
  54. // Because escape sequences are, at this point embeded within
  55. // the output string, we can't measure the length of the string
  56. // in a useful way, without separating what is an escape sequence,
  57. // versus a printable character (`c`). So we resort to counting the
  58. // length manually.
  59. eyes.print = function (str, label, options) {
  60. for (var c = 0, i = 0; i < str.length; i++) {
  61. if (str.charAt(i) === '\033') { i += 4 } // `4` because '\033[25m'.length + 1 == 5
  62. else if (c === options.maxLength) {
  63. str = str.slice(0, i - 1) + '…';
  64. break;
  65. } else { c++ }
  66. }
  67. return options.stream.write.call(options.stream, (label ?
  68. this.stylize(label, options.styles.label, options.styles) + ': ' : '') +
  69. this.stylize(str, options.styles.all, options.styles) + '\033[0m' + "\n");
  70. };
  71. // Apply a style to a string, eventually,
  72. // I'd like this to support passing multiple
  73. // styles.
  74. eyes.stylize = function (str, style, styles) {
  75. var codes = {
  76. 'bold' : [1, 22],
  77. 'underline' : [4, 24],
  78. 'inverse' : [7, 27],
  79. 'cyan' : [36, 39],
  80. 'magenta' : [35, 39],
  81. 'blue' : [34, 39],
  82. 'yellow' : [33, 39],
  83. 'green' : [32, 39],
  84. 'red' : [31, 39],
  85. 'grey' : [90, 39]
  86. }, endCode;
  87. if (style && codes[style]) {
  88. endCode = (codes[style][1] === 39 && styles.all) ? codes[styles.all][0]
  89. : codes[style][1];
  90. return '\033[' + codes[style][0] + 'm' + str +
  91. '\033[' + endCode + 'm';
  92. } else { return str }
  93. };
  94. // Convert any object to a string, ready for output.
  95. // When an 'array' or an 'object' are encountered, they are
  96. // passed to specialized functions, which can then recursively call
  97. // stringify().
  98. function stringify(obj, options) {
  99. var that = this, stylize = function (str, style) {
  100. return eyes.stylize(str, options.styles[style], options.styles)
  101. }, index, result;
  102. if ((index = stack.indexOf(obj)) !== -1) {
  103. return stylize(new(Array)(stack.length - index + 1).join('.'), 'special');
  104. }
  105. stack.push(obj);
  106. result = (function (obj) {
  107. switch (typeOf(obj)) {
  108. case "string" : obj = stringifyString(obj.indexOf("'") === -1 ? "'" + obj + "'"
  109. : '"' + obj + '"');
  110. return stylize(obj, 'string');
  111. case "regexp" : return stylize('/' + obj.source + '/', 'regexp');
  112. case "number" : return stylize(obj + '', 'number');
  113. case "function" : return options.stream ? stylize("Function", 'other') : '[Function]';
  114. case "null" : return stylize("null", 'special');
  115. case "undefined": return stylize("undefined", 'special');
  116. case "boolean" : return stylize(obj + '', 'bool');
  117. case "date" : return stylize(obj.toUTCString());
  118. case "array" : return stringifyArray(obj, options, stack.length);
  119. case "object" : return stringifyObject(obj, options, stack.length);
  120. }
  121. })(obj);
  122. stack.pop();
  123. return result;
  124. };
  125. // Escape invisible characters in a string
  126. function stringifyString (str, options) {
  127. return str.replace(/\\/g, '\\\\')
  128. .replace(/\n/g, '\\n')
  129. .replace(/[\u0001-\u001F]/g, function (match) {
  130. return '\\0' + match[0].charCodeAt(0).toString(8);
  131. });
  132. }
  133. // Convert an array to a string, such as [1, 2, 3].
  134. // This function calls stringify() for each of the elements
  135. // in the array.
  136. function stringifyArray(ary, options, level) {
  137. var out = [];
  138. var pretty = options.pretty && (ary.length > 4 || ary.some(function (o) {
  139. return (o !== null && typeof(o) === 'object' && Object.keys(o).length > 0) ||
  140. (Array.isArray(o) && o.length > 0);
  141. }));
  142. var ws = pretty ? '\n' + new(Array)(level * 4 + 1).join(' ') : ' ';
  143. for (var i = 0; i < ary.length; i++) {
  144. out.push(stringify(ary[i], options));
  145. }
  146. if (out.length === 0) {
  147. return '[]';
  148. } else {
  149. return '[' + ws
  150. + out.join(',' + (pretty ? ws : ' '))
  151. + (pretty ? ws.slice(0, -4) : ws) +
  152. ']';
  153. }
  154. };
  155. // Convert an object to a string, such as {a: 1}.
  156. // This function calls stringify() for each of its values,
  157. // and does not output functions or prototype values.
  158. function stringifyObject(obj, options, level) {
  159. var out = [];
  160. var pretty = options.pretty && (Object.keys(obj).length > 2 ||
  161. Object.keys(obj).some(function (k) { return typeof(obj[k]) === 'object' }));
  162. var ws = pretty ? '\n' + new(Array)(level * 4 + 1).join(' ') : ' ';
  163. var keys = options.showHidden ? Object.keys(obj) : Object.getOwnPropertyNames(obj);
  164. keys.forEach(function (k) {
  165. if (Object.prototype.hasOwnProperty.call(obj, k)
  166. && !(obj[k] instanceof Function && options.hideFunctions)) {
  167. out.push(eyes.stylize(k, options.styles.key, options.styles) + ': ' +
  168. stringify(obj[k], options));
  169. }
  170. });
  171. if (out.length === 0) {
  172. return '{}';
  173. } else {
  174. return "{" + ws
  175. + out.join(',' + (pretty ? ws : ' '))
  176. + (pretty ? ws.slice(0, -4) : ws) +
  177. "}";
  178. }
  179. };
  180. // A better `typeof`
  181. function typeOf(value) {
  182. var s = typeof(value),
  183. types = [Object, Array, String, RegExp, Number, Function, Boolean, Date];
  184. if (s === 'object' || s === 'function') {
  185. if (value) {
  186. types.forEach(function (t) {
  187. if (value instanceof t) { s = t.name.toLowerCase() }
  188. });
  189. } else { s = 'null' }
  190. }
  191. return s;
  192. }
  193. function merge(/* variable args */) {
  194. var objs = Array.prototype.slice.call(arguments);
  195. var target = {};
  196. objs.forEach(function (o) {
  197. Object.keys(o).forEach(function (k) {
  198. if (k === 'styles') {
  199. if (! o.styles) {
  200. target.styles = false;
  201. } else {
  202. target.styles = {}
  203. for (var s in o.styles) {
  204. target.styles[s] = o.styles[s];
  205. }
  206. }
  207. } else {
  208. target[k] = o[k];
  209. }
  210. });
  211. });
  212. return target;
  213. }