Comparing version 3.4.1 to 3.5.0
@@ -29,3 +29,4 @@ 'use strict'; | ||
prefix: 'green', | ||
path: 'blue' | ||
path: 'blue', | ||
json: 'green' | ||
}, | ||
@@ -35,2 +36,3 @@ defaults: { | ||
plain: Joi.boolean().default(false), | ||
json: Joi.boolean().default(false), | ||
path: Joi.boolean().default(false), | ||
@@ -45,3 +47,3 @@ proto: Joi.boolean().default(false).description('include the prototype chain and print all inherited properties'), | ||
internals.purdy = function (object, options) { | ||
internals.purdy = function (options) { | ||
@@ -51,3 +53,2 @@ this.indentLevel = 0; | ||
this.path = []; | ||
this.object = object; | ||
@@ -61,20 +62,34 @@ this.settings = Joi.attempt(options || {}, internals.defaults); | ||
const Purdy = new internals.purdy(object, options); | ||
return console.log(Purdy.stringify()); | ||
const purdy = new internals.purdy(options); | ||
return purdy.print(object); | ||
}; | ||
module.exports.purdy = function (options) { | ||
return new internals.purdy(options); | ||
}; | ||
module.exports.stringify = function (object, options) { | ||
const Purdy = new internals.purdy(object, options); | ||
return Purdy.stringify(); | ||
const purdy = new internals.purdy(options); | ||
return purdy.stringify(object); | ||
}; | ||
internals.purdy.prototype.print = function (...args) { | ||
internals.purdy.prototype.stringify = function () { | ||
console.log(this.stringify(...args)); | ||
}; | ||
if (this.settings.proto) { | ||
this.object = internals.mergeInherited(this.object); | ||
internals.purdy.prototype.stringify = function (...args) { | ||
const res = []; | ||
for (let object of args) { | ||
if (this.settings.proto) { | ||
object = internals.mergeInherited(object); | ||
} | ||
res.push(this.travel(object, '', 0)); | ||
} | ||
return this.travel(this.object, '', 0); | ||
return res.join(' '); | ||
}; | ||
@@ -92,2 +107,3 @@ | ||
} | ||
return result; | ||
@@ -143,3 +159,4 @@ }; | ||
return '<Buffer ' + buffer.slice(0, 25).toString('utf8') + ' ... >'; | ||
const endStr = buffer.length < 26 ? '>' : ' ... >'; | ||
return '<Buffer ' + buffer.slice(0, 25).toString('utf8') + endStr; | ||
}; | ||
@@ -175,2 +192,3 @@ | ||
} | ||
this.seen.push(object); | ||
@@ -192,2 +210,3 @@ this.path.push(path); | ||
} | ||
const longest = keyLengths.sort(internals.lengthCompare)[keyLengths.length - 1]; | ||
@@ -203,7 +222,10 @@ const keyStr = key.toString(); | ||
} | ||
if (i !== keys.length - 1) { | ||
out = out + ','; | ||
} | ||
out = out + '\n'; | ||
} | ||
this.indentLevel = this.indentLevel - 1; | ||
@@ -234,2 +256,3 @@ out = out + this.indent() + '}'; | ||
} | ||
this.seen.push(array); | ||
@@ -248,2 +271,3 @@ this.path.push(path); | ||
} | ||
const indexStr = this.settings.arrayIndex ? '[' + this.printMember(i, array.length) + '] ' : ''; | ||
@@ -257,7 +281,10 @@ if (this.settings.depth === null || this.settings.depth + 1 >= depth) { | ||
} | ||
if (i !== array.length - 1) { | ||
out = out + ','; | ||
} | ||
out = out + '\n'; | ||
} | ||
this.indentLevel = this.indentLevel - 1; | ||
@@ -275,2 +302,3 @@ out = out + this.indent() + ']'; | ||
} | ||
const plain = this.settings.plain; | ||
@@ -299,4 +327,24 @@ this.settings.plain = true; | ||
internals.Json = function Json(str) { | ||
const parsed = JSON.parse(str); | ||
for (const key in parsed) { | ||
this[key] = parsed[key]; | ||
} | ||
}; | ||
internals.purdy.prototype.String = function (str) { | ||
if (this.settings.json) { | ||
try { | ||
if (str.match(/^\s*{/)) { | ||
const thing = new internals.Json(str); | ||
return this.colorize(this.travel(thing, '', 0), 'json'); | ||
} | ||
} | ||
catch (e) {} | ||
} | ||
return '\'' + str + '\''; | ||
@@ -317,2 +365,3 @@ }; | ||
} | ||
return this.colorize(bool + '', 'BoolFalse'); | ||
@@ -357,2 +406,7 @@ }; | ||
} | ||
if (member.indexOf && member.indexOf(' ') > 0) { | ||
member = `'${member}'`; | ||
} | ||
const memberLength = member.length; | ||
@@ -359,0 +413,0 @@ const maxLength = max.length; |
{ | ||
"name": "purdy", | ||
"version": "3.4.1", | ||
"version": "3.5.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 extremely 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 100 -v -I Reflect -L" | ||
"test": "lab -a code -t 100 -v -I core,__core-js_shared__,BigUint64Array,BigInt64Array,BigInt,URL,URLSearchParams,TextEncoder,TextDecoder,queueMicrotask -L", | ||
"lint": "eslint ." | ||
}, | ||
@@ -9,0 +10,0 @@ "homepage": "https://github.com/danielb2/purdy.js", |
@@ -30,2 +30,3 @@ # Purdy ![build](https://travis-ci.org/danielb2/purdy.js.svg) ![coverage](https://img.shields.io/badge/coverage-100%25-green.svg) | ||
* `proto` - include the prototype chain and print all inherited properties as well. Default: `false` | ||
* `json` - attempt to parse strings beginning with { as JSON. Default: `false` | ||
@@ -43,2 +44,12 @@ | ||
### `Purdy.purdy([options]);` | ||
Useful if you want to print multiple objects like console.log | ||
``` javascript | ||
const purdy = Purdy.purdy({plain: true}); | ||
purdy.print('one', 'two'); | ||
purdy.stringify('one', 'two'); | ||
``` | ||
### Examples | ||
@@ -87,5 +98,5 @@ | ||
A new option `-l` has been added to parse log files that has one JSON string per line in the file. | ||
A new option `-l` has been added to parse log files for newline delimited JSON (NDJSON). | ||
`purdy -l logfile.log` will print that. | ||
`purdy -l logfile.log` will print that or `cat app.log | purdy -l -s` | ||
@@ -92,0 +103,0 @@ ## Contributing |
@@ -21,2 +21,49 @@ 'use strict'; | ||
it('should print keys with spaces using quotes', (done) => { | ||
const out = Purdy.stringify({ 'arya killed the night king': 'woot', normal: 'OK' }, { plain: true }); | ||
expect(out).to.equal('{\n \'arya killed the night king\': \'woot\',\n normal: \'OK\'\n}'); | ||
done(); | ||
}); | ||
it('should print multiple things', (done) => { | ||
const purdy = Purdy.purdy({ plain: true }); | ||
const out = purdy.stringify('one', 'two', { three: 3 }); | ||
expect(out).to.equal('\'one\' \'two\' {\n three: 3\n}'); | ||
done(); | ||
}); | ||
describe('json', () => { | ||
it('should handle printing strings with json', (done) => { | ||
const out = Purdy.stringify([1, 2, 1, 1, '{"foo": "bar", "int": 3 }'], { plain: true, json: true }); | ||
expect(out).to.equal('[\n [0] 1,\n [1] 2,\n [2] 1,\n [3] 1,\n [4] Json {\n foo: \'bar\',\n int: 3\n }\n]'); | ||
done(); | ||
}); | ||
it('should handle printing strings with json - color check', (done) => { | ||
const out = Purdy.stringify('{"a":3}', { plain: false, json: true }); | ||
expect(out).to.equal('\u001b[33m\u001b[32m\u001b[32mJson\u001b[32m {\u001b[33m\u001b[39m\n\u001b[33m\u001b[32m \u001b[1m\u001b[37ma\u001b[32m\u001b[22m: \u001b[1m\u001b[34m3\u001b[32m\u001b[22m\u001b[33m\u001b[39m\n\u001b[33m\u001b[32m}\u001b[33m\u001b[39m'); | ||
done(); | ||
}); | ||
it('should only print strings as json wrapped in {} to avoid mistaking for string', (done) => { | ||
const out = Purdy.stringify('3', { plain: true, json: true }); | ||
expect(out).to.equal('\'3\''); | ||
done(); | ||
}); | ||
it('shouldnt print json if json is false', (done) => { | ||
const out = Purdy.stringify('{"a":3}', { plain: true, json: false }); | ||
expect(out).to.equal('\'{"a":3}\''); | ||
done(); | ||
}); | ||
}); | ||
describe('prototype print', () => { | ||
@@ -26,3 +73,3 @@ | ||
const person = { firstName: 'billy', lastName: 'bob' }; | ||
const person = { firstName: 'billy', lastName: 'Bob' }; | ||
const withAge = Object.create(person); | ||
@@ -33,3 +80,2 @@ withAge.age = 24; | ||
withYear.firstName = 'Billy'; | ||
withYear.lastName = 'Bob'; | ||
const out = Purdy.stringify(withYear, { plain: true, proto: true }); | ||
@@ -94,5 +140,14 @@ expect(out).to.equal('{\n firstName: \'Billy\',\n lastName: \'Bob\',\n age: 24,\n year: 1999\n}'); | ||
it('should not print ... if string is short', (done) => { | ||
const buffer = new Buffer.from('shorter'); | ||
const out = Purdy.stringify({ buffer }); | ||
expect(out).to.equal('{\n \u001b[1m\u001b[37mbuffer\u001b[39m\u001b[22m: <Buffer shorter>\n}'); | ||
done(); | ||
}); | ||
it('should print binary', (done) => { | ||
const fs = require('fs'); | ||
fs.readFile('/bin/sh', (err, data) => { | ||
@@ -140,2 +195,3 @@ | ||
// eslint-disable-next-line prefer-rest-params | ||
const out = Purdy.stringify(arguments, { depth: null, plain: true, arrayIndex: false, indent: 2 }); | ||
@@ -177,2 +233,3 @@ expect(out).to.equal('[\n \'hello\',\n \'purdy\'\n]'); | ||
}; | ||
const obj = { instance: new mises() }; | ||
@@ -190,2 +247,3 @@ const out = Purdy.stringify(obj, { indent: 1 }); | ||
}; | ||
const obj = { instance: new mises() }; | ||
@@ -404,2 +462,3 @@ const out = Purdy.stringify(obj, { indent: 1 }); | ||
}; | ||
Purdy('hello', { plain: true }); | ||
@@ -406,0 +465,0 @@ process.stdout.write = stdout; |
Sorry, the diff of this file is not supported yet
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
865
108
39992
8