├── README.md ├── package.json ├── pretty-data.js └── test ├── test_css.js ├── test_json.js ├── test_sql.js └── test_xml.js /README.md: -------------------------------------------------------------------------------- 1 | # pretty-data - Nodejs plugin 2 | 3 | nodejs plugin to **pretty-print** or **minify** 4 | text in **XML**, **JSON**, **CSS** and **SQL** formats. 5 | 6 | **Version** - 0.50.0 7 | 8 | **Copyright** (c) 2012 Vadim Kiryukhin ( vkiryukhin @ gmail.com ) 9 | 10 | **Home page:** [http://www.eslinstructor.net/pretty-data/](http://www.eslinstructor.net/pretty-data/) 11 | 12 | **License:** Dual licensed under 13 | the MIT and GPL licenses: 14 | 15 | [http://www.opensource.org/licenses/mit-license.php](http://www.opensource.org/licenses/mit-license.php) 16 | 17 | [http://www.gnu.org/licenses/gpl.html](http://www.gnu.org/licenses/gpl.html) 18 | 19 | ##Description 20 | 21 | * `pd.xml(data )` - pretty print XML; 22 | 23 | * `pd.json(data)` - pretty print JSON; 24 | 25 | * `pd.css(data )` - pretty print CSS; 26 | 27 | * `pd.sql(data )` - pretty print SQL; 28 | 29 | * `pd.xmlmin(data [, preserveComments]) ` - minify XML; 30 | 31 | * `pd.jsonmin(data)` - minify JSON text; 32 | 33 | * `pd.cssmin(data [, preserveComments] )` - minify CSS text; 34 | 35 | * `pd.sqlmin(data)` - minify SQL text; 36 | 37 | **PARAMETERS:** 38 | 39 | `@data` - String; XML, JSON, CSS or SQL text to beautify; 40 | 41 | `@preserveComments` - Bool (optional, used in npp.minxml and npp.mincss only); 42 | Set this flag to true to prevent removing comments from @data; 43 | 44 | `@Return` - String; 45 | 46 | 47 | **USAGE:** 48 | 49 | `var pd = require('pretty-data').pd; ` 50 | 51 | `var xml_pp = pd.xml(data); ` 52 | 53 | `var xml_min = pd.xmlmin(data [,true]);` 54 | 55 | `var json_pp = pd.json(data);` 56 | 57 | `var json_min = pd.jsonmin(data);` 58 | 59 | `var css_pp = pd.css(data); ` 60 | 61 | `var css_min = pd.cssmin(data [, true]);` 62 | 63 | `var sql_pp = pd.sql(data);` 64 | 65 | `var sql_min = pd.sqlmin(data);` 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pretty-data", 3 | "version": "0.50.0", 4 | "author": "Vadim Kiryukhin ", 5 | "description": "plugin to pretty-print or minify XML, JSON, CSS and SQL files", 6 | "contributors": [ 7 | { 8 | "name": "Vadim Kiryukhin", 9 | "email": "vkiryukhin@gmail.com" 10 | } 11 | ], 12 | "repository": { 13 | "type": "git", 14 | "url": "http://github.com/vkiryukhin/pretty-data.git" 15 | }, 16 | "main":"./pretty-data", 17 | "keywords": [ 18 | "pretty print", 19 | "beautify", 20 | "minify", 21 | "XML", 22 | "JSON", 23 | "CSS", 24 | "SQL" 25 | ], 26 | "license": "MIT", 27 | "engine": { 28 | "node": ">=0.4.9" 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /pretty-data.js: -------------------------------------------------------------------------------- 1 | /** 2 | * pretty-data - nodejs plugin to pretty-print or minify data in XML, JSON and CSS formats. 3 | * 4 | * Version - 0.40.0 5 | * Copyright (c) 2012 Vadim Kiryukhin 6 | * vkiryukhin @ gmail.com 7 | * http://www.eslinstructor.net/pretty-data/ 8 | * 9 | * Dual licensed under the MIT and GPL licenses: 10 | * http://www.opensource.org/licenses/mit-license.php 11 | * http://www.gnu.org/licenses/gpl.html 12 | * 13 | * pd.xml(data ) - pretty print XML; 14 | * pd.json(data) - pretty print JSON; 15 | * pd.css(data ) - pretty print CSS; 16 | * pd.sql(data) - pretty print SQL; 17 | * 18 | * pd.xmlmin(data [, preserveComments] ) - minify XML; 19 | * pd.jsonmin(data) - minify JSON; 20 | * pd.cssmin(data [, preserveComments] ) - minify CSS; 21 | * pd.sqlmin(data) - minify SQL; 22 | * 23 | * PARAMETERS: 24 | * 25 | * @data - String; XML, JSON, CSS or SQL text to beautify; 26 | * @preserveComments - Bool (optional, used in minxml and mincss only); 27 | * Set this flag to true to prevent removing comments from @text; 28 | * @Return - String; 29 | * 30 | * USAGE: 31 | * 32 | * var pd = require('pretty-data').pd; 33 | * 34 | * var xml_pp = pd.xml(xml_text); 35 | * var xml_min = pd.xmlmin(xml_text [,true]); 36 | * var json_pp = pd.json(json_text); 37 | * var json_min = pd.jsonmin(json_text); 38 | * var css_pp = pd.css(css_text); 39 | * var css_min = pd.cssmin(css_text [, true]); 40 | * var sql_pp = pd.sql(sql_text); 41 | * var sql_min = pd.sqlmin(sql_text); 42 | * 43 | * TEST: 44 | * comp-name:pretty-data$ node ./test/test_xml 45 | * comp-name:pretty-data$ node ./test/test_json 46 | * comp-name:pretty-data$ node ./test/test_css 47 | * comp-name:pretty-data$ node ./test/test_sql 48 | */ 49 | 50 | 51 | function pp() { 52 | this.shift = ['\n']; // array of shifts 53 | this.step = ' '; // 2 spaces 54 | var ix = 0; 55 | 56 | // initialize array with shifts; nesting level == 100 // 57 | for(ix=0;ix<100;ix++){ 58 | this.shift.push(this.shift[ix]+this.step); 59 | } 60 | 61 | } 62 | 63 | // ----------------------- XML section ---------------------------------------------------- 64 | 65 | pp.prototype.xml = function(text) { 66 | 67 | var ar = text.replace(/>\s{0,}<") 68 | .replace(/ or -1) { 81 | str += this.shift[deep]+ar[ix]; 82 | inComment = true; 83 | // end comment or // 84 | if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1 || ar[ix].search(/!DOCTYPE/) > -1 ) { 85 | inComment = false; 86 | } 87 | } else 88 | // end comment or // 89 | if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1) { 90 | str += ar[ix]; 91 | inComment = false; 92 | } else 93 | // // 94 | if( /^<\w/.exec(ar[ix-1]) && /^<\/\w/.exec(ar[ix]) && 95 | /^<[\w:\-\.\,]+/.exec(ar[ix-1]) == /^<\/[\w:\-\.\,]+/.exec(ar[ix])[0].replace('/','')) { 96 | str += ar[ix]; 97 | if(!inComment) deep--; 98 | } else 99 | // // 100 | if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) == -1 && ar[ix].search(/\/>/) == -1 ) { 101 | str = !inComment ? str += this.shift[deep++]+ar[ix] : str += ar[ix]; 102 | } else 103 | // ... // 104 | if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) > -1) { 105 | str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix]; 106 | } else 107 | // // 108 | if(ar[ix].search(/<\//) > -1) { 109 | str = !inComment ? str += this.shift[--deep]+ar[ix] : str += ar[ix]; 110 | } else 111 | // // 112 | if(ar[ix].search(/\/>/) > -1 ) { 113 | str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix]; 114 | } else 115 | // // 116 | if(ar[ix].search(/<\?/) > -1) { 117 | str += this.shift[deep]+ar[ix]; 118 | } else 119 | // xmlns // 120 | if( ar[ix].search(/xmlns\:/) > -1 || ar[ix].search(/xmlns\=/) > -1) { 121 | str += this.shift[deep]+ar[ix]; 122 | } 123 | 124 | else { 125 | str += ar[ix]; 126 | } 127 | } 128 | 129 | return (str[0] == '\n') ? str.slice(1) : str; 130 | }; 131 | 132 | // ----------------------- JSON section ---------------------------------------------------- 133 | 134 | pp.prototype.json = function(text) { 135 | 136 | if ( typeof text === "string" ) { 137 | return JSON.stringify(JSON.parse(text), null, this.step); 138 | } 139 | if ( typeof text === "object" ) { 140 | return JSON.stringify(text, null, this.step); 141 | } 142 | return null; 143 | }; 144 | 145 | // ----------------------- CSS section ---------------------------------------------------- 146 | 147 | pp.prototype.css = function(text) { 148 | 149 | var ar = text.replace(/\s{1,}/g,' ') 150 | .replace(/\{/g,"{~::~") 151 | .replace(/\}/g,"~::~}~::~") 152 | .replace(/\;/g,";~::~") 153 | .replace(/\/\*/g,"~::~/*") 154 | .replace(/\*\//g,"*/~::~") 155 | .replace(/~::~\s{0,}~::~/g,"~::~") 156 | .split('~::~'), 157 | len = ar.length, 158 | deep = 0, 159 | str = '', 160 | ix = 0; 161 | 162 | for(ix=0;ix/g,""); 292 | return str.replace(/>\s{0,}<"); 293 | }; 294 | 295 | pp.prototype.jsonmin = function(text) { 296 | 297 | return text.replace(/\s{0,}\{\s{1,}/g,"{") 298 | .replace(/\s{0,}\[$/g,"[") 299 | .replace(/\[\s{0,}/g,"[") 300 | .replace(/:\s{0,}\[/g,':[') 301 | .replace(/\s{1,}\}\s{0,}/g,"}") 302 | .replace(/\s{0,}\]\s{0,}/g,"]") 303 | .replace(/\"\s{0,}\,/g,'",') 304 | .replace(/\,\s{0,}\"/g,',"') 305 | .replace(/\"\s{0,}:/g,'":') 306 | .replace(/:\s{0,}\"/g,':"') 307 | .replace(/:\s{0,}\[/g,':[') 308 | .replace(/\,\s{0,}\[/g,',[') 309 | .replace(/\,\s{2,}/g,', ') 310 | .replace(/\]\s{0,},\s{0,}\[/g,'],['); 311 | }; 312 | 313 | pp.prototype.cssmin = function(text, preserveComments) { 314 | 315 | var str = preserveComments ? text 316 | : text.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\//g,"") ; 317 | return str.replace(/\s{1,}/g,' ') 318 | .replace(/\{\s{1,}/g,"{") 319 | .replace(/\}\s{1,}/g,"}") 320 | .replace(/\;\s{1,}/g,";") 321 | .replace(/\/\*\s{1,}/g,"/*") 322 | .replace(/\*\/\s{1,}/g,"*/"); 323 | }; 324 | 325 | pp.prototype.sqlmin = function(text) { 326 | return text.replace(/\s{1,}/g," ").replace(/\s{1,}\(/,"(").replace(/\s{1,}\)/,")"); 327 | }; 328 | 329 | // -------------------------------------------------------------------------------------------- 330 | 331 | exports.pd= new pp(); 332 | -------------------------------------------------------------------------------- /test/test_css.js: -------------------------------------------------------------------------------- 1 | 2 | var css = '.headbg{margin:0 8px;display:none; }a:link,a:focus{ color:#00c }\n /* comment */ a:active{ color:red }', 3 | pp_css = require('../pretty-data').pd.css(css), 4 | pp_cssmin_com = require('../pretty-data').pd.cssmin(css,true), 5 | pp_cssmin = require('../pretty-data').pd.cssmin(css); 6 | 7 | console.log('\n==============================================================================\n'); 8 | console.log('\n/*------- Original CSS string: -------*/\n\n' + css + '\n'); 9 | console.log('\n/*------- Beautified original CSS -------------*/\n\n' + pp_css + '\n'); 10 | console.log('\n/*------- Minified original CSS with preserved comments: -------*/\n\n' + pp_cssmin_com + '\n'); 11 | console.log('\n/*------- Minified original CSS with deleted comments: ---------*/\n\n' + pp_cssmin + '\n'); 12 | console.log('\n===============================================================================\n'); 13 | -------------------------------------------------------------------------------- /test/test_json.js: -------------------------------------------------------------------------------- 1 | 2 | var json = '{"menu":{"id": "file","welcome": "text{{variable}}text","value": \n[[1,2,3],[4,5,6] ],\n"popup":{"menuitem":[{"value": ["one","two"],\n"onclick":"CreateNewDoc()"},{"value":"Close","onclick":"CloseDoc()"}, {\n"welcome": "abc {{variable}} xyz"\n}]}}}', 3 | 4 | json_pretty = require('../pretty-data').pd.json(json), // pretty original 5 | json_min = require('../pretty-data').pd.jsonmin(json), // min original 6 | json_pretty2 = require('../pretty-data').pd.json(json_min), // pretty min 7 | json_min2 = require('../pretty-data').pd.jsonmin(json_pretty2); // min pretty 8 | 9 | console.log('\n==============================================================================\n'); 10 | console.log('\n/*------- Original JSON string: -------*/\n\n' + json + '\n'); 11 | 12 | console.log('\n/*------- Beautify original JSON: -------------*/\n\n' + json_pretty + '\n'); 13 | console.log('\n/*------- Minify original JSON: ---------------*/\n\n' + json_min + '\n'); 14 | 15 | console.log('\n/*------- Beautify minified JSON: -------------*/\n\n' + json_pretty2 + '\n'); 16 | console.log('\n/*------- Minify beautified JSON: ---------------*/\n\n' + json_min2 + '\n'); 17 | console.log('\n===============================================================================\n'); 18 | -------------------------------------------------------------------------------- /test/test_sql.js: -------------------------------------------------------------------------------- 1 | 2 | var sql = "select ca.proj_id as proj_id, ca.ca_name as proj_name, ca.ca_date_start as proj_start, ca.ca_date_end AS proj_end,\n(select COUNT(*) from rotations r \nwhere r.proj_id = proj_id and r.r_status = 'R' \ngroup by r.proj_id) r_count, \n(select count(*) from rotations r \nwhere r.proj_id = proj_id and r.channel_id = 24) r_rtb_count \nfrom projs ca, clients c, proj_auth caa \nwhere ca.client_id = 12345 and ca.client_id = c.client_id and ca_type = 'zzz' \nand c.agency_id = 0 and ca.client_id = NVL( caa.client_id, ca.client_id) \nand proj_id = NVL( caa.proj_id, proj_id) and caa.contact_id = 7890"; 3 | var sql_pp = require('../pretty-data').pd.sql(sql); 4 | var sql_min = require('../pretty-data').pd.sqlmin(sql); 5 | 6 | console.log('\n==============================================================================\n'); 7 | console.log('\n/*------- Original SQL string: -------*/\n\n' + sql + '\n'); 8 | console.log('\n/*------- Beautified SQL: -------------*/\n\n' + sql_pp + '\n'); 9 | console.log('\n/*------- Minified SQL: ---------------*/\n\n' + sql_min + '\n'); 10 | console.log('\n===============================================================================\n'); 11 | -------------------------------------------------------------------------------- /test/test_xml.js: -------------------------------------------------------------------------------- 1 | 2 | var xml = ' bbb ]]>', 3 | pp_xml = require('../pretty-data').pd.xml(xml), 4 | pp_xmlmin_com = require('../pretty-data').pd.xmlmin(xml,true), 5 | pp_xmlmin = require('../pretty-data').pd.xmlmin(xml); 6 | 7 | console.log('\n==============================================================================\n'); 8 | console.log('\n/*------- Original XML string: -------*/\n\n' + xml + '\n'); 9 | console.log('\n/*------- Beautified XML -------------*/\n\n' + pp_xml + '\n'); 10 | console.log('\n/*------- Minified XML with preserved comments: -------*/\n\n' + pp_xmlmin_com + '\n'); 11 | console.log('\n/*------- Minified XML with deleted comments: ---------*/\n\n' + pp_xmlmin + '\n'); 12 | console.log('\n===============================================================================\n'); 13 | --------------------------------------------------------------------------------