├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── bin └── cli.js ├── examples ├── example1.png ├── example2.png └── example3.png ├── lib ├── browser-style.js └── generator.js ├── logo.png ├── package.json ├── script.js └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '4' 5 | - '6' 6 | - '7' 7 | - '8' 8 | - '9' 9 | before_script: npm link 10 | script: jsome package.json && cat package.json | jsome && npm test -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Khalid REHIOUI 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Make your JSON objects look AWESOME! 2 | ==================================== 3 | 4 | ![Jsome](https://api.travis-ci.org/Javascipt/Jsome.svg) 5 | ![Jsome](https://david-dm.org/Javascipt/Jsome.svg) 6 | ![Jsome](https://img.shields.io/npm/dm/jsome.svg) 7 | 8 | ![Downloads stats](https://nodei.co/npm/jsome.png?downloadRank=true&stars=true) 9 | 10 | ![Jsome](https://raw.githubusercontent.com/Javascipt/Jsome/master/logo.png) 11 | 12 | This package allows you to give style to your JSON on your console! 13 | 14 | ## Installation : 15 | 16 | ```bash 17 | $ npm install jsome 18 | ``` 19 | 20 | if you need to use jsome as a command line, you may need to instal it globally 21 | 22 | ```bash 23 | $ [sudo] npm install -g jsome 24 | ``` 25 | 26 | ## How does it work ? 27 | 28 | #### Command line : 29 | 30 | Using jsome as a command line, you need to run the following command that takes the path to your json file as argument 31 | 32 | ```bash 33 | $ jsome /path/to/your/json/file.json 34 | $ jsome [options] /path/to/your/json/file.json 35 | ``` 36 | 37 | You can also send a json string through a pipe (`|`) 38 | 39 | ```bash 40 | $ cat /path/to/your/json/file.json | jsome 41 | ``` 42 | 43 | The options available are : 44 | - `-c`: to enable or disable colors (defualt value: true) 45 | - `-l`: to enable or disable levels (default value: false) 46 | - `-s`: to specify the number of tabulation spaces (default value: 2) 47 | - `-r`: to specify valid JSON as output (default value: true) 48 | 49 | examples : 50 | 51 | ```bash 52 | $ jsome -c false /path/to/your/file.json 53 | $ jsome -c false -l true /path/to/your/file.json 54 | $ jsome -s 4 /path/to/your/file.json 55 | ``` 56 | 57 | ##### Module : 58 | 59 | On your nodejs application, when you need to console.log a json object, all you need to do is to use the jsome function 60 | 61 | ```javascript 62 | var jsome = require('jsome'); 63 | jsome([{"id":1,"email":"Khalid@Morocco.ma","active":true},{"id":2,"email":"Someone@somewhere.com","active":false},{"id":3,"email":"chinese@bamboo.tree","active":true}]); 64 | ``` 65 | 66 | Then your json object will be displayed on the console in a pretty format with Awsome colors ! 67 | Here is the result : 68 | 69 | ![jsome](https://raw.githubusercontent.com/Javascipt/Jsome/master/examples/example1.png) 70 | 71 | The `jsome` 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 72 | 73 | ```javascript 74 | 75 | // instead of 76 | 77 | var foo = { 78 | bar : obj 79 | } 80 | jsome (obj); 81 | 82 | // you can do this : 83 | 84 | var foo = { 85 | bar : jsome(obj) 86 | } 87 | 88 | ``` 89 | 90 | You can add some points to show levels of elements... very helpful when you are dealing with complex json objects 91 | 92 | ```javascript 93 | jsome.level.show = true; 94 | ``` 95 | 96 | ![jsome](https://raw.githubusercontent.com/Javascipt/Jsome/master/examples/example2.png) 97 | 98 | The object `jsome.level` has as default value the following json : 99 | 100 | ```javascript 101 | jsome.level = { 102 | 'show' : false 103 | , 'char' : '.' 104 | , 'color' : 'red' 105 | , 'spaces' : 2 106 | , 'start' : 0 107 | } 108 | ``` 109 | 110 | You can change the level char, its color ( [see chalk package](http://npmjs.org/package/chalk) ) and the number of spaces for each level. 111 | 112 | 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 `jsome.level.start`. 113 | 114 | You can configure the colors of the displayed json by changing the values of the `jsome.colors` object which has as default these values. 115 | 116 | ```javascript 117 | jsome.colors = { 118 | 'num' : 'cyan' // stands for numbers 119 | , 'str' : 'magenta' // stands for strings 120 | , 'bool' : 'red' // stands for booleans 121 | , 'regex' : 'blue' // stands for regular expressions 122 | , 'undef' : 'grey' // stands for undefined 123 | , 'null' : 'grey' // stands for null 124 | , 'attr' : 'green' // objects attributes -> { attr : value } 125 | , 'quot' : 'yellow' // strings quotes -> "..." 126 | , 'punc' : 'yellow' // commas seperating arrays and objects values -> [ , , , ] 127 | , 'brack' : 'yellow' // for both {} and [] 128 | } 129 | ``` 130 | 131 | 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 chalk package for more details](http://npmjs.org/package/chalk) ) 132 | 133 | 134 | ```javascript 135 | jsome.colors.bool = ['green' , 'bgRed'] 136 | jsome.colors.attr = ['green' , 'bold'] 137 | jsome.colors.quot = ['yellow', 'bold'] 138 | jsome.colors.punc = ['yellow', 'bold'] 139 | jsome.colors.brack = ['yellow', 'bold'] 140 | ``` 141 | ![jsome](https://raw.githubusercontent.com/Javascipt/Jsome/master/examples/example3.png) 142 | 143 | 144 | When you have a json as a string, instead of passing by `JSON.parse` function, you can just call the parse function of jsome 145 | 146 | ```javascript 147 | jsome(JSON.parse('[1,2,3]')); 148 | ``` 149 | 150 | becomes: 151 | 152 | ```javascript 153 | jsome.parse('[1,2,3]'); 154 | ``` 155 | 156 | If you need to disable the colors: 157 | 158 | ```javascript 159 | jsome.params.colored = false; 160 | ``` 161 | 162 | If you need JSON which pases linting: 163 | 164 | ```javascript 165 | jsome.params.lintable = true; 166 | ``` 167 | 168 | When you have a very long json to display, don't make your code blocking... you can enable the asynchronous mode. 169 | 170 | ```javascript 171 | jsome.params.async = true; 172 | 173 | jsome(longJson, function () { 174 | /* Your code here */ 175 | }); 176 | ``` 177 | 178 | The default value of `params` is: 179 | 180 | ```javascript 181 | jsome.params = { 182 | 'colored' : true 183 | , 'async' : false 184 | , 'lintable': false 185 | } 186 | ``` 187 | 188 | In order to get the colored string without printing it on the console : 189 | 190 | ```javascript 191 | var coloredString = jsome.getColoredString(obj) 192 | ``` 193 | -------------------------------------------------------------------------------- /bin/cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var path = require("path") 4 | , fs = require("fs") 5 | , jsome = require("../script"); 6 | 7 | var argv = require('yargs') 8 | .usage('Usage: jsome [options] ') 9 | .option('c', { 10 | alias : 'colors' 11 | , default : true 12 | , describe : 'color the output' 13 | , type : 'boolean' 14 | }).option('l', { 15 | alias : 'levels' 16 | , default : false 17 | , describe : 'show indentation levels' 18 | , type : 'boolean' 19 | }).option('s', { 20 | alias : 'spaces' 21 | , default : 2 22 | , describe : 'specifying tabulation spaces' 23 | , type : 'number' 24 | }).option('r',{ 25 | alias : 'lintable' 26 | , default : false 27 | , describe : 'output valid json' 28 | , type : 'boolean' 29 | }) 30 | .example('jsome -cl /some/dir/file.json', 'print out the content of file.json in color displaying indentation levels') 31 | .example('jsome -c false -l /some/dir/file.json', 'print out the content of file.json without color but with indentation levels') 32 | .help('h') 33 | .argv; 34 | 35 | jsome.params.colored = argv.c; 36 | jsome.params.lintable = argv.r; 37 | jsome.level.show = argv.l; 38 | jsome.level.spaces = argv.s; 39 | 40 | var filePath = argv._[0] || ''; 41 | 42 | if(filePath) { 43 | fs.exists(path.resolve(filePath), function (exists) { 44 | if(!exists) return jsome({ error : "File doesn't exist" }); 45 | fs.readFile(path.resolve(filePath), function (error, jsonString) { 46 | if(error) return jsome ({ error : error.message }); 47 | jsome.parse(jsonString.toString()); 48 | }); 49 | }); 50 | } else { 51 | process.stdin.resume(); 52 | process.stdin.setEncoding('utf8'); 53 | 54 | process.stdin.on('data', function (data) { 55 | jsome(JSON.parse(data)); 56 | }); 57 | } 58 | -------------------------------------------------------------------------------- /examples/example1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Javascipt/Jsome/c2e8fb4f140293c5cf57d6d3ac5f8a7104795714/examples/example1.png -------------------------------------------------------------------------------- /examples/example2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Javascipt/Jsome/c2e8fb4f140293c5cf57d6d3ac5f8a7104795714/examples/example2.png -------------------------------------------------------------------------------- /examples/example3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Javascipt/Jsome/c2e8fb4f140293c5cf57d6d3ac5f8a7104795714/examples/example3.png -------------------------------------------------------------------------------- /lib/browser-style.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | black : 'color: #000000' 3 | , red : 'color: #FF0000' 4 | , green : 'color: #00FF00' 5 | , yellow : 'color: #FFFF00' 6 | , blue : 'color: #0000FF' 7 | , magenta : 'color: #FF00FF' 8 | , cyan : 'color: #00FFFF' 9 | , white : 'color: #FFFFFF' 10 | , gray : 'color: #C0C0C0' 11 | , bgBlack : 'background: #000000' 12 | , bgRed : 'background: #FF0000' 13 | , bgGreen : 'background: #00FF00' 14 | , bgYellow : 'background: #FFFF00' 15 | , bgBlue : 'background: #0000FF' 16 | , bgMagenta : 'background: #FF00FF' 17 | , bgCyan : 'background: #00FFFF' 18 | , bgWhite : 'background: #000000' 19 | , bgGray : 'background: #C0C0C0' 20 | } -------------------------------------------------------------------------------- /lib/generator.js: -------------------------------------------------------------------------------- 1 | module.exports = (function () { 2 | var jsomeRef 3 | , browserColors = [] 4 | , browserStyle = require('./browser-style') 5 | 6 | function getType (value) { 7 | var map = { 8 | '[object Number]' : 'num' 9 | , '[object String]' : 'str' 10 | , '[object Boolean]' : 'bool' 11 | , '[object RegExp]' : 'regex' 12 | , '[object Function]' : 'func' 13 | , 'null' : 'null' 14 | , 'undefined' : 'undef' 15 | } 16 | 17 | return map[toString.call(value)] || map[''+value]; 18 | } 19 | 20 | function isBrowser () { 21 | return typeof window == 'object'; 22 | } 23 | 24 | function repeat (str, times) { 25 | return Array(times ? (times+1) : 0).join(str); 26 | } 27 | 28 | function cleanObject (obj) { 29 | var lastKey = ''; 30 | for (var key in obj) { 31 | (getType(obj[key])==='func') && delete obj[key] || (lastKey = key); 32 | } 33 | return lastKey; 34 | } 35 | 36 | function cleanArray (arr) { 37 | return arr.filter(function (item) { 38 | return getType(item) !== 'func'; 39 | }); 40 | } 41 | 42 | function generateLevel (level) { 43 | var levelStr = repeat(' ', jsomeRef.level.spaces) 44 | , opts = jsomeRef.level; 45 | 46 | if (jsomeRef.level.show && levelStr.length) { 47 | levelStr = levelStr.replace(' ', useColorProvider(opts.char, opts.color)) 48 | } 49 | 50 | return repeat(levelStr, level); 51 | } 52 | 53 | function hasChild (obj) { 54 | for (var key in obj) { 55 | if (isArray(obj[key]) || isObject(obj[key])) return true; 56 | } 57 | } 58 | 59 | function isArray (arr) { 60 | return toString.call(arr).match(/^\[object Array\]$/); 61 | } 62 | 63 | function isObject (obj) { 64 | return toString.call(obj).match(/^\[object Object\]$/); 65 | } 66 | 67 | function colorify (value, level) { 68 | var color = jsomeRef.colors[getType(value)]; 69 | return generateLevel(level) 70 | + (getType(value) === 'str' ? colorifySpec('"', 'quot') : '') 71 | + useColorProvider('' + value, color) 72 | + (getType(value) === 'str' ? colorifySpec('"', 'quot') : ''); 73 | } 74 | 75 | function colorifySpec (char, type, level) { 76 | var quote = ( 77 | jsomeRef.params.lintable && type === 'attr' 78 | ? colorifySpec('"', 'quot', 0) 79 | : '' 80 | ) 81 | return generateLevel(level) + quote 82 | + useColorProvider('' + char, jsomeRef.colors[type]) + quote; 83 | } 84 | 85 | function useColorProvider (str, color) { 86 | if(isBrowser()) { 87 | var style = (isArray(color) ? color : [color]).map(function (item) { 88 | return browserStyle[item] 89 | }).join(';'); 90 | browserColors.push(style); 91 | return "%c" + str; 92 | } else { 93 | if (jsomeRef.params.colored) { 94 | var chalk = require('chalk'); 95 | if (isArray(color)) { 96 | if(color.length) { 97 | return useColorProvider(chalk[color[0]](str), color.slice(1)); 98 | } else { 99 | return str; 100 | } 101 | } else { 102 | return chalk[color](str); 103 | } 104 | } 105 | } 106 | 107 | return str; 108 | } 109 | 110 | return { 111 | gen : function (json, level, isChild) { 112 | var colored = []; 113 | level = level || 0; 114 | 115 | if (isObject(json)) { 116 | 117 | var lastKey = cleanObject(json); 118 | colored.push(colorifySpec('{', 'brack', isChild ? 0 : level)); 119 | level++; 120 | 121 | for(var key in json) { 122 | var result = colorifySpec(key, 'attr', level) 123 | + colorifySpec(': ', 'punc') 124 | + this.gen(json[key], level, true) 125 | + (key !== lastKey ? colorifySpec(',', 'punc') : ''); 126 | colored.push(result); 127 | } 128 | 129 | colored.push(colorifySpec('}', 'brack', --level)); 130 | 131 | } else if (isArray(json)) { 132 | json = cleanArray(json); 133 | 134 | if (hasChild(json)) { 135 | 136 | var result = json.map(function(item) { 137 | return this.gen(item, level+1); 138 | }.bind(this)); 139 | 140 | colored.push(colorifySpec('[', 'brack', isChild ? 0 : level));; 141 | colored.push(result.join(colorifySpec(', ', 'punc') + '\n' )); 142 | colored.push(colorifySpec(']', 'brack', level)); 143 | 144 | } else { 145 | 146 | var coloredArray = colorifySpec('[', 'brack', isChild ? 0 : level); 147 | for (var key in json) { 148 | coloredArray += colorify(json[key]) + (json.length-1>key ? colorifySpec(', ', 'punc') : ''); 149 | } 150 | colored.push(coloredArray + colorifySpec(']', 'brack')); 151 | 152 | } 153 | 154 | } else { 155 | return generateLevel(isChild ? 0 : level) + colorify(json); 156 | } 157 | 158 | return isBrowser() ? [colored.join('\n')].concat(browserColors) : colored.join('\n'); 159 | }, 160 | setJsomeRef : function (jsome) { 161 | jsomeRef = jsome; 162 | return this; 163 | } 164 | } 165 | 166 | })(); -------------------------------------------------------------------------------- /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Javascipt/Jsome/c2e8fb4f140293c5cf57d6d3ac5f8a7104795714/logo.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jsome", 3 | "version": "2.5.0", 4 | "description": "Make your JSON look AWESOME!", 5 | "main": "script.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/Javascipt/Jsome.git" 9 | }, 10 | "keywords": [ 11 | "colors", 12 | "json", 13 | "console", 14 | "log", 15 | "format", 16 | "pretty", 17 | "print", 18 | "command-line", 19 | "command", 20 | "cli" 21 | ], 22 | "author": "Khalid REHIOUI (http://github.com/javascipt)", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/Javascipt/Jsome/issues" 26 | }, 27 | "homepage": "https://github.com/Javascipt/Jsome#readme", 28 | "dependencies": { 29 | "chalk": "^2.3.0", 30 | "json-stringify-safe": "^5.0.1", 31 | "yargs": "^11.0.0" 32 | }, 33 | "bin": { 34 | "jsome": "./bin/cli.js" 35 | }, 36 | "scripts": { 37 | "test": "jest" 38 | }, 39 | "devDependencies": { 40 | "jest": "^21.2.1", 41 | "jsesc": "^2.5.1" 42 | }, 43 | "jest": { 44 | "testEnvironment": "node" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- 1 | var colors = { 2 | 'num' : 'cyan' 3 | , 'str' : 'magenta' 4 | , 'bool' : 'red' 5 | , 'regex' : 'blue' 6 | , 'undef' : 'grey' 7 | , 'null' : 'grey' 8 | , 'attr' : 'green' 9 | , 'quot' : 'yellow' 10 | , 'punc' : 'yellow' 11 | , 'brack' : 'yellow' 12 | } 13 | 14 | , level = { 15 | 'show' : false 16 | , 'char' : '.' 17 | , 'color' : 'red' 18 | , 'spaces' : 2 19 | , 'start' : 0 20 | } 21 | 22 | , params = { 23 | 'colored' : true 24 | , 'async' : false 25 | , 'lintable': false 26 | } 27 | 28 | 29 | module.exports = (function () { 30 | 31 | function jsome (json, callBack) { 32 | return jsome.parse(stringify(json), callBack); 33 | } 34 | 35 | jsome.colors = colors; 36 | jsome.level = level; 37 | jsome.params = params; 38 | 39 | var generator = require("./lib/generator").setJsomeRef(jsome) 40 | , stringify = require('json-stringify-safe'); 41 | 42 | jsome.parse = function (jsonString, callBack) { 43 | var json = JSON.parse(jsonString); 44 | 45 | if (!jsome.params.async) { 46 | var output = generator.gen(json, jsome.level.start); 47 | if(Array.isArray(output)) { 48 | console.log.apply(console, output); 49 | } else { 50 | console.log(output); 51 | } 52 | } else { 53 | setTimeout(function () { 54 | console.log(generator.gen(json, jsome.level.start)); 55 | callBack && callBack(); 56 | }); 57 | } 58 | 59 | return json; 60 | } 61 | 62 | jsome.getColoredString = function(jsonString, callBack){ 63 | var json = JSON.parse(stringify(jsonString)); 64 | if (!jsome.params.async) { 65 | var output = generator.gen(json, jsome.level.start); 66 | return output 67 | } else { 68 | setTimeout(function () { 69 | var output = generator.gen(json, jsome.level.start) 70 | callBack && callBack(output); 71 | }); 72 | } 73 | } 74 | 75 | 76 | return jsome; 77 | 78 | })(); 79 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'), 2 | jsome = require('../script.js'), 3 | chalk = require('chalk'), 4 | enc = require('jsesc'), 5 | y = chalk.yellow, 6 | g = chalk.green, 7 | m = chalk.magenta 8 | 9 | describe('Jsome run with', function () { 10 | 11 | describe('spec non-compliant coloured output', function () { 12 | var expected = y('{') + 13 | '\n ' + g('string') + y(': ') + y('"') + m('value') + y('"') + 14 | y(',') + 15 | '\n ' + g('list') + y(': ') + y('[') + y('"') + m('one') + y('"') + 16 | y(', ') + y('"') + m('two') + 17 | y('"') + y(']') + 18 | '\n' + y('}') 19 | var actual = jsome.getColoredString( 20 | {'string': 'value', 'list': ['one', 'two']} 21 | ) 22 | 23 | test('should return a simple non-standard json string', function () { 24 | assert.equal(actual, expected) 25 | }) 26 | 27 | test('should return a simple non-standard ANSI escaped, json string', 28 | function () { 29 | assert.equal(enc(actual), enc(expected)) 30 | }) 31 | 32 | }) 33 | 34 | describe('spec compliant coloured output', function () { 35 | var expected = y('{') + 36 | '\n ' + y('"') + g('string') + y('"') + y(': ') + y('"') + m('value') + 37 | y('"') + 38 | y(',') + 39 | '\n ' + y('"') + g('list') + y('"') + y(': ') + y('[') + y('"') + 40 | m('one') + y('"') + 41 | y(', ') + y('"') + m('two') + 42 | y('"') + y(']') + 43 | '\n' + y('}') 44 | jsome.params.lintable = true 45 | var actual = jsome.getColoredString( 46 | {'string': 'value', 'list': ['one', 'two']} 47 | ) 48 | 49 | test('should return a simple non-standard json string', function () { 50 | assert.equal(actual, expected) 51 | }) 52 | 53 | test('should return a simple non-standard ANSI escaped, json string', 54 | function () { 55 | assert.equal(enc(actual), enc(expected)) 56 | }) 57 | }) 58 | 59 | }) --------------------------------------------------------------------------------