Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format buffer #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions lib/eyes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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(' ') : ' ';
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions test/eyes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ eyes.inspect({
func: function () {},
bool: false,
nil: null,
nilarr: [null],
undef: undefined,
object: {attr: []}
}, "native types");
Expand Down Expand Up @@ -47,6 +48,8 @@ eyes.inspect({
}, 'complex');


eyes.inspect(new Buffer(100), "buffer");

var inspect = eyes.inspector({ stream: null });

sys.puts(inspect('something', "something"));
Expand Down