Skip to content

Commit 142490c

Browse files
committed
depth printing
1 parent 45d0831 commit 142490c

File tree

4 files changed

+110
-13
lines changed

4 files changed

+110
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ you're dealing with.
2323
* `arrayIndex` - enables index printing for arrays. default: `true`
2424
* `indent` - defines the number of spaces to indent default: `4`
2525
* `align` - determines how to align object keys. default: `left`
26+
* `depth` - tells purdy how many times to recurse while formatting the object. This is useful for viewing complicated objects. default: `2`. Set to `null` to recurse indefinitely
2627

2728

2829
### `Purdy.stringify(object, [options])`

lib/index.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ var internals = {
1313
BoolTrue: 'green.bold',
1414
Circular: 'grey.bold',
1515
Date: 'green',
16-
error: 'red',
1716
Function: 'cyan',
1817
Key: 'white.bold',
1918
Null: 'red.bold',
@@ -22,9 +21,12 @@ var internals = {
2221
String: 'yellow',
2322
Symbol: 'magenta.bold',
2423
Undefined: 'red.inverse',
24+
depth: 'grey',
25+
error: 'red',
2526
path: 'blue'
2627
},
2728
defaults: {
29+
depth: 2,
2830
plain: false,
2931
path: false,
3032
indent: 4,
@@ -45,8 +47,8 @@ internals.stringify = function (object, options) {
4547

4648
internals.seen = [];
4749
internals.path = [];
48-
internals.settings = Hoek.applyToDefaults(internals.defaults, options || {});
49-
return internals.travel(object, '');
50+
internals.settings = Hoek.applyToDefaults(internals.defaults, options || {}, true);
51+
return internals.travel(object, '', 0);
5052
};
5153

5254

@@ -56,13 +58,13 @@ internals.purdy.stringify = internals.stringify;
5658
module.exports = internals.purdy;
5759

5860

59-
internals.travel = function (object, path) {
61+
internals.travel = function (object, path, depth) {
6062

6163
var type = toString.call(object).split(' ')[1].slice(0, -1);
6264
var format = '';
6365

6466
if (internals[type]) {
65-
format = internals[type](object, path);
67+
format = internals[type](object, path, depth);
6668
}
6769
else {
6870
format = String(object);
@@ -105,7 +107,7 @@ internals.tidyPath = function (path) {
105107
return internals.colorize(path.slice(1, path.size), 'path');
106108
};
107109

108-
internals.Object = internals.process = internals.global = function (object, path) {
110+
internals.Object = internals.process = internals.global = function (object, path, depth) {
109111

110112
var keys = Object.keys(object);
111113

@@ -117,6 +119,7 @@ internals.Object = internals.process = internals.global = function (object, path
117119
return '{}';
118120
}
119121

122+
++depth;
120123
var index = internals.seen.indexOf(object);
121124
if (index !== -1) {
122125
return internals.showCircular(index);
@@ -139,7 +142,14 @@ internals.Object = internals.process = internals.global = function (object, path
139142
}
140143
var longest = keyLengths.sort(internals.lengthCompare)[keyLengths.length - 1];
141144
var keyStr = key.toString();
142-
out = out + internals.indent() + '' + (internals.printMember(keyStr, longest) + ':') + ' ' + internals.travel(item, path + '.' + keyStr);
145+
146+
if (internals.settings.depth === null || internals.settings.depth + 1 >= depth) {
147+
out = out + internals.indent() + '' + (internals.printMember(keyStr, longest) + ':') + ' ' + internals.travel(item, path + '.' + keyStr, depth);
148+
}
149+
else {
150+
internals.indentLevel = internals.indentLevel - 1;
151+
return internals.colorize('[Object]', 'depth');
152+
}
143153
if (i !== il - 1) {
144154
out = out + ',';
145155
}
@@ -159,12 +169,13 @@ internals.showCircular = function (index) {
159169
};
160170

161171

162-
internals.Array = function (array, path) {
172+
internals.Array = function (array, path, depth) {
163173

164174
if (array.length === 0) {
165175
return '[]';
166176
}
167177

178+
++depth;
168179
var index = internals.seen.indexOf(array);
169180
if (index !== -1) {
170181
return internals.showCircular(index);
@@ -183,7 +194,13 @@ internals.Array = function (array, path) {
183194
internals.tidyPath(path + '.' + i) + '\n';
184195
}
185196
var indexStr = internals.settings.arrayIndex ? '[' + internals.printMember(i, il) + '] ' : '';
186-
out = out + internals.indent() + '' + indexStr + internals.travel(item, path + '.' + i);
197+
if (internals.settings.depth === null || internals.settings.depth + 1 >= depth) {
198+
out = out + internals.indent() + '' + indexStr + internals.travel(item, path + '.' + i, depth);
199+
}
200+
else {
201+
internals.indentLevel = internals.indentLevel - 1;
202+
return internals.colorize('[Object]', 'depth');
203+
}
187204
if (i !== il - 1) {
188205
out = out + ',';
189206
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "pretty print objects in real purdy colors. Allows clearer visualization of objects than you get from most pretty printers due to colors. It will also print out the complete path to an object, something that's extremly useful for debugging. Purdy will also print out the path to access a variable using Hoek format making it useful on accessing values.",
55
"main": "lib/index.js",
66
"scripts": {
7-
"test": "lab -a code -t 98 -r html -o coverage.html -r console -o stdout -v"
7+
"test": "lab -a code -t 99 -r html -o coverage.html -r console -o stdout -v"
88
},
99
"homepage": "https://github.com/danielb2/purdy.js",
1010
"bugs": "https://github.com/danielb2/purdy.js/issues",

test/purdy.js

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('Purdy', function () {
3030
var obj = {
3131
theError: error
3232
};
33-
var out = Purdy.stringify(obj);
33+
var out = Purdy.stringify(obj, { depth: null });
3434
expect(out).to.equal('{\n \u001b[1m\u001b[37mtheError\u001b[39m\u001b[22m: { \u001b[31m[Error: some bad, bad error]\u001b[39m\n \u001b[1m\u001b[37mwithKey\u001b[39m\u001b[22m: \u001b[33m\'key\'\u001b[39m\n }\n}');
3535
done();
3636
});
@@ -60,7 +60,7 @@ describe('Purdy', function () {
6060

6161
var afunc = function () {
6262

63-
var out = Purdy.stringify(arguments, { plain: true, arrayIndex: false, indent: 2 });
63+
var out = Purdy.stringify(arguments, { depth: null, plain: true, arrayIndex: false, indent: 2 });
6464
expect(out).to.equal('[\n \'hello\',\n \'purdy\'\n]');
6565
done();
6666
};
@@ -227,7 +227,7 @@ describe('Purdy', function () {
227227
};
228228
var orig = Hoek.clone(obj);
229229

230-
var out = Purdy.stringify(obj, { plain: false, path: true, align: 'right' });
230+
var out = Purdy.stringify(obj, { plain: false, path: true, align: 'right', depth: null });
231231
expect(out).to.equal('{\n \u001b[1m\u001b[37mtravel\u001b[39m\u001b[22m: {\n \u001b[34m// \u001b[39m\u001b[34mtravel.down\u001b[39m\n \u001b[1m\u001b[37mdown\u001b[39m\u001b[22m: {\n \u001b[34m// \u001b[39m\u001b[34mtravel.down.a\u001b[39m\n \u001b[1m\u001b[37m a\u001b[39m\u001b[22m: [\n \u001b[34m// \u001b[39m\u001b[34mtravel.down.a.0\u001b[39m\n [\u001b[1m\u001b[37m0\u001b[39m\u001b[22m] {\n \u001b[34m// \u001b[39m\u001b[34mtravel.down.a.0.path\u001b[39m\n \u001b[1m\u001b[37mpath\u001b[39m\u001b[22m: \u001b[33m\'to get here\'\u001b[39m\n }\n ]\n }\n }\n}');
232232
expect(obj).to.deep.equal(orig);
233233
done();
@@ -301,5 +301,84 @@ describe('Purdy', function () {
301301
expect(out).to.equal('{\n Symbol(blah): \'symbol\'\n}');
302302
done();
303303
});
304+
305+
it('obeys depth printing', function (done) {
306+
307+
var obj = {
308+
count: 1,
309+
a: {
310+
count: 2,
311+
b: {
312+
count: 3,
313+
c: {
314+
count: 4
315+
}
316+
}
317+
}
318+
};
319+
320+
var out = Purdy.stringify(obj, { depth: 1, arrayIndex: false, plain: true });
321+
expect(out).to.equal('{\n count: 1,\n a: {\n count: 2,\n b: [Object]\n }\n}');
322+
done();
323+
});
324+
325+
it('should handle depth printing for array', function (done) {
326+
327+
var obj = [[[[[[[[[[]]]]]]]]]];
328+
329+
var out = Purdy.stringify(obj, { depth: 1, arrayIndex: false, plain: true });
330+
expect(out).to.equal('[\n [\n [Object]\n ]\n]');
331+
done();
332+
});
333+
334+
it('should print using zero depth', function (done) {
335+
336+
var obj = [[[[[[[[[[]]]]]]]]]];
337+
338+
var out = Purdy.stringify(obj, { depth: 0, indent: 1, arrayIndex: false, plain: true });
339+
expect(out).to.equal('[\n [Object]\n]');
340+
done();
341+
});
342+
343+
it('should handle depth printing for mixed', function (done) {
344+
345+
var obj = {
346+
count: 1,
347+
a: {
348+
foo: [[[[[[[[[[]]]]]]]]]],
349+
count: 2,
350+
b: {
351+
count: 3,
352+
c: {
353+
count: 4
354+
}
355+
}
356+
}
357+
};
358+
359+
var out = Purdy.stringify(obj, { depth: 1, arrayIndex: false, plain: true });
360+
expect(out).to.equal('{\n count: 1,\n a: {\n foo: [Object],\n count: 2,\n b: [Object]\n }\n}');
361+
done();
362+
});
363+
364+
it('should handle depth printing using null depth', function (done) {
365+
366+
var obj = {
367+
count: 1,
368+
a: {
369+
count: 2,
370+
b: {
371+
count: 3,
372+
c: {
373+
count: 4
374+
}
375+
}
376+
}
377+
};
378+
379+
var out = Purdy.stringify(obj, { depth: null, arrayIndex: false, plain: true });
380+
expect(out).to.equal('{\n count: 1,\n a: {\n count: 2,\n b: {\n count: 3,\n c: {\n count: 4\n }\n }\n }\n}');
381+
done();
382+
});
304383
});
305384

0 commit comments

Comments
 (0)