├── .eslintrc ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── lib └── engine.js └── package.json /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "marm" 3 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # tmp files 2 | lib-cov 3 | *.seed 4 | *.log 5 | *.csv 6 | *.dat 7 | *.out 8 | *.pid 9 | *.gz 10 | 11 | # tmp folders 12 | pids/ 13 | logs/ 14 | results/ 15 | coverage/ 16 | 17 | # node.js 18 | node_modules/ 19 | npm-debug.log 20 | 21 | # osx 22 | .DS_Store 23 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '0.10' 5 | - '0.12' 6 | - '4' 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License (ISC) 2 | 3 | Copyright (c) 2015-2016, Keith Williams 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 12 | SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 14 | OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 15 | CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 | 17 | 18 | The MIT License (MIT) 19 | 20 | Copyright (c) 2015 Khalid REHIOUI 21 | 22 | Permission is hereby granted, free of charge, to any person obtaining a copy 23 | of this software and associated documentation files (the "Software"), to deal 24 | in the Software without restriction, including without limitation the rights 25 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 26 | copies of the Software, and to permit persons to whom the Software is 27 | furnished to do so, subject to the following conditions: 28 | 29 | The above copyright notice and this permission notice shall be included in all 30 | copies or substantial portions of the Software. 31 | 32 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 33 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 34 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 35 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 36 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 37 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 38 | SOFTWARE. 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # json-colorz 2 | [![NPM version][npm-image]][npm-url] 3 | [![schoolmarm-standard-style][marm-image]][marm-url] 4 | [![experimental][stability-image]][stability-url] 5 | [![Downloads][downloads-image]][downloads-url] 6 | 7 | > display a json or javascript object in the console with colorz. 8 | 9 | ## Why? 10 | [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, is not configured to run in the browser, but can display functions and date calls within javascript objects. This is useful to me for debugging purposes. The main motivation here was to stress test `colorz`. Jsome/json-colorz proved to be the module which would push the limits. 11 | 12 | So, I advocate to you, the user, USE [JSOME](https://github.com/Javascipt/Jsome). If you like this version, :+1:, great, fantastic. 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. 13 | 14 | ## Installation 15 | ```bash 16 | $ npm install json-colorz 17 | ``` 18 | 19 | ## Usage 20 | ```js 21 | var jclrz = require('json-colorz'); 22 | 23 | var obj = { 24 | install: false, 25 | devpackages: ["colorz", "json-colorz"], 26 | packages: [1, 2, 3], 27 | git: false, 28 | verbose: /(app)/, 29 | dryrun: true, 30 | files: { 31 | gitignore: false, 32 | eslintrc: true, 33 | index: true, 34 | license: false, 35 | package: true, 36 | readme: true, 37 | test: false, 38 | travis: false 39 | }, 40 | meta: { 41 | date: "Mon Oct 19 2015 16:48:33 GMT-0400 (EDT)", 42 | year: "2015", 43 | packageName: "testproj607", 44 | type: "private", 45 | repo: "none", 46 | remote: false, 47 | push: false, 48 | author: "Your Name", 49 | email: "git@your.email", 50 | name: "yourHandle", 51 | url: "https://github.com/yourHandle/testproj607", 52 | version: "0.1.0", 53 | license: "ISC", 54 | description: "An awesome module being created" 55 | } 56 | } 57 | 58 | jclrz(obj) 59 | // see image below 60 | ``` 61 | 62 | ![jclrz](http://i.imgur.com/0A3rHRc.png) 63 | 64 | The following is a duplication of [jsome's readme](https://github.com/Javascipt/Jsome/blob/master/README.md). References changed and additions made where appropriate. 65 | 66 | ## API 67 | 68 | The `jclrz` function returns the object passed as an argument. When debugging, you can print the value of an object without having to change a lot on your code 69 | 70 | ```javascript 71 | 72 | // instead of 73 | 74 | var foo = { 75 | bar : obj 76 | } 77 | jclrz (obj) 78 | 79 | // you can do this : 80 | 81 | var foo = { 82 | bar : jclrz(obj) 83 | } 84 | 85 | ``` 86 | 87 | #### `jclrz.level` 88 | 89 | You can add some points to show levels of elements... very helpful when you are dealing with complex json objects 90 | 91 | ```javascript 92 | jclrz.level.show = true 93 | jclrz.level.spaces = 2 94 | jclrz.level.start = 6 95 | ``` 96 | 97 | ![jclrz](http://i.imgur.com/txBcXjW.png) 98 | 99 | The object `jclrz.level` has as default value the following json : 100 | 101 | ```javascript 102 | jclrz.level = { 103 | 'show' : false, 104 | 'char' : '.', 105 | 'color' : 'red', 106 | 'spaces' : 2, 107 | 'start' : 0 108 | } 109 | ``` 110 | 111 | You can change the level char, its color ( [see colorz package](http://npmjs.org/package/colorz) ) and the number of spaces for each level. 112 | 113 | 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`. 114 | 115 | #### `jclrz.colors` 116 | 117 | You can configure the colors of the displayed json by changing the values of the `jclrz.colors` object which has as default these values. 118 | 119 | ```javascript 120 | jclrz.colors = { 121 | 'num' : 'cyan', // stands for numbers 122 | 'str' : 'magenta', // stands for strings 123 | 'bool' : 'red', // stands for booleans 124 | 'regex' : 'blue', // stands for regular expressions 125 | 'undef' : 'grey', // stands for undefined 126 | 'null' : 'grey', // stands for null 127 | 'attr' : 'green', // objects attributes -> { attr : value } 128 | 'quot' : 'yellow', // strings quotes -> "..." 129 | 'punc' : 'yellow', // commas seperating arrays and objects values -> [ , , , ] 130 | 'brack' : 'yellow', // for both {} and [] 131 | 'func' : 'grey' // stands for functions 132 | // dates are not defined and will be displayed in the default term color. 133 | } 134 | ``` 135 | 136 | 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) ) 137 | 138 | 139 | ```javascript 140 | jclrz.colors.bool = ['green' , 'bgRed'] 141 | jclrz.colors.attr = ['green' , 'bold'] 142 | jclrz.colors.quot = ['yellow', 'bold'] 143 | jclrz.colors.punc = ['yellow', 'bold'] 144 | jclrz.colors.brack = ['yellow', 'bold'] 145 | jclrz.colors.date = ['red'] // you can defined a color for dates this way. 146 | ``` 147 | ![jclrz_no_func_date](http://i.imgur.com/e5l1Yox.png) 148 | 149 | #### `jclrz.display` 150 | 151 | You now have the option of displaying functions or dates within a javascript object (plain old javascript objects and arrays). Functions are indented by the level at which they occur. This changes the previous behavior compared to JSOME. 152 | The picture below does not show functions nor dates expanded within an array. Also note, formatting or alignment of functions displayed within an array has not been perfected yet. 153 | 154 | 155 | ```javascript 156 | jclrz.display.func = true 157 | jclrz.display.date = true 158 | jclrz.display.xarr = true 159 | ``` 160 | 161 | ![jclrz_func_date](http://imgur.com/K4mrEME.png) 162 | 163 | The default value of `display` is: 164 | 165 | ```javascript 166 | jclrz.display = { 167 | func: false, 168 | date: false, 169 | xarr: true 170 | } 171 | ``` 172 | 173 | #### `jclrz.params` 174 | 175 | If you need to disable the colors: 176 | 177 | ```javascript 178 | jclrz.params.colored = false 179 | ``` 180 | 181 | When you have a very long json to display, don't make your code blocking... you can enable the asynchronous mode. 182 | 183 | ```javascript 184 | jclrz.params.async = true; 185 | 186 | jclrz(longJson, function () { 187 | /* Your code here */ 188 | }); 189 | ``` 190 | 191 | The default value of `params` is: 192 | 193 | ```javascript 194 | jclrz.params = { 195 | 'colored' : true 196 | , 'async' : false 197 | } 198 | ``` 199 | 200 | #### `jclrz.parse` 201 | 202 | When you have a json as a string, instead of passing by `JSON.parse` function, you can just call the parse function of jclrz 203 | 204 | ```javascript 205 | jclrz(JSON.parse('[1,2,3]')) 206 | ``` 207 | 208 | becomes: 209 | 210 | ```javascript 211 | jclrz.parse('[1,2,3]') 212 | ``` 213 | 214 | ## See Also 215 | - [jsome](https://www.npmjs.com/package/jsome): the awesome package json-colorz shamelessly duplicated 216 | 217 | ## License 218 | [ISC](https://github.com/akileez/json-colorz/blob/master/LICENSE) 219 | 220 | [npm-image]: https://img.shields.io/npm/v/json-colorz.svg?style=flat-square 221 | [npm-url]: https://npmjs.org/package/json-colorz 222 | [marm-image]: https://img.shields.io/badge/code%20style-marm-brightgreen.svg?style=flat-square 223 | [marm-url]: https://github.com/akileez/eslint-config-marm 224 | [stability-image]: https://img.shields.io/badge/stability-experimental-darkorange.svg?style=flat-square 225 | [stability-url]: https://github.com/akileez/json-colorz 226 | [downloads-image]: http://img.shields.io/npm/dm/json-colorz.svg?style=flat-square 227 | [downloads-url]: https://npmjs.org/package/json-colorz 228 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * json-colorz 3 | * 4 | * Copyright (c) 2015-2016 Keith Williams. 5 | * Licensed under the ISC license. 6 | * 7 | * Original Code: 8 | * jsome 9 | * Copyright (c) 2015 Khalid REHIOUI (http://github.com/javascipt) 10 | * Licensed under he MIT License (MIT) 11 | */ 12 | 13 | var colors = { 14 | num : 'cyan', 15 | str : 'magenta', 16 | bool : 'red', 17 | regex : 'blue', 18 | undef : 'grey', 19 | null : 'grey', 20 | attr : 'green', 21 | quot : 'yellow', 22 | punc : 'yellow', 23 | brack : 'yellow', 24 | func : 'grey' 25 | } 26 | 27 | var display = { 28 | func: false, 29 | date: false, 30 | xarr: true 31 | } 32 | 33 | var level = { 34 | show : false, 35 | char : '.', 36 | color : 'red', 37 | spaces : 2, 38 | start : 0 39 | } 40 | 41 | var params = { 42 | colored: true, 43 | async: false 44 | } 45 | 46 | var options = { 47 | colors : colors, 48 | display: display, 49 | level : level, 50 | params : params 51 | } 52 | 53 | var engine = require('./lib/engine').setOptions(options) 54 | 55 | function colorize (engine) { 56 | // main function: jclrz 57 | function jclrz (json, cb) { 58 | if (!jclrz.params.async) { 59 | process.stdout.write(engine.gen(json, options.level.start) + '\n') 60 | } else { 61 | process.nextTick(function () { 62 | process.stdout.write(engine.gen(json, options.level.start) + '\n') 63 | cb && cb() 64 | }) 65 | } 66 | return json 67 | } 68 | 69 | // parse 70 | jclrz.parse = function (jsonString, cb) { 71 | return jclrz(JSON.parse(jsonString), cb) 72 | } 73 | 74 | // options 75 | jclrz.colors = colors 76 | jclrz.display = display 77 | jclrz.level = level 78 | jclrz.params = params 79 | 80 | return jclrz 81 | } 82 | 83 | module.exports = colorize(engine) 84 | -------------------------------------------------------------------------------- /lib/engine.js: -------------------------------------------------------------------------------- 1 | var colorz = require('colorz') 2 | 3 | function engine () { 4 | var options 5 | 6 | function kindOf (value) { 7 | if (value === null) return 'null' 8 | if (value === undefined) return 'undefined' 9 | if (Array.isArray(value)) return 'array' 10 | 11 | return typeof value === 'object' 12 | ? Object.prototype.toString.call(value) 13 | .replace(/^\[object |\]$/g, '') 14 | .toLowerCase() 15 | : typeof value 16 | } 17 | 18 | function isObject (obj) { 19 | return Object.prototype.toString.call(obj) === '[object Object]' 20 | } 21 | 22 | function filter (arr, fn) { 23 | var result = [] 24 | 25 | var val 26 | var i = -1 27 | var j = -1 28 | var len = arr.length 29 | 30 | while (++i < len) { 31 | val = arr[i] 32 | if (fn(val, i, arr)) result[++j] = val 33 | } 34 | return result 35 | } 36 | 37 | function repeat (str, n) { 38 | var result = '' 39 | 40 | while (n > 0) { 41 | if (n % 2) result += str 42 | n = Math.floor(n / 2) 43 | str += str 44 | } 45 | return result 46 | } 47 | 48 | function indent (str, char, num) { 49 | if (num === 0) return str 50 | char = num > 1 51 | ? repeat(char, num) 52 | : char 53 | 54 | var re = /^(?!\s*$)/mg 55 | return str.replace(re, char) 56 | } 57 | 58 | function getType (value) { 59 | var map = { 60 | 'number' : 'num', 61 | 'string' : 'str', 62 | 'boolean' : 'bool', 63 | 'function' : 'func', 64 | 'null' : 'null', 65 | 'undefined' : 'undef', 66 | 'object' : 'obj', 67 | 'array' : 'arr', 68 | 'regexp' : 'regex', 69 | 'date' : 'date' 70 | } 71 | return map[kindOf(value)] || map['' + value] 72 | } 73 | 74 | function cleanObject (obj) { 75 | var lastKey = '' 76 | var key 77 | 78 | for (key in obj) { 79 | // get object's prototype last key for placement of trailing comma 80 | lastKey = key 81 | } 82 | return lastKey 83 | } 84 | 85 | function cleanArray (arr) { 86 | if (options.display.xarr) return arr 87 | 88 | return filter(arr, function (item) { 89 | return ['func', 'date'].indexOf(getType(item)) === -1 90 | }) 91 | } 92 | 93 | function generateLevel (level) { 94 | var levelStr = repeat(' ', options.level.spaces) 95 | var opts = options.level 96 | 97 | if (options.level.show && levelStr.length) { 98 | levelStr = levelStr.replace(' ', useColorProvider(opts.char, opts.color)) 99 | } 100 | return repeat(levelStr, level) 101 | } 102 | 103 | function hasChild (obj) { 104 | var key 105 | for (key in obj) { 106 | if (Array.isArray(obj[key]) || isObject(obj[key])) return true 107 | } 108 | } 109 | 110 | function colorify (value, level) { 111 | var type = getType(value) 112 | var color = options.colors[type] 113 | 114 | return generateLevel(type === 'func' ? 0 : level) 115 | + (type === 'str' ? colorifySpec('"', 'quot') : '') 116 | + useColorProvider(formatOutputType(value, type, level), color) 117 | + (type === 'str' ? colorifySpec('"', 'quot') : '') 118 | } 119 | 120 | function colorifySpec (char, type, level) { 121 | return generateLevel(level) + useColorProvider('' + char, options.colors[type]) 122 | } 123 | 124 | function useColorProvider (str, color) { 125 | if (!color) return str 126 | if (options.params.colored) { 127 | if (Array.isArray(color) && color.length > 1) { 128 | return useColorProvider(colorz[color[0]](str), color.slice(1)) 129 | } else { 130 | return colorz[Array.isArray(color) ? color[0] : color](str) 131 | } 132 | } 133 | return str 134 | } 135 | 136 | function formatOutputType (value, type, level) { 137 | if (type === 'func') { 138 | if (options.display.func) { 139 | var str = value.toString().split(/\n/) 140 | var first = str[0] + '\n' 141 | var rest = str.slice(1) 142 | return first + indent(rest.join('\n'), ' ', generateLevel(level).length) 143 | } else { 144 | return '[Function]' 145 | } 146 | } 147 | 148 | if (type === 'date' && !options.display.date) return '[Date]' 149 | 150 | return '' + value 151 | } 152 | 153 | return { 154 | gen: function (json, level, isChild) { 155 | var colored = '' 156 | var result 157 | var key 158 | 159 | level = level || 0 160 | 161 | if (isObject(json)) { 162 | var lastKey = cleanObject(json) 163 | colored += colorifySpec('{', 'brack', isChild ? 0 : level) + '\n' 164 | level++ 165 | 166 | for (key in json) { 167 | result = colorifySpec(key, 'attr', level) 168 | + colorifySpec(': ', 'punc') 169 | + this.gen(json[key], level, true) 170 | + (key !== lastKey ? colorifySpec(',', 'punc') : '') 171 | 172 | colored += result + '\n' 173 | } 174 | 175 | colored += colorifySpec('}', 'brack', --level) 176 | } else if (Array.isArray(json)) { 177 | json = cleanArray(json) 178 | 179 | if (hasChild(json)) { 180 | result = json.map(function (item) { 181 | return this.gen(item, level + 1) 182 | }.bind(this)) 183 | 184 | colored += colorifySpec('[', 'brack', isChild ? 0 : level) 185 | colored += result.join(colorifySpec(', ', 'punc') + '\n') 186 | colored += colorifySpec(']', 'brack', level) 187 | } else { 188 | var coloredArray = colorifySpec('[', 'brack', isChild ? 0 : level) 189 | 190 | for (key in json) { 191 | coloredArray += colorify(json[key]) + (json.length - 1 > key ? colorifySpec(', ', 'punc') : '') 192 | } 193 | 194 | colored += coloredArray + colorifySpec(']', 'brack') 195 | } 196 | } else { 197 | return generateLevel(isChild ? 0 : level) 198 | + colorify(json, typeof json === 'function' ? level : '') 199 | } 200 | 201 | return colored 202 | }, 203 | 204 | setOptions: function (opts) { 205 | options = opts 206 | return this 207 | } 208 | } 209 | } 210 | 211 | module.exports = engine() 212 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-colorz", 3 | "version": "0.2.7", 4 | "description": "display a json or javascript object in the console with colorz", 5 | "main": "index.js", 6 | "scripts": {}, 7 | "engines": { 8 | "node": ">=0.10.0" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/akileez/json-colorz.git" 13 | }, 14 | "author": { 15 | "name": "Keith Williams", 16 | "email": "git@keeda.net", 17 | "url": "https://github.com/akileez/json-colorz" 18 | }, 19 | "homepage": "https://github.com/akileez/json-colorz", 20 | "bugs": { 21 | "url": "https://github.com/akileez/json-colorz/issues" 22 | }, 23 | "license": "ISC", 24 | "keywords": [ 25 | "json", 26 | "color", 27 | "colors", 28 | "ansi", 29 | "node", 30 | "console", 31 | "terminal", 32 | "iTerm", 33 | "format", 34 | "jsome" 35 | ], 36 | "dependencies": { 37 | "colorz": "^0.1.9" 38 | }, 39 | "devDependencies": {}, 40 | "files": [ 41 | "LICENSE", 42 | "index.js", 43 | "README.md", 44 | "lib" 45 | ] 46 | } 47 | --------------------------------------------------------------------------------