Socket
Socket
Sign inDemoInstall

pretty-data

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pretty-data - npm Package Compare versions

Comparing version 0.20.2 to 0.30.0

test/test_sql.js

9

package.json
{
"name": "pretty-data",
"version": "0.20.2",
"version": "0.30.0",
"author": "Vadim Kiryukhin <vkiryukhin@gmail.com>",
"description": "plugin to pretty print or minify XML, JSON and CSS files",
"description": "plugin to pretty print or minify XML, JSON, CSS and SQL files",
"contributors": [

@@ -21,5 +21,6 @@ {

"minify",
"XML",
"XML",
"JSON",
"CSS"
"CSS",
"SQL"
],

@@ -26,0 +27,0 @@ "license": "MIT",

/**
* pretty-data - nodejs plugin to pretty-print or minify data in XML, JSON and CSS formats.
*
* Version - 0.20.2
* Version - 0.30.0
* Copyright (c) 2012 Vadim Kiryukhin

@@ -16,11 +16,13 @@ * vkiryukhin @ gmail.com

* pd.css(data ) - pretty print CSS;
* pd.sql(data) - pretty print SQL;
*
* pd.xmlmin(data [, preserveComments] ) - minify XML;
* pd.jsonmin(data preserveComments) - minify JSON;
* pd.jsonmin(data) - minify JSON;
* pd.cssmin(data [, preserveComments] ) - minify CSS;
* pd.sqlmin(data) - minify SQL;
*
* PARAMETERS:
*
* @data - String; XML, JSON or CSS text to beautify;
* @preserveComments - Bool (optional, used in npp.minxml and npp.mincss only);
* @data - String; XML, JSON, CSS or SQL text to beautify;
* @preserveComments - Bool (optional, used in minxml and mincss only);
* Set this flag to true to prevent removing comments from @text;

@@ -32,3 +34,3 @@ * @Return - String;

* var pd = require('pretty-data').pd;
*
* var xml_pp = pd.xml(xml_text);

@@ -40,7 +42,10 @@ * var xml_min = pd.xmlmin(xml_text [,true]);

* var css_min = pd.cssmin(css_text [, true]);
* var sql_pp = pd.sql(sql_text);
* var sql_min = pd.sqlmin(sql_text);
*/
function pp() {
this.shift = ['\n']; // array of shifts
var step = ' ', // 2 spaces
this.step = ' ', // 2 spaces
maxdeep = 100, // nesting level

@@ -51,3 +56,3 @@ ix = 0;

for(ix=0;ix<maxdeep;ix++){
this.shift.push(this.shift[ix]+step);
this.shift.push(this.shift[ix]+this.step);
}

@@ -57,2 +62,4 @@

// ----------------------- XML section ----------------------------------------------------
pp.prototype.xml = function(text) {

@@ -115,2 +122,4 @@

// ----------------------- JSON section ----------------------------------------------------
pp.prototype.json = function(text) {

@@ -162,2 +171,4 @@

// ----------------------- CSS section ----------------------------------------------------
pp.prototype.css = function(text) {

@@ -196,2 +207,112 @@

// ----------------------- SQL section ----------------------------------------------------
function isSubquery(str, parenthesisLevel) {
return parenthesisLevel - (str.replace(/\(/g,'').length - str.replace(/\)/g,'').length )
}
function split_sql(str, tab) {
return str.replace(/\s{1,}/g," ")
.replace(/ AND /ig,"~::~"+tab+tab+"AND ")
.replace(/ BETWEEN /ig,"~::~"+tab+"BETWEEN ")
.replace(/ CASE /ig,"~::~"+tab+"CASE ")
.replace(/ ELSE /ig,"~::~"+tab+"ELSE ")
.replace(/ END /ig,"~::~"+tab+"END ")
.replace(/ FROM /ig,"~::~FROM ")
.replace(/ GROUP\s{1,}BY/ig,"~::~GROUP BY ")
.replace(/ HAVING /ig,"~::~HAVING ")
//.replace(/ IN /ig,"~::~"+tab+"IN ")
.replace(/ IN /ig," IN ")
.replace(/ JOIN /ig,"~::~JOIN ")
.replace(/ CROSS~::~{1,}JOIN /ig,"~::~CROSS JOIN ")
.replace(/ INNER~::~{1,}JOIN /ig,"~::~INNER JOIN ")
.replace(/ LEFT~::~{1,}JOIN /ig,"~::~LEFT JOIN ")
.replace(/ RIGHT~::~{1,}JOIN /ig,"~::~RIGHT JOIN ")
.replace(/ ON /ig,"~::~"+tab+"ON ")
.replace(/ OR /ig,"~::~"+tab+tab+"OR ")
.replace(/ ORDER\s{1,}BY/ig,"~::~ORDER BY ")
.replace(/ OVER /ig,"~::~"+tab+"OVER ")
.replace(/\(\s{0,}SELECT /ig,"~::~(SELECT ")
.replace(/\)\s{0,}SELECT /ig,")~::~SELECT ")
.replace(/ THEN /ig," THEN~::~"+tab+"")
.replace(/ UNION /ig,"~::~UNION~::~")
.replace(/ USING /ig,"~::~USING ")
.replace(/ WHEN /ig,"~::~"+tab+"WHEN ")
.replace(/ WHERE /ig,"~::~WHERE ")
.replace(/ WITH /ig,"~::~WITH ")
//.replace(/\,\s{0,}\(/ig,",~::~( ")
//.replace(/\,/ig,",~::~"+tab+tab+"")
.replace(/ ALL /ig," ALL ")
.replace(/ AS /ig," AS ")
.replace(/ ASC /ig," ASC ")
.replace(/ DESC /ig," DESC ")
.replace(/ DISTINCT /ig," DISTINCT ")
.replace(/ EXISTS /ig," EXISTS ")
.replace(/ NOT /ig," NOT ")
.replace(/ NULL /ig," NULL ")
.replace(/ LIKE /ig," LIKE ")
.replace(/\s{0,}SELECT /ig,"SELECT ")
.replace(/~::~{1,}/g,"~::~")
.split('~::~');
}
pp.prototype.sql = function(text) {
var ar_by_quote = text.replace(/\s{1,}/g," ")
.replace(/\'/ig,"~::~\'")
.split('~::~'),
len = ar_by_quote.length,
ar = [],
deep = 0,
tab = this.step,//+this.step,
inComment = true,
inQuote = false,
parenthesisLevel = 0,
str = '',
ix = 0;
for(ix=0;ix<len;ix++) {
if(ix%2) {
ar = ar.concat(ar_by_quote[ix]);
} else {
ar = ar.concat(split_sql(ar_by_quote[ix], tab) );
}
}
len = ar.length;
for(ix=0;ix<len;ix++) {
parenthesisLevel = isSubquery(ar[ix], parenthesisLevel);
if( /\s{0,}\s{0,}SELECT\s{0,}/.exec(ar[ix])) {
ar[ix] = ar[ix].replace(/\,/g,",\n"+tab+tab+"")
}
if( /\s{0,}\(\s{0,}SELECT\s{0,}/.exec(ar[ix])) {
deep++;
str += this.shift[deep]+ar[ix];
} else
if( /\'/.exec(ar[ix]) ) {
if(parenthesisLevel<1 && deep) {
deep--;
}
str += ar[ix];
}
else {
str += this.shift[deep]+ar[ix];
if(parenthesisLevel<1 && deep) {
deep--;
}
}
}
str = str.replace(/^\n{1,}/,'').replace(/\n{1,}/g,"\n");
return str;
}
// ----------------------- min section ----------------------------------------------------
pp.prototype.xmlmin = function(text, preserveComments) {

@@ -229,3 +350,9 @@

}
pp.prototype.sqlmin = function(text) {
return text.replace(/\s{1,}/g," ").replace(/\s{1,}\(/,"(").replace(/\s{1,}\)/,")");
}
// --------------------------------------------------------------------------------------------
exports.pd= new pp;

@@ -232,0 +359,0 @@

# pretty-data
nodejs plugin to **pretty-print** or **minify**
text in **XML**, **JSON** and **CSS** formats.
text in **XML**, **JSON**, **CSS** and **SQL** formats.
**Version** - 0.20.2
**Version** - 0.30.0

@@ -27,2 +27,4 @@ **Copyright** (c) 2012 Vadim Kiryukhin ( vkiryukhin @ gmail.com )

* `pd.sql(data )` - pretty print SQL;
* `pd.xmlmin(data [, preserveComments]) ` - minify XML;

@@ -32,7 +34,9 @@

* `pd.cssmin(text [data [, preserveComments] )` - minify CSS text;
* `pd.cssmin(data [, preserveComments] )` - minify CSS text;
* `pd.sqlmin(data)` - minify JSON text;
**PARAMETERS:**
`@data` - String; XML, JSON or CSS text to beautify;
`@data` - String; XML, JSON, CSS or SQL text to beautify;

@@ -61,2 +65,6 @@ `@preserveComments` - Bool (optional, used in npp.minxml and npp.mincss only);

`var sql_pp = pd.sql(data);`
`var sql_min = pd.sqlmin(data);`
var css = '.headbg{margin:0 8px;display:none; }a:link,a:focus{ color:#00c }\n /* comment */ a:active{ color:red }',
pp_css = require('../pretty-data').pd.css(css),
pp_cssmin_com = require('../pretty-data').pd.cssmin(css,true),
pp_cssmin = require('../pretty-data').pd.cssmin(css);
var css = '.headbg{margin:0 8px;display:none; }a:link,a:focus{ color:#00c }\n /* comment */ a:active{ color:red }',
pp_css = require('../pretty-data').pd.css(css),
pp_cssmin_com = require('../pretty-data').pd.cssmin(css,true),
pp_cssmin = require('../pretty-data').pd.cssmin(css);

@@ -7,0 +7,0 @@ console.log('\n==============================================================================\n');

var json = '{"menu":{"id": "file","value": \n[1,2,3],\n"popup":{"menuitem":[{"value": ["one","two"],\n"onclick":"CreateNewDoc()"},{"value":"Close","onclick":"CloseDoc()"}]}}}',
json_pp = require('../pretty-data').pd.json(json);
json_min = require('../pretty-data').pd.jsonmin(json);
var json = '{"menu":{"id": "file","value": \n[1,2,3],\n"popup":{"menuitem":[{"value": ["one","two"],\n"onclick":"CreateNewDoc()"},{"value":"Close","onclick":"CloseDoc()"}]}}}',
json_pp = require('../pretty-data').pd.json(json),
json_min = require('../pretty-data').pd.jsonmin(json);

@@ -6,0 +6,0 @@ console.log('\n==============================================================================\n');

var xml = '<a> <b> <c>zz xxx zz</c>\n <!-- comment --> <d/> \n</b>\n</a>',
pp_xml = require('../pretty-data').pd.xml(xml),
pp_xmlmin_com = require('../pretty-data').pd.xmlmin(xml,true),
pp_xmlmin = require('../pretty-data').pd.xmlmin(xml);
var xml = '<a> <b> <c>zz xxx zz</c>\n <!-- comment --> <d/> \n</b>\n</a>',
pp_xml = require('../pretty-data').pd.xml(xml),
pp_xmlmin_com = require('../pretty-data').pd.xmlmin(xml,true),
pp_xmlmin = require('../pretty-data').pd.xmlmin(xml);

@@ -7,0 +7,0 @@ console.log('\n==============================================================================\n');

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc