json-colorz
Advanced tools
Comparing version 0.1.3 to 0.2.0
215
index.js
@@ -6,183 +6,70 @@ /*! | ||
* Licensed under the ISC license. | ||
* | ||
* Original Code: | ||
* jsome <https://github.com/Javascipt/Jsome> | ||
* Copyright (c) 2015 Khalid REHIOUI <Array.prototype@gmail.com> (http://github.com/javascipt) | ||
* Licensed under he MIT License (MIT) | ||
*/ | ||
var clrz = require('colorz') | ||
var data = { | ||
global: { | ||
colored : true, | ||
async : false | ||
}, | ||
colors: { | ||
num : "cyan", | ||
str : "magenta", | ||
bool : "red", | ||
undef : "grey", | ||
null : "grey", | ||
attr : "green", | ||
quot : "magenta", | ||
punc : "yellow", | ||
brack : "yellow" | ||
}, | ||
level: { | ||
show : false, | ||
char : ".", | ||
color : "yellow", | ||
spaces : 4 | ||
} | ||
var colors = { | ||
num : 'cyan', | ||
str : 'magenta', | ||
bool : 'red', | ||
regex : 'blue', | ||
undef : 'grey', | ||
null : 'grey', | ||
attr : 'green', | ||
quot : 'yellow', | ||
punc : 'yellow', | ||
brack : 'yellow' | ||
} | ||
var colors = data.colors | ||
var level = data.level | ||
var global = data.global | ||
function generateColors (value, type) { | ||
if (!global.colored) return value | ||
if (type) return c(value, colors[type]) | ||
if ((''+value) == 'null') return c('null', colors.null) | ||
if ((''+value) == 'undefined') return c('undefined', colors.undef) | ||
switch (kindOf(value)) { | ||
case 'number' : return c(value.toString(), colors.num) | ||
case 'string' : return [c('"', colors.quot), c(value, colors.str), c('"', colors.quot)].join('') | ||
case 'boolean' : return c(value.toString(), colors.bool) | ||
case 'function' : return c('null', colors.null) | ||
case 'array' : | ||
var log = c('[', colors.brack) | ||
var i = -1 | ||
var len = value.length | ||
while (++i < len) { | ||
log += generateColors(value[i]) | ||
if (i < value.length - 1) log += c(', ', colors.punc) | ||
} | ||
log += c(']', colors.brack) | ||
return log | ||
} | ||
var level = { | ||
show : false, | ||
char : '.', | ||
color : 'red', | ||
spaces : 2, | ||
start : 0 | ||
} | ||
function getTabs (lvl) { | ||
var tabs = '' | ||
var spaces = ' ' | ||
var i = -1 | ||
while (spaces.length < level.spaces) { | ||
spaces += ' ' | ||
} | ||
while (++i < lvl) { | ||
tabs += ((level.show) ? c(level.char, level.color) : '') + spaces | ||
} | ||
return tabs | ||
var params = { | ||
colored: true, | ||
async: false | ||
} | ||
function hasChilds (array) { | ||
var i = -1 | ||
var len = array.length | ||
while (++i < len) { | ||
if (kindOf(array[i]) === 'array' || kindOf(array[i]) === 'object') return true | ||
} | ||
return false | ||
var options = { | ||
colors : colors, | ||
level : level, | ||
params : params | ||
} | ||
function clearObject (json) { | ||
var len = 0 | ||
for (var key in json) { | ||
if (json.hasOwnProperty(key) && kindOf(json[key] !== 'function')) len ++ | ||
else delete json[key] | ||
} | ||
return len | ||
} | ||
var engine = require('./lib/engine').setOptions(options) | ||
function clearArray (json) { | ||
for (var key in json) { | ||
if (kindOf(json[key]) === 'function') json[key] = null | ||
function colorize (engine) { | ||
// main function: jclrz | ||
function jclrz (json, cb) { | ||
if (!jclrz.params.async) { | ||
process.stdout.write(engine.gen(json, options.level.start) + '\n') | ||
} else { | ||
process.nextTick(function () { | ||
process.stdout.write(engine.gen(json, options.level.start) + '\n') | ||
cb && cb() | ||
}) | ||
} | ||
return json | ||
} | ||
return json.length | ||
} | ||
function isEmpty (elem) { | ||
for (key in elem) { | ||
return false | ||
// parse | ||
jclrz.parse = function (jsonString, cb) { | ||
return jclrz(JSON.parse(jsonString), cb) | ||
} | ||
return true | ||
} | ||
function kindOf (value) { | ||
return typeof value === 'object' | ||
? Object.prototype.toString.call(value).replace(/^\[object |\]$/g,'').toLowerCase() | ||
: typeof value | ||
} | ||
// options | ||
jclrz.colors = colors | ||
jclrz.level = level | ||
jclrz.params = params | ||
function c (str, prop) { | ||
return clrz[prop](str) | ||
return jclrz | ||
} | ||
function jsonColorize (json, lvl, inObj) { | ||
var level = lvl ? lvl : 0 | ||
var result = '' | ||
if (kindOf(json) === 'object') { | ||
var len = clearObject(json) | ||
if (isEmpty(json)) return getTabs(level) + generateColors('{}', 'brack') | ||
result += getTabs(inObj ? 0 : level) + generateColors('{\n', 'brack') | ||
for (key in json) { | ||
len -- | ||
if (kindOf(json[key]) === 'object' || kindOf(json[key]) === 'array') { | ||
result += getTabs(level +1) | ||
+ generateColors(key, 'attr') | ||
+ generateColors(': ', 'punc') | ||
+ jsonColorize(json[key], level + 1, true) | ||
+ generateColors((len ? ', ' : '') + '\n', 'punc') | ||
} else { | ||
result += getTabs(level + 1) | ||
+ generateColors(key, 'attr') | ||
+ generateColors(': ', 'punc') | ||
+ generateColors(json[key]) | ||
+ generateColors((len ? ', ' : '') + '\n', 'punc') | ||
} | ||
} | ||
result += getTabs(level) + generateColors('}', 'brack') | ||
} else if (kindOf(json) === 'array' && hasChilds(json)) { | ||
if (isEmpty(json)) return getTabs(level) + generateColors('[]', 'brack') | ||
result += getTabs(inObj ? 0 : level) + generateColors('[\n', 'brack') | ||
var len = clearArray(json) | ||
for (key in json) { | ||
len -- | ||
if (kindOf(json[key]) === 'object' || kindOf(json[key]) === 'array') { | ||
result += jsonColorize(json[key], level + 1) + generateColors((len ? ', ' : '') + '\n', 'punc') | ||
} else { | ||
result += getTabs(level + 1) | ||
+ generateColors(json[key]) | ||
+ generateColors((len ? ', ' : '') + '\n', 'punc') | ||
} | ||
} | ||
result += getTabs(level) + generateColors(']', 'brack') | ||
} else { | ||
result += getTabs(inObj ? 0 : level) + generateColors(json) | ||
} | ||
return result | ||
} | ||
function service (json, func) { | ||
// if (global.async) { | ||
// jsonColorize.than(json, function (data) { | ||
// console.log | ||
// }) | ||
// } | ||
console.log(jsonColorize(json)) | ||
} | ||
service.parse = function (json) { | ||
this(JSON.parse(json)) | ||
} | ||
service.colors = colors | ||
service.level = level | ||
service.global = global | ||
module.exports = service | ||
module.exports = colorize(engine) |
{ | ||
"name": "json-colorz", | ||
"version": "0.1.3", | ||
"description": "An awesome module being created", | ||
"version": "0.2.0", | ||
"description": "display a json or javascript object in the console with colorz", | ||
"main": "index.js", | ||
@@ -33,6 +33,7 @@ "scripts": {}, | ||
"iTerm", | ||
"format" | ||
"format", | ||
"jsome" | ||
], | ||
"dependencies": { | ||
"colorz": "^0.1.3" | ||
"colorz": "^0.1.9" | ||
}, | ||
@@ -43,4 +44,5 @@ "devDependencies": {}, | ||
"index.js", | ||
"README.md" | ||
"README.md", | ||
"lib" | ||
] | ||
} |
180
README.md
# json-colorz | ||
[![NPM version][npm-image]][npm-url] | ||
[![js-standard-style][standard-image]][standard-url] | ||
[![schoolmarm-standard-style][marm-image]][marm-url] | ||
[![experimental][stability-image]][stability-url] | ||
[![Downloads][downloads-image]][downloads-url] | ||
This package allows you to display your json object on the console in a pretty format with colors. | ||
[WIP] | ||
> display a json or javascript object in the console with colorz. | ||
## Why? | ||
[Jsome](https://www.npmjs.com/package/jsome) uses [chalk](https://www.npmjs.com/package/chalk), has a command line interface and can be used in the browser. Json-colorz uses [colorz](https://www.npmjs.com/package/colorz), does not have a cli and is not configured to run in the browser. The motivation here was to stress test `colorz` and jsome (json-colorz) proved to be the module which would push `colorz` to its limits. | ||
So, I advocate to you, the user, USE [JSOME](https://github.com/Javascipt/Jsome). If you like this version, :+1:, but then go star [JSOME](https://github.com/Javascipt/Jsome). All credit for this code goes to Jsome author, [Khalid REHIOUI](https://www.npmjs.com/~javascript). What changes I have made are particular to my use case senarios. And I don't care much about stars and ratings. | ||
## Installation | ||
@@ -17,14 +21,168 @@ ```bash | ||
```js | ||
var jlog = require('json-colorz'); | ||
jlog([{"id":1,"email":"UDawn@porta.gov","active":true},{"id":2,"email":"LZeigler@pharetra.com","active":false},{"id":3,"email":"VSobel@neque.ly","active":false}]); | ||
var jclrz = require('json-colorz'); | ||
var obj = { | ||
install: false, | ||
devpackages: ["colorz", "json-colorz"], | ||
packages: [1, 2, 3], | ||
git: false, | ||
verbose: /(app)/, | ||
dryrun: true, | ||
files: { | ||
gitignore: false, | ||
eslintrc: true, | ||
index: true, | ||
license: false, | ||
package: true, | ||
readme: true, | ||
test: false, | ||
travis: false | ||
}, | ||
meta: { | ||
date: "Mon Oct 19 2015 16:48:33 GMT-0400 (EDT)", | ||
year: "2015", | ||
packageName: "testproj607", | ||
type: "private", | ||
repo: "none", | ||
remote: false, | ||
push: false, | ||
author: "Your Name", | ||
email: "git@your.email", | ||
name: "yourHandle", | ||
url: "https://github.com/yourHandle/testproj607", | ||
version: "0.1.0", | ||
license: "ISC", | ||
description: "An awesome module being created" | ||
} | ||
} | ||
jclrz(obj) | ||
// see image below | ||
``` | ||
![jclrz](http://i.imgur.com/0A3rHRc.png) | ||
The following is a duplication of [jsome's readme](https://github.com/Javascipt/Jsome/blob/master/README.md). References changed where appropriate. | ||
## API | ||
```js | ||
The `jclrz` function returns the object passed as argument so that when debugging, you can print the value of an object without having to change a lot on your code | ||
```javascript | ||
// instead of | ||
var foo = { | ||
bar : obj | ||
} | ||
jclrz (obj) | ||
// you can do this : | ||
var foo = { | ||
bar : jclrz(obj) | ||
} | ||
``` | ||
## Why? | ||
Jsome implements [colors](https://www.npmjs.com/package/colors) by the method of extending String.prototype. So, instead of using [colors](https://www.npmjs.com/package/colors), I wanted to use [colorz](https://www.npmjs.com/package/colorz) -- a basic ansi string substitution method for colorizing output. All credit for this code goes to Jsome author, [Khalid REHIOUI](https://www.npmjs.com/~javascript). | ||
#### `jclrz.level` | ||
You can add some points to show levels of elements... very helpful when you are dealing with complex json objects | ||
```javascript | ||
jclrz.level.show = true | ||
jclrz.level.spaces = 2 | ||
jclrz.level.start = 6 | ||
``` | ||
![jclrz](http://i.imgur.com/txBcXjW.png) | ||
The object `jclrz.level` has as default value the following json : | ||
```javascript | ||
jclrz.level = { | ||
'show' : false, | ||
'char' : '.', | ||
'color' : 'red', | ||
'spaces' : 2, | ||
'start' : 0 | ||
} | ||
``` | ||
You can change the level char, its color ( [see colorz package](http://npmjs.org/package/colorz) ) and the number of spaces for each level. | ||
You can also display your json starting from a specific level to avoid displaying your json starting from the extreme left. You can do that by changing the value `jclrz.level.start`. | ||
#### `jclrz.colors` | ||
You can configure the colors of the displayed json by changing the values of the `jclrz.colors` object which has as default these values. | ||
```javascript | ||
jclrz.colors = { | ||
'num' : 'cyan', // stands for numbers | ||
'str' : 'magenta', // stands for strings | ||
'bool' : 'red', // stands for booleans | ||
'regex' : 'blue', // stands for regular expressions | ||
'undef' : 'grey', // stands for undefined | ||
'null' : 'grey', // stands for null | ||
'attr' : 'green', // objects attributes -> { attr : value } | ||
'quot' : 'yellow', // strings quotes -> "..." | ||
'punc' : 'yellow', // commas seperating arrays and objects values -> [ , , , ] | ||
'brack' : 'yellow' // for both {} and [] | ||
} | ||
``` | ||
You can not only use the color value as string but also you can use an array to specify the background color or you can make things look bold ( [see colorz package for more details](http://npmjs.org/package/colorz) ) | ||
```javascript | ||
jclrz.colors.bool = ['green' , 'bgRed'] | ||
jclrz.colors.attr = ['green' , 'bold'] | ||
jclrz.colors.quot = ['yellow', 'bold'] | ||
jclrz.colors.punc = ['yellow', 'bold'] | ||
jclrz.colors.brack = ['yellow', 'bold'] | ||
``` | ||
![jclrz](http://i.imgur.com/AKoAPJM.png) | ||
#### `jclrz.params` | ||
If you need to disable the colors: | ||
```javascript | ||
jclrz.params.colored = false | ||
``` | ||
When you have a very long json to display, don't make your code blocking... you can enable the asynchronous mode. | ||
```javascript | ||
jclrz.params.async = true; | ||
jclrz(longJson, function () { | ||
/* Your code here */ | ||
}); | ||
``` | ||
The default value of `params` is: | ||
```javascript | ||
jclrz.params = { | ||
'colored' : true | ||
, 'async' : false | ||
} | ||
``` | ||
#### `jclrz.parse` | ||
When you have a json as a string, instead of passing by `JSON.parse` function, you can just call the parse function of jclrz | ||
```javascript | ||
jclrz(JSON.parse('[1,2,3]')) | ||
``` | ||
becomes: | ||
```javascript | ||
jclrz.parse('[1,2,3]') | ||
``` | ||
## See Also | ||
@@ -38,7 +196,7 @@ - [jsome](https://www.npmjs.com/package/jsome): the awesome package json-colorz shamelessly duplicated | ||
[npm-url]: https://npmjs.org/package/json-colorz | ||
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square | ||
[standard-url]: https://github.com/feross/standard | ||
[stability-image]: https://img.shields.io/badge/stability-experimental-orange.svg?style=flat-square | ||
[marm-image]: https://img.shields.io/badge/code%20style-marm-brightgreen.svg?style=flat-square | ||
[marm-url]: https://github.com/akileez/eslint-config-marm | ||
[stability-image]: https://img.shields.io/badge/stability-experimental-darkorange.svg?style=flat-square | ||
[stability-url]: https://github.com/akileez/json-colorz | ||
[downloads-image]: http://img.shields.io/npm/dm/json-colorz.svg?style=flat-square | ||
[downloads-url]: https://npmjs.org/package/json-colorz |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
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
14414
5
203
201
1
1
Updatedcolorz@^0.1.9