Comparing version 2.0.0 to 2.1.0
@@ -24,6 +24,6 @@ #!/usr/bin/env node | ||
internals.parse = function (str) { | ||
internals.parse = function (str, depth) { | ||
try { | ||
Purdy(JSON.parse(str)); | ||
Purdy(JSON.parse(str), { depth: depth }); | ||
} | ||
@@ -42,2 +42,15 @@ catch (e) { | ||
try { | ||
var depthIdx = process.argv.indexOf('--depth'); | ||
var depth = 2; | ||
if (depthIdx !== -1) { | ||
var pair = process.argv.splice(depthIdx, 2); | ||
depth = parseFloat(pair[1]); | ||
if (String(depth) === 'NaN') { | ||
var e = new Error('Depth requires a numerical value'); | ||
e.depth = pair[1]; | ||
Purdy(e); | ||
process.exit(1); | ||
} | ||
} | ||
if (process.argv[2] === '-') { | ||
@@ -52,3 +65,3 @@ stream = process.stdin; | ||
internals.parse(str); | ||
internals.parse(str, depth); | ||
}); | ||
@@ -55,0 +68,0 @@ } |
169
lib/index.js
@@ -5,2 +5,3 @@ // Load modules | ||
var Hoek = require('hoek'); | ||
var Joi = require('joi'); | ||
@@ -11,3 +12,2 @@ | ||
var internals = { | ||
indentLevel: 0, | ||
colors: { | ||
@@ -31,41 +31,50 @@ BoolFalse: 'red.bold', | ||
defaults: { | ||
depth: 2, | ||
plain: false, | ||
path: false, | ||
indent: 4, | ||
align: 'left', | ||
arrayIndex: true, | ||
pathPrefix: '// ' | ||
depth: Joi.number().min(0).allow(null).default(2), | ||
plain: Joi.boolean().default(false), | ||
path: Joi.boolean().default(false), | ||
indent: Joi.number().default(4), | ||
align: Joi.string().valid(['left', 'right']).default('left'), | ||
arrayIndex: Joi.boolean().default(true), | ||
pathPrefix: Joi.string().default('// ') | ||
} | ||
}; | ||
internals.purdy = function (object, options) { | ||
return console.log(internals.stringify(object, options)); | ||
this.indentLevel = 0; | ||
this.seen = []; | ||
this.path = []; | ||
this.object = object; | ||
this.settings = Joi.attempt(options || {}, internals.defaults); | ||
}; | ||
internals.stringify = function (object, options) { | ||
internals.seen = []; | ||
internals.path = []; | ||
internals.settings = Hoek.applyToDefaults(internals.defaults, options || {}, true); | ||
return internals.travel(object, '', 0); | ||
module.exports = function (object, options) { | ||
var Purdy = new internals.purdy(object, options); | ||
return console.log(Purdy.stringify()); | ||
}; | ||
module.exports.stringify = function (object, options) { | ||
internals.purdy.stringify = internals.stringify; | ||
var Purdy = new internals.purdy(object, options); | ||
return Purdy.stringify(); | ||
} | ||
module.exports = internals.purdy; | ||
internals.purdy.prototype.stringify = function (object, options) { | ||
return this.travel(this.object, '', 0); | ||
}; | ||
internals.travel = function (object, path, depth) { | ||
internals.purdy.prototype.travel = function (object, path, depth) { | ||
var type = toString.call(object).split(' ')[1].slice(0, -1); | ||
var format = ''; | ||
if (internals[type]) { | ||
format = internals[type](object, path, depth); | ||
if (this[type]) { | ||
format = this[type](object, path, depth); | ||
} | ||
@@ -76,9 +85,9 @@ else { | ||
return internals.colorize(format, type); | ||
return this.colorize(format, type); | ||
}; | ||
internals.colorize = function (string, type) { | ||
internals.purdy.prototype.colorize = function (string, type) { | ||
if (internals.settings.plain) { | ||
if (this.settings.plain) { | ||
return string; | ||
@@ -109,8 +118,8 @@ } | ||
internals.tidyPath = function (path) { | ||
internals.purdy.prototype.tidyPath = function (path) { | ||
return internals.colorize(path.slice(1, path.size), 'path'); | ||
return this.colorize(path.slice(1, path.size), 'path'); | ||
}; | ||
internals.Object = internals.process = internals.global = function (object, path, depth) { | ||
internals.purdy.prototype.Object = internals.purdy.prototype.process = internals.purdy.prototype.global = function (object, path, depth) { | ||
@@ -128,11 +137,11 @@ var keys = Object.keys(object); | ||
++depth; | ||
var index = internals.seen.indexOf(object); | ||
var index = this.seen.indexOf(object); | ||
if (index !== -1) { | ||
return internals.showCircular(index); | ||
return this.showCircular(index); | ||
} | ||
internals.seen.push(object); | ||
internals.path.push(path); | ||
this.seen.push(object); | ||
this.path.push(path); | ||
var keyLengths = Hoek.clone(keys); | ||
internals.indentLevel = internals.indentLevel + 1; | ||
this.indentLevel = this.indentLevel + 1; | ||
var out = '{\n'; | ||
@@ -143,7 +152,7 @@ | ||
var item = object[key]; | ||
if (internals.settings.path && path.length > 0) { | ||
keyLengths.push(internals.settings.pathPrefix); | ||
out = out + internals.indent() + | ||
internals.colorize(internals.settings.pathPrefix, 'path') + | ||
internals.tidyPath(path + '.' + key) + '\n'; | ||
if (this.settings.path && path.length > 0) { | ||
keyLengths.push(this.settings.pathPrefix); | ||
out = out + this.indent() + | ||
this.colorize(this.settings.pathPrefix, 'path') + | ||
this.tidyPath(path + '.' + key) + '\n'; | ||
} | ||
@@ -153,8 +162,8 @@ var longest = keyLengths.sort(internals.lengthCompare)[keyLengths.length - 1]; | ||
if (internals.settings.depth === null || internals.settings.depth + 1 >= depth) { | ||
out = out + internals.indent() + '' + (internals.printMember(keyStr, longest) + ':') + ' ' + internals.travel(item, path + '.' + keyStr, depth); | ||
if (this.settings.depth === null || this.settings.depth + 1 >= depth) { | ||
out = out + this.indent() + '' + (this.printMember(keyStr, longest) + ':') + ' ' + this.travel(item, path + '.' + keyStr, depth); | ||
} | ||
else { | ||
internals.indentLevel = internals.indentLevel - 1; | ||
return internals.colorize('[Object]', 'depth'); | ||
this.indentLevel = this.indentLevel - 1; | ||
return this.colorize('[Object]', 'depth'); | ||
} | ||
@@ -166,4 +175,4 @@ if (i !== il - 1) { | ||
} | ||
internals.indentLevel = internals.indentLevel - 1; | ||
out = out + internals.indent() + '}'; | ||
this.indentLevel = this.indentLevel - 1; | ||
out = out + this.indent() + '}'; | ||
return out; | ||
@@ -173,11 +182,11 @@ }; | ||
internals.showCircular = function (index) { | ||
internals.purdy.prototype.showCircular = function (index) { | ||
var showPath = internals.path[index]; | ||
var showPath = this.path[index]; | ||
showPath = showPath === '' ? '' : ' ' + showPath.slice(1, showPath.length); | ||
return internals.colorize('[Circular~' + showPath + ']', 'Circular'); | ||
return this.colorize('[Circular~' + showPath + ']', 'Circular'); | ||
}; | ||
internals.Array = function (array, path, depth) { | ||
internals.purdy.prototype.Array = function (array, path, depth) { | ||
@@ -189,26 +198,26 @@ if (array.length === 0) { | ||
++depth; | ||
var index = internals.seen.indexOf(array); | ||
var index = this.seen.indexOf(array); | ||
if (index !== -1) { | ||
return internals.showCircular(index); | ||
return this.showCircular(index); | ||
} | ||
internals.seen.push(array); | ||
internals.path.push(path); | ||
this.seen.push(array); | ||
this.path.push(path); | ||
var out = '[\n'; | ||
internals.indentLevel = internals.indentLevel + 1; | ||
this.indentLevel = this.indentLevel + 1; | ||
for (var i = 0, il = array.length; i < il; ++i) { | ||
var item = array[i]; | ||
if (internals.settings.path && path.length > 0) { | ||
out = out + internals.indent() + | ||
internals.colorize(internals.settings.pathPrefix, 'path') + | ||
internals.tidyPath(path + '.' + i) + '\n'; | ||
if (this.settings.path && path.length > 0) { | ||
out = out + this.indent() + | ||
this.colorize(this.settings.pathPrefix, 'path') + | ||
this.tidyPath(path + '.' + i) + '\n'; | ||
} | ||
var indexStr = internals.settings.arrayIndex ? '[' + internals.printMember(i, il) + '] ' : ''; | ||
if (internals.settings.depth === null || internals.settings.depth + 1 >= depth) { | ||
out = out + internals.indent() + '' + indexStr + internals.travel(item, path + '.' + i, depth); | ||
var indexStr = this.settings.arrayIndex ? '[' + this.printMember(i, il) + '] ' : ''; | ||
if (this.settings.depth === null || this.settings.depth + 1 >= depth) { | ||
out = out + this.indent() + '' + indexStr + this.travel(item, path + '.' + i, depth); | ||
} | ||
else { | ||
internals.indentLevel = internals.indentLevel - 1; | ||
return internals.colorize('[Object]', 'depth'); | ||
this.indentLevel = this.indentLevel - 1; | ||
return this.colorize('[Object]', 'depth'); | ||
} | ||
@@ -220,4 +229,4 @@ if (i !== il - 1) { | ||
} | ||
internals.indentLevel = internals.indentLevel - 1; | ||
out = out + internals.indent() + ']'; | ||
this.indentLevel = this.indentLevel - 1; | ||
out = out + this.indent() + ']'; | ||
return out; | ||
@@ -227,13 +236,13 @@ }; | ||
internals.Error = function (err) { | ||
internals.purdy.prototype.Error = function (err) { | ||
if (Object.keys(err).length === 0) { | ||
return internals.colorize('[' + err + ']', 'error'); | ||
return this.colorize('[' + err + ']', 'error'); | ||
} | ||
var obj = internals.Object(err); | ||
return obj.replace(/^{/, '{ ' + internals.colorize('[Error: ' + err.message + ']', 'error') ); | ||
var obj = this.Object(err, '', null); | ||
return obj.replace(/^{/, '{ ' + this.colorize('[Error: ' + err.message + ']', 'error') ); | ||
}; | ||
internals.String = function (str) { | ||
internals.purdy.prototype.String = function (str) { | ||
@@ -243,30 +252,30 @@ return '\'' + str + '\''; | ||
internals.Arguments = function (obj) { | ||
internals.purdy.prototype.Arguments = function (obj) { | ||
var arr = Array.prototype.slice.call(obj); | ||
return internals.Array(arr); | ||
return this.Array(arr); | ||
}; | ||
internals.Boolean = function (bool) { | ||
internals.purdy.prototype.Boolean = function (bool) { | ||
if (bool === true) { | ||
return internals.colorize(bool + '', 'BoolTrue'); | ||
return this.colorize(bool + '', 'BoolTrue'); | ||
} | ||
return internals.colorize(bool + '', 'BoolFalse'); | ||
return this.colorize(bool + '', 'BoolFalse'); | ||
}; | ||
internals.Function = function (func) { | ||
internals.purdy.prototype.Function = function (func) { | ||
if (func.name) { | ||
return internals.colorize('[Function: ' + func.name + ']', 'Function'); | ||
return this.colorize('[Function: ' + func.name + ']', 'Function'); | ||
} | ||
return internals.colorize('[Function: ?]', 'Function'); | ||
return this.colorize('[Function: ?]', 'Function'); | ||
}; | ||
internals.indent = function () { | ||
internals.purdy.prototype.indent = function () { | ||
return internals.spaces(internals.indentLevel * internals.settings.indent); | ||
return internals.spaces(this.indentLevel * this.settings.indent); | ||
}; | ||
@@ -286,5 +295,5 @@ | ||
internals.printMember = function (member, max) { | ||
internals.purdy.prototype.printMember = function (member, max) { | ||
if (internals.settings.align === 'left') { | ||
if (this.settings.align === 'left') { | ||
max = 0; | ||
@@ -295,3 +304,3 @@ } | ||
var toShift = maxLength - memberLength; | ||
return internals.colorize(internals.spaces(toShift) + member, 'Key'); | ||
return this.colorize(internals.spaces(toShift) + member, 'Key'); | ||
}; |
{ | ||
"name": "purdy", | ||
"version": "2.0.0", | ||
"version": "2.1.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.", | ||
@@ -33,5 +33,6 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"chalk": "0.4.x", | ||
"hoek": "2.x.x", | ||
"chalk": "0.4.x" | ||
"joi": "^6.10.1" | ||
} | ||
} |
@@ -26,2 +26,11 @@ var Code = require('code'); | ||
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) { | ||
@@ -28,0 +37,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
151149
573
3
+ Addedjoi@^6.10.1
+ Addedisemail@1.2.0(transitive)
+ Addedjoi@6.10.1(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedtopo@1.1.0(transitive)