diff --git a/lib/eyes.js b/lib/eyes.js index 23caafe..8d94500 100644 --- a/lib/eyes.js +++ b/lib/eyes.js @@ -26,6 +26,14 @@ eyes.defaults = { bool: 'blue', // true false regexp: 'green', // /\d+/ }, + hexy: { + width: 16, + numbering: 'hex_bytes', + format: 'twos', + caps: 'lower', + annotate: 'ascii', + prefix: '', + }, pretty: true, // Indent object literals hideFunctions: false, stream: process.stdout, @@ -45,6 +53,8 @@ eyes.inspector = function (options) { // if not, we just return the stringified object. eyes.inspect = function (obj, label, options) { options = merge(this.defaults, options || {}); + if (options.hexy !== false) + options.hexy = merge(this.defaults.hexy, options.hexy || {}); if (options.stream) { return this.print(stringify(obj, options), label, options); @@ -126,6 +136,7 @@ function stringify(obj, options) { case "date" : return stylize(obj.toUTCString()); case "array" : return stringifyArray(obj, options, stack.length); case "object" : return stringifyObject(obj, options, stack.length); + case "buffer" : return stringifyBuffer(obj, options, stack.length); } })(obj); @@ -148,7 +159,7 @@ function stringifyString (str, options) { function stringifyArray(ary, options, level) { var out = []; var pretty = options.pretty && (ary.length > 4 || ary.some(function (o) { - return (typeof(o) === 'object' && Object.keys(o).length > 0) || + return (typeof(o) === 'object' && o && Object.keys(o).length > 0) || (Array.isArray(o) && o.length > 0); })); var ws = pretty ? '\n' + new(Array)(level * 4 + 1).join(' ') : ' '; @@ -193,10 +204,30 @@ function stringifyObject(obj, options, level) { } }; +// Convert a buffer to string such as 00000000: 00 when hexy is installed +// otherwise it shows the Buffer as Object +var hexy = null; +try {hexy = require('hexy').hexy} catch(e) {}; +function stringifyBuffer(obj, options, level) { + if (hexy === null || !options.hexy || !options.pretty) + return stringifyObject(obj, options, level); + + var opts = {}; + Object.keys(options.hexy).forEach(function (k) { + if (options.hexy.hasOwnProperty(k)) + opts[k] = options.hexy[k]; + }); + if (opts.indent === undefined) + opts.indent = level * 4 + 1; + + var out = hexy(obj, opts); + return (out.split('\n').length > 2 ? '\n':'') + out; +}; + // A better `typeof` function typeOf(value) { var s = typeof(value), - types = [Object, Array, String, RegExp, Number, Function, Boolean, Date]; + types = [Object, Array, Buffer, String, RegExp, Number, Function, Boolean, Date]; if (s === 'object' || s === 'function') { if (value) { diff --git a/test/eyes-test.js b/test/eyes-test.js index d3a1d26..93c19b7 100644 --- a/test/eyes-test.js +++ b/test/eyes-test.js @@ -9,6 +9,7 @@ eyes.inspect({ func: function () {}, bool: false, nil: null, + nilarr: [null], undef: undefined, object: {attr: []} }, "native types"); @@ -47,6 +48,8 @@ eyes.inspect({ }, 'complex'); +eyes.inspect(new Buffer(100), "buffer"); + var inspect = eyes.inspector({ stream: null }); sys.puts(inspect('something', "something"));