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

fixes logging of array values (issue #10) #13

Open
wants to merge 2 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
10 changes: 8 additions & 2 deletions lib/formatters/human.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = function(options, severity, date, elems) {
var strVal = '';
try {
strVal = val.toString();
if (strVal === '[object Object]') {
if (isObject(val)) {
// Pretty-print objects with JSON
strVal = ("\n" + JSON.stringify(val, null, " "))
.replace(/\n/g, "\n\t ");
Expand All @@ -77,8 +77,14 @@ module.exports = function(options, severity, date, elems) {
return msg;
};

function isObject(obj) {
// This logic was copied from underscore.js
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
};

function sanitize(str) {
if (str)
str.replace(/\n\s*/g, "\n\t\t");
return str;
}
}
6 changes: 5 additions & 1 deletion test/formatters/human.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe('Human Formatter', function() {
'Hello!',
{is_it: 'me', "you're": 'looking for'},
{file: __filename, line: 82},
{obj: { str: "hello" }}
{obj: { str: "hello" }},
{ objs: [{a: 1}, {a: 2}]}
]);
});
it("should output multiple lines", function() {
Expand All @@ -36,4 +37,7 @@ describe('Human Formatter', function() {
msg.should.not.match(/\[object Object\]/);
msg.should.match(/"str": "hello"/);
});
it("should stringify arrays of object literals", function() {
msg.should.not.match(/objs\: \[object Object\],\[object Object\]/);
});
});