Comparing version
@@ -16,3 +16,2 @@ // Load modules | ||
Date: 'green', | ||
Function: 'cyan', | ||
Key: 'white.bold', | ||
@@ -27,2 +26,4 @@ Null: 'red.bold', | ||
error: 'red', | ||
function: 'cyan', | ||
prefix: 'green', | ||
path: 'blue' | ||
@@ -129,4 +130,6 @@ }, | ||
var prefix = ['Object', 'Function', 'Error', ''].indexOf(object.constructor.name) !== -1 ? '' : this.colorize(object.constructor.name, 'prefix') + ' '; | ||
if (keys.length === 0) { | ||
return '{}'; | ||
return prefix + '{}'; | ||
} | ||
@@ -144,3 +147,3 @@ | ||
this.indentLevel = this.indentLevel + 1; | ||
var out = '{\n'; | ||
var out = prefix + '{\n'; | ||
@@ -234,3 +237,4 @@ for (var i = 0, il = keys.length; i < il; ++i) { | ||
var obj = this.Object(err, '', null); | ||
return obj.replace(/^{/, '{ ' + this.colorize('[Error: ' + err.message + ']', 'error') ); | ||
var message = err.message ? ': ' + err.message : ''; | ||
return obj.replace(/^{/, '{ ' + this.colorize('[Error'+ message + ']', 'error') ); | ||
}; | ||
@@ -260,8 +264,12 @@ | ||
internals.purdy.prototype.Function = function (func) { | ||
internals.purdy.prototype.Function = function (obj) { | ||
if (func.name) { | ||
return this.colorize('[Function: ' + func.name + ']', 'Function'); | ||
var name = obj.name ? ': ' + obj.name : ''; | ||
if (Object.keys(obj).length === 0) { | ||
return this.colorize('[Function' + name + ']', 'function'); | ||
} | ||
return this.colorize('[Function: ?]', 'Function'); | ||
var props = this.Object(obj, '', null); | ||
return props.replace(/^{/, '{ ' + this.colorize('[Function' + name + ']', 'function') ); | ||
}; | ||
@@ -268,0 +276,0 @@ |
{ | ||
"name": "purdy", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"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.", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "lab -a code -t 99 -r html -o coverage.html -r console -o stdout -v" | ||
"test": "lab -a code -t 100 -r html -o coverage.html -r console -o stdout -v" | ||
}, | ||
@@ -9,0 +9,0 @@ "homepage": "https://github.com/danielb2/purdy.js", |
@@ -1,2 +0,2 @@ | ||
# Purdy  | ||
# Purdy   | ||
@@ -14,2 +14,4 @@ Print things real purdy for nodejs. | ||
 | ||
Prints anything indented, and with arrays with index keys, and different | ||
@@ -42,10 +44,13 @@ types in colors such that it's very easy to get an overview of what object | ||
The following code prints what's in the image above. | ||
``` javascript | ||
var Purdy = require('purdy'); | ||
var mises = function mises () { this.moop = 3 } | ||
var instance = new mises(); | ||
var circularObj = { }; | ||
circularObj.a = circularObj; | ||
Purdy({ | ||
var obj = { | ||
integer: Date.now(), | ||
string: 'foo', | ||
anonymous: Purdy, | ||
anonPurdy: Purdy, | ||
defined: function Yes() {}, | ||
@@ -59,48 +64,14 @@ nested: {hello: 'hapi'}, | ||
trueBool: true, | ||
symbol: Symbol('purdy'), | ||
emptyArr: [], | ||
circular: circularObj, | ||
date: new Date(), | ||
arrayWithVisibleIndex: [ 'one', 'two', 'three' ] | ||
}); | ||
arrayWithVisibleIndex: [ 'one', 'two', 'three' ], | ||
instance: instance, | ||
}; | ||
Purdy(obj); | ||
``` | ||
 | ||
``` javascript | ||
// var obj = { | ||
// travel: { | ||
// down: { | ||
// a: [{ | ||
// path: 'to get here' | ||
// }] | ||
// } | ||
// } | ||
// Purdy(obj, { path: true }); | ||
{ | ||
travel: { | ||
// travel.down | ||
down: { | ||
// travel.down.a | ||
a: [ | ||
// travel.down.a.0 | ||
[0] { | ||
// travel.down.a.0.path | ||
path: 'to get here' | ||
} | ||
] | ||
} | ||
} | ||
} | ||
// var Hoek = require('hoek'); | ||
// Purdy(Hoek.reach(obj, 'travel.down.a.0.path')); | ||
{ | ||
path: 'to get here' | ||
} | ||
``` | ||
### Command-line Interface | ||
@@ -107,0 +78,0 @@ |
@@ -18,29 +18,42 @@ var Code = require('code'); | ||
it('should display an error', function (done) { | ||
describe('errors', function () { | ||
var error = new Error('plain error'); | ||
var out = Purdy.stringify(error); | ||
expect(out).to.equal('\u001b[31m[Error: plain error]\u001b[39m'); | ||
done(); | ||
}); | ||
it('should display an error with properties', function (done) { | ||
it('should display an error', function (done) { | ||
var error = new Error('error with properties'); | ||
error.code = 'BAD'; | ||
var out = Purdy.stringify(error); | ||
expect(out).to.equal('{ \u001b[31m[Error: error with properties]\u001b[39m\n \u001b[1m\u001b[37mcode\u001b[39m\u001b[22m: \u001b[33m\'BAD\'\u001b[39m\n}'); | ||
done(); | ||
}); | ||
var error = new Error('plain error'); | ||
var out = Purdy.stringify(error); | ||
expect(out).to.equal('\u001b[31m[Error: plain error]\u001b[39m'); | ||
done(); | ||
}); | ||
it('should display an error with detail', function (done) { | ||
it('should display an error with no error message and property correctly', function (done) { | ||
var error = new Error('some bad, bad error'); | ||
error.withKey = 'key'; | ||
var obj = { | ||
theError: error | ||
}; | ||
var out = Purdy.stringify(obj, { depth: null }); | ||
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}'); | ||
done(); | ||
var error = new Error(); | ||
error.murray = 'rothbard'; | ||
var out = Purdy.stringify(error); | ||
expect(out).to.equal('{ \u001b[31m[Error]\u001b[39m\n \u001b[1m\u001b[37mmurray\u001b[39m\u001b[22m: \u001b[33m\'rothbard\'\u001b[39m\n}'); | ||
done(); | ||
}); | ||
it('should display an error with properties', function (done) { | ||
var error = new Error('error with properties'); | ||
error.code = 'BAD'; | ||
var out = Purdy.stringify(error); | ||
expect(out).to.equal('{ \u001b[31m[Error: error with properties]\u001b[39m\n \u001b[1m\u001b[37mcode\u001b[39m\u001b[22m: \u001b[33m\'BAD\'\u001b[39m\n}'); | ||
done(); | ||
}); | ||
it('should display an error with detail', function (done) { | ||
var error = new Error('some bad, bad error'); | ||
error.withKey = 'key'; | ||
var obj = { | ||
theError: error | ||
}; | ||
var out = Purdy.stringify(obj, { depth: null }); | ||
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}'); | ||
done(); | ||
}); | ||
}); | ||
@@ -101,15 +114,58 @@ | ||
it('should print a function', function (done) { | ||
describe('functions', function () { | ||
var out = Purdy.stringify(Array.isArray); | ||
expect(out).to.equal('\u001b[36m\u001b[36m[Function: isArray]\u001b[39m\u001b[39m'); | ||
done(); | ||
}); | ||
it('should print constructor name', function (done) { | ||
it('should print an anonymous function', function (done) { | ||
var mises = function mises () { this.moop = 3 } | ||
var obj = { instance: new mises() }; | ||
var out = Purdy.stringify(obj, { indent: 1 }); | ||
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}'); | ||
done(); | ||
}); | ||
var anon = function () {}; | ||
var out = Purdy.stringify(anon); | ||
expect(out).to.equal('\u001b[36m\u001b[36m[Function: ?]\u001b[39m\u001b[39m'); | ||
done(); | ||
it('should print not print common constructor', function (done) { | ||
var mises = function () { this.moop = 3 } | ||
var obj = { instance: new mises() }; | ||
var out = Purdy.stringify(obj, { indent: 1 }); | ||
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}'); | ||
done(); | ||
}); | ||
it('should print a function', function (done) { | ||
var out = Purdy.stringify(Array.isArray); | ||
expect(out).to.equal('\u001b[36m[Function: isArray]\u001b[39m'); | ||
done(); | ||
}); | ||
it('should print an anonymous function', function (done) { | ||
var anon = function () {}; | ||
var out = Purdy.stringify(anon); | ||
expect(out).to.equal('\u001b[36m[Function]\u001b[39m'); | ||
done(); | ||
}); | ||
it('should print properties for functions', function (done) { | ||
var obj = function () {}; | ||
obj.property = 3; | ||
var out = Purdy.stringify(obj, { indent: 1, plain: false }); | ||
expect(out).to.equal('{ \u001b[36m[Function]\u001b[39m\n \u001b[1m\u001b[37mproperty\u001b[39m\u001b[22m: \u001b[1m\u001b[34m3\u001b[39m\u001b[22m\n}'); | ||
done(); | ||
}); | ||
it('should print properties for functions with name', function (done) { | ||
var obj = function Liberty () {}; | ||
obj.property = 3; | ||
var out = Purdy.stringify(obj, { indent: 1, plain: false }); | ||
expect(out).to.equal('{ \u001b[36m[Function: Liberty]\u001b[39m\n \u001b[1m\u001b[37mproperty\u001b[39m\u001b[22m: \u001b[1m\u001b[34m3\u001b[39m\u001b[22m\n}'); | ||
done(); | ||
}); | ||
}); | ||
@@ -288,109 +344,148 @@ | ||
it('prints symbols', { skip: Object.getOwnPropertySymbols === undefined }, function (done) { | ||
describe('symbols', function () { | ||
var blah = Symbol(); | ||
it('prints symbols', { skip: Object.getOwnPropertySymbols === undefined }, function (done) { | ||
var obj = { | ||
a: 2323 | ||
}; | ||
var blah = Symbol(); | ||
obj[blah] = 'symbol'; | ||
var obj = { | ||
a: 2323 | ||
}; | ||
var out = Purdy.stringify(obj, { arrayIndex: false, plain: true }); | ||
expect(out).to.equal('{\n a: 2323,\n Symbol(): \'symbol\'\n}'); | ||
done(); | ||
}); | ||
obj[blah] = 'symbol'; | ||
it('prints only symbols', { skip: Object.getOwnPropertySymbols === undefined }, function (done) { | ||
var out = Purdy.stringify(obj, { arrayIndex: false, plain: true }); | ||
expect(out).to.equal('{\n a: 2323,\n Symbol(): \'symbol\'\n}'); | ||
done(); | ||
}); | ||
var blah = Symbol('blah'); | ||
it('prints only symbols', { skip: Object.getOwnPropertySymbols === undefined }, function (done) { | ||
var obj = {}; | ||
var blah = Symbol('blah'); | ||
obj[blah] = 'symbol'; | ||
var obj = {}; | ||
var out = Purdy.stringify(obj, { arrayIndex: false, plain: true }); | ||
expect(out).to.equal('{\n Symbol(blah): \'symbol\'\n}'); | ||
done(); | ||
obj[blah] = 'symbol'; | ||
var out = Purdy.stringify(obj, { arrayIndex: false, plain: true }); | ||
expect(out).to.equal('{\n Symbol(blah): \'symbol\'\n}'); | ||
done(); | ||
}); | ||
describe('100% coverage', function () { | ||
it('should have coverage for having getOwnPropertySymbols', { skip: !Object.getOwnPropertySymbols }, function (done) { | ||
var getOwnPropertySymbols = Object.getOwnPropertySymbols; | ||
Object.getOwnPropertySymbols = undefined; | ||
var obj = { a: 3 }; | ||
var out = Purdy.stringify(obj, { plain: true }); | ||
expect(out).to.equal('{\n a: 3\n}'); | ||
Object.getOwnPropertySymbols = getOwnPropertySymbols; | ||
done(); | ||
}); | ||
it('should have coverage for not having getOwnPropertySymbols', { skip: !!Object.getOwnPropertySymbols }, function (done) { | ||
Object.getOwnPropertySymbols = function () { | ||
return []; | ||
}; | ||
var obj = { a: 3 }; | ||
var out = Purdy.stringify(obj, { plain: true }); | ||
expect(out).to.equal('{\n a: 3\n}'); | ||
Object.getOwnPropertySymbols = undefined; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('obeys depth printing', function (done) { | ||
describe('depth', function () { | ||
var obj = { | ||
count: 1, | ||
a: { | ||
count: 2, | ||
b: { | ||
count: 3, | ||
c: { | ||
count: 4 | ||
it('should handle depth printing', function (done) { | ||
var obj = { | ||
count: 1, | ||
a: { | ||
count: 2, | ||
b: { | ||
count: 3, | ||
c: { | ||
count: 4 | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
var out = Purdy.stringify(obj, { depth: 1, arrayIndex: false, plain: true }); | ||
expect(out).to.equal('{\n count: 1,\n a: {\n count: 2,\n b: [Object]\n }\n}'); | ||
done(); | ||
}); | ||
var out = Purdy.stringify(obj, { depth: 1, arrayIndex: false, plain: true }); | ||
expect(out).to.equal('{\n count: 1,\n a: {\n count: 2,\n b: [Object]\n }\n}'); | ||
done(); | ||
}); | ||
it('should handle depth printing for array', function (done) { | ||
it('should handle depth printing for array', function (done) { | ||
var obj = [[[[[[[[[[]]]]]]]]]]; | ||
var obj = [[[[[[[[[[]]]]]]]]]]; | ||
var out = Purdy.stringify(obj, { depth: 1, arrayIndex: false, plain: true }); | ||
expect(out).to.equal('[\n [\n [Object]\n ]\n]'); | ||
done(); | ||
}); | ||
var out = Purdy.stringify(obj, { depth: 1, arrayIndex: false, plain: true }); | ||
expect(out).to.equal('[\n [\n [Object]\n ]\n]'); | ||
done(); | ||
}); | ||
it('should print using zero depth', function (done) { | ||
it('should print using zero depth', function (done) { | ||
var obj = [[[[[[[[[[]]]]]]]]]]; | ||
var obj = [[[[[[[[[[]]]]]]]]]]; | ||
var out = Purdy.stringify(obj, { depth: 0, indent: 1, arrayIndex: false, plain: true }); | ||
expect(out).to.equal('[\n [Object]\n]'); | ||
done(); | ||
}); | ||
var out = Purdy.stringify(obj, { depth: 0, indent: 1, arrayIndex: false, plain: true }); | ||
expect(out).to.equal('[\n [Object]\n]'); | ||
done(); | ||
}); | ||
it('should handle depth printing for mixed', function (done) { | ||
it('should handle depth printing for mixed', function (done) { | ||
var obj = { | ||
count: 1, | ||
a: { | ||
foo: [[[[[[[[[[]]]]]]]]]], | ||
count: 2, | ||
b: { | ||
count: 3, | ||
c: { | ||
count: 4 | ||
var obj = { | ||
count: 1, | ||
a: { | ||
foo: [[[[[[[[[[]]]]]]]]]], | ||
count: 2, | ||
b: { | ||
count: 3, | ||
c: { | ||
count: 4 | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
var out = Purdy.stringify(obj, { depth: 1, arrayIndex: false, plain: true }); | ||
expect(out).to.equal('{\n count: 1,\n a: {\n foo: [Object],\n count: 2,\n b: [Object]\n }\n}'); | ||
done(); | ||
}); | ||
var out = Purdy.stringify(obj, { depth: 1, arrayIndex: false, plain: true }); | ||
expect(out).to.equal('{\n count: 1,\n a: {\n foo: [Object],\n count: 2,\n b: [Object]\n }\n}'); | ||
done(); | ||
}); | ||
it('should handle depth printing using null depth', function (done) { | ||
it('should handle depth printing using null depth', function (done) { | ||
var obj = { | ||
count: 1, | ||
a: { | ||
count: 2, | ||
b: { | ||
count: 3, | ||
c: { | ||
count: 4 | ||
var obj = { | ||
count: 1, | ||
a: { | ||
count: 2, | ||
b: { | ||
count: 3, | ||
c: { | ||
count: 4 | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
var out = Purdy.stringify(obj, { depth: null, arrayIndex: false, plain: true }); | ||
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}'); | ||
done(); | ||
var out = Purdy.stringify(obj, { depth: null, arrayIndex: false, plain: true }); | ||
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}'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
Sorry, the diff of this file is not supported yet
340839
125.5%10
11.11%642
12.04%89
-24.58%