Skip to content

Commit 36ed390

Browse files
committed
Merge pull request #39 from danielb2/constructor.name
prints constructor name if availabel
2 parents 433be35 + 9cb797c commit 36ed390

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

lib/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var internals = {
2323
depth: 'grey',
2424
error: 'red',
2525
function: 'cyan',
26+
prefix: 'green',
2627
path: 'blue'
2728
},
2829
defaults: {
@@ -124,8 +125,10 @@ internals.purdy.prototype.Object = internals.purdy.prototype.process = internals
124125
keys = keys.concat(Object.getOwnPropertySymbols(object));
125126
}
126127

128+
var prefix = ['Object', 'Function', 'Error', ''].indexOf(object.constructor.name) !== -1 ? '' : this.colorize(object.constructor.name, 'prefix') + ' ';
129+
127130
if (keys.length === 0) {
128-
return '{}';
131+
return prefix + '{}';
129132
}
130133

131134
++depth;
@@ -138,7 +141,7 @@ internals.purdy.prototype.Object = internals.purdy.prototype.process = internals
138141

139142
var keyLengths = Hoek.clone(keys);
140143
this.indentLevel = this.indentLevel + 1;
141-
var out = '{\n';
144+
var out = prefix + '{\n';
142145

143146
for (var i = 0, il = keys.length; i < il; ++i) {
144147
var key = keys[i];

test/purdy.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ describe('Purdy', function () {
112112

113113
describe('functions', function () {
114114

115+
it('should print constructor name', function (done) {
116+
117+
var mises = function mises () { this.moop = 3 }
118+
var obj = { instance: new mises() };
119+
var out = Purdy.stringify(obj, { indent: 1 });
120+
expect(out).to.equal('{\n \u001b[1m\u001b[37minstance\u001b[39m\u001b[22m: \u001b[32mmises\u001b[39m {\n \u001b[1m\u001b[37mmoop\u001b[39m\u001b[22m: \u001b[1m\u001b[34m3\u001b[39m\u001b[22m\n }\n}');
121+
done();
122+
});
123+
124+
it('should print not print common constructor', function (done) {
125+
126+
var mises = function () { this.moop = 3 }
127+
var obj = { instance: new mises() };
128+
var out = Purdy.stringify(obj, { indent: 1 });
129+
expect(out).to.equal('{\n \u001b[1m\u001b[37minstance\u001b[39m\u001b[22m: {\n \u001b[1m\u001b[37mmoop\u001b[39m\u001b[22m: \u001b[1m\u001b[34m3\u001b[39m\u001b[22m\n }\n}');
130+
done();
131+
});
132+
115133
it('should print a function', function (done) {
116134

117135
var out = Purdy.stringify(Array.isArray);

0 commit comments

Comments
 (0)