PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/pretty-data/pretty-data.js

https://gitlab.com/raphaelmachiels/partie_pratique_raphael_machiels
JavaScript | 344 lines | 224 code | 52 blank | 68 comment | 51 complexity | 34b6de5f0647e3bcdd74f07f23008260 MD5 | raw file
  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. function pp() {
  50. this.shift = ['\n']; // array of shifts
  51. this.step = ' ', // 2 spaces
  52. maxdeep = 100, // nesting level
  53. ix = 0;
  54. // initialize array with shifts //
  55. for(ix=0;ix<maxdeep;ix++){
  56. this.shift.push(this.shift[ix]+this.step);
  57. }
  58. };
  59. // ----------------------- XML section ----------------------------------------------------
  60. pp.prototype.xml = function(text) {
  61. var ar = text.replace(/>\s{0,}</g,"><")
  62. .replace(/</g,"~::~<")
  63. .replace(/xmlns\:/g,"~::~xmlns:")
  64. .replace(/xmlns\=/g,"~::~xmlns=")
  65. .split('~::~'),
  66. len = ar.length,
  67. inComment = false,
  68. deep = 0,
  69. str = '',
  70. ix = 0;
  71. for(ix=0;ix<len;ix++) {
  72. // start comment or <![CDATA[...]]> or <!DOCTYPE //
  73. if(ar[ix].search(/<!/) > -1) {
  74. str += this.shift[deep]+ar[ix];
  75. inComment = true;
  76. // end comment or <![CDATA[...]]> //
  77. if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1 || ar[ix].search(/!DOCTYPE/) > -1 ) {
  78. inComment = false;
  79. }
  80. } else
  81. // end comment or <![CDATA[...]]> //
  82. if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1) {
  83. str += ar[ix];
  84. inComment = false;
  85. } else
  86. // <elm></elm> //
  87. if( /^<\w/.exec(ar[ix-1]) && /^<\/\w/.exec(ar[ix]) &&
  88. /^<[\w:\-\.\,]+/.exec(ar[ix-1]) == /^<\/[\w:\-\.\,]+/.exec(ar[ix])[0].replace('/','')) {
  89. str += ar[ix];
  90. if(!inComment) deep--;
  91. } else
  92. // <elm> //
  93. if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) == -1 && ar[ix].search(/\/>/) == -1 ) {
  94. str = !inComment ? str += this.shift[deep++]+ar[ix] : str += ar[ix];
  95. } else
  96. // <elm>...</elm> //
  97. if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) > -1) {
  98. str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix];
  99. } else
  100. // </elm> //
  101. if(ar[ix].search(/<\//) > -1) {
  102. str = !inComment ? str += this.shift[--deep]+ar[ix] : str += ar[ix];
  103. } else
  104. // <elm/> //
  105. if(ar[ix].search(/\/>/) > -1 ) {
  106. str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix];
  107. } else
  108. // <? xml ... ?> //
  109. if(ar[ix].search(/<\?/) > -1) {
  110. str += this.shift[deep]+ar[ix];
  111. } else
  112. // xmlns //
  113. if( ar[ix].search(/xmlns\:/) > -1 || ar[ix].search(/xmlns\=/) > -1) {
  114. str += this.shift[deep]+ar[ix];
  115. }
  116. else {
  117. str += ar[ix];
  118. }
  119. }
  120. return (str[0] == '\n') ? str.slice(1) : str;
  121. }
  122. // ----------------------- JSON section ----------------------------------------------------
  123. pp.prototype.json = function(text) {
  124. if ( typeof text === "string" ) {
  125. return JSON.stringify(JSON.parse(text), null, this.step);
  126. }
  127. if ( typeof text === "object" ) {
  128. return JSON.stringify(text, null, this.step);
  129. }
  130. return null;
  131. }
  132. // ----------------------- CSS section ----------------------------------------------------
  133. pp.prototype.css = function(text) {
  134. var ar = text.replace(/\s{1,}/g,' ')
  135. .replace(/\{/g,"{~::~")
  136. .replace(/\}/g,"~::~}~::~")
  137. .replace(/\;/g,";~::~")
  138. .replace(/\/\*/g,"~::~/*")
  139. .replace(/\*\//g,"*/~::~")
  140. .replace(/~::~\s{0,}~::~/g,"~::~")
  141. .split('~::~'),
  142. len = ar.length,
  143. deep = 0,
  144. str = '',
  145. ix = 0;
  146. for(ix=0;ix<len;ix++) {
  147. if( /\{/.exec(ar[ix])) {
  148. str += this.shift[deep++]+ar[ix];
  149. } else
  150. if( /\}/.exec(ar[ix])) {
  151. str += this.shift[--deep]+ar[ix];
  152. } else
  153. if( /\*\\/.exec(ar[ix])) {
  154. str += this.shift[deep]+ar[ix];
  155. }
  156. else {
  157. str += this.shift[deep]+ar[ix];
  158. }
  159. }
  160. return str.replace(/^\n{1,}/,'');
  161. }
  162. // ----------------------- SQL section ----------------------------------------------------
  163. function isSubquery(str, parenthesisLevel) {
  164. return parenthesisLevel - (str.replace(/\(/g,'').length - str.replace(/\)/g,'').length )
  165. }
  166. function split_sql(str, tab) {
  167. return str.replace(/\s{1,}/g," ")
  168. .replace(/ AND /ig,"~::~"+tab+tab+"AND ")
  169. .replace(/ BETWEEN /ig,"~::~"+tab+"BETWEEN ")
  170. .replace(/ CASE /ig,"~::~"+tab+"CASE ")
  171. .replace(/ ELSE /ig,"~::~"+tab+"ELSE ")
  172. .replace(/ END /ig,"~::~"+tab+"END ")
  173. .replace(/ FROM /ig,"~::~FROM ")
  174. .replace(/ GROUP\s{1,}BY/ig,"~::~GROUP BY ")
  175. .replace(/ HAVING /ig,"~::~HAVING ")
  176. //.replace(/ IN /ig,"~::~"+tab+"IN ")
  177. .replace(/ IN /ig," IN ")
  178. .replace(/ JOIN /ig,"~::~JOIN ")
  179. .replace(/ CROSS~::~{1,}JOIN /ig,"~::~CROSS JOIN ")
  180. .replace(/ INNER~::~{1,}JOIN /ig,"~::~INNER JOIN ")
  181. .replace(/ LEFT~::~{1,}JOIN /ig,"~::~LEFT JOIN ")
  182. .replace(/ RIGHT~::~{1,}JOIN /ig,"~::~RIGHT JOIN ")
  183. .replace(/ ON /ig,"~::~"+tab+"ON ")
  184. .replace(/ OR /ig,"~::~"+tab+tab+"OR ")
  185. .replace(/ ORDER\s{1,}BY/ig,"~::~ORDER BY ")
  186. .replace(/ OVER /ig,"~::~"+tab+"OVER ")
  187. .replace(/\(\s{0,}SELECT /ig,"~::~(SELECT ")
  188. .replace(/\)\s{0,}SELECT /ig,")~::~SELECT ")
  189. .replace(/ THEN /ig," THEN~::~"+tab+"")
  190. .replace(/ UNION /ig,"~::~UNION~::~")
  191. .replace(/ USING /ig,"~::~USING ")
  192. .replace(/ WHEN /ig,"~::~"+tab+"WHEN ")
  193. .replace(/ WHERE /ig,"~::~WHERE ")
  194. .replace(/ WITH /ig,"~::~WITH ")
  195. //.replace(/\,\s{0,}\(/ig,",~::~( ")
  196. //.replace(/\,/ig,",~::~"+tab+tab+"")
  197. .replace(/ ALL /ig," ALL ")
  198. .replace(/ AS /ig," AS ")
  199. .replace(/ ASC /ig," ASC ")
  200. .replace(/ DESC /ig," DESC ")
  201. .replace(/ DISTINCT /ig," DISTINCT ")
  202. .replace(/ EXISTS /ig," EXISTS ")
  203. .replace(/ NOT /ig," NOT ")
  204. .replace(/ NULL /ig," NULL ")
  205. .replace(/ LIKE /ig," LIKE ")
  206. .replace(/\s{0,}SELECT /ig,"SELECT ")
  207. .replace(/~::~{1,}/g,"~::~")
  208. .split('~::~');
  209. }
  210. pp.prototype.sql = function(text) {
  211. var ar_by_quote = text.replace(/\s{1,}/g," ")
  212. .replace(/\'/ig,"~::~\'")
  213. .split('~::~'),
  214. len = ar_by_quote.length,
  215. ar = [],
  216. deep = 0,
  217. tab = this.step,//+this.step,
  218. inComment = true,
  219. inQuote = false,
  220. parenthesisLevel = 0,
  221. str = '',
  222. ix = 0;
  223. for(ix=0;ix<len;ix++) {
  224. if(ix%2) {
  225. ar = ar.concat(ar_by_quote[ix]);
  226. } else {
  227. ar = ar.concat(split_sql(ar_by_quote[ix], tab) );
  228. }
  229. }
  230. len = ar.length;
  231. for(ix=0;ix<len;ix++) {
  232. parenthesisLevel = isSubquery(ar[ix], parenthesisLevel);
  233. if( /\s{0,}\s{0,}SELECT\s{0,}/.exec(ar[ix])) {
  234. ar[ix] = ar[ix].replace(/\,/g,",\n"+tab+tab+"")
  235. }
  236. if( /\s{0,}\(\s{0,}SELECT\s{0,}/.exec(ar[ix])) {
  237. deep++;
  238. str += this.shift[deep]+ar[ix];
  239. } else
  240. if( /\'/.exec(ar[ix]) ) {
  241. if(parenthesisLevel<1 && deep) {
  242. deep--;
  243. }
  244. str += ar[ix];
  245. }
  246. else {
  247. str += this.shift[deep]+ar[ix];
  248. if(parenthesisLevel<1 && deep) {
  249. deep--;
  250. }
  251. }
  252. }
  253. str = str.replace(/^\n{1,}/,'').replace(/\n{1,}/g,"\n");
  254. return str;
  255. }
  256. // ----------------------- min section ----------------------------------------------------
  257. pp.prototype.xmlmin = function(text, preserveComments) {
  258. var str = preserveComments ? text
  259. : text.replace(/\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>/g,"");
  260. return str.replace(/>\s{0,}</g,"><");
  261. }
  262. pp.prototype.jsonmin = function(text) {
  263. return text.replace(/\s{0,}\{\s{0,}/g,"{")
  264. .replace(/\s{0,}\[$/g,"[")
  265. .replace(/\[\s{0,}/g,"[")
  266. .replace(/:\s{0,}\[/g,':[')
  267. .replace(/\s{0,}\}\s{0,}/g,"}")
  268. .replace(/\s{0,}\]\s{0,}/g,"]")
  269. .replace(/\"\s{0,}\,/g,'",')
  270. .replace(/\,\s{0,}\"/g,',"')
  271. .replace(/\"\s{0,}:/g,'":')
  272. .replace(/:\s{0,}\"/g,':"')
  273. .replace(/:\s{0,}\[/g,':[')
  274. .replace(/\,\s{0,}\[/g,',[')
  275. .replace(/\,\s{2,}/g,', ')
  276. .replace(/\]\s{0,},\s{0,}\[/g,'],[');
  277. }
  278. pp.prototype.cssmin = function(text, preserveComments) {
  279. var str = preserveComments ? text
  280. : text.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\//g,"") ;
  281. return str.replace(/\s{1,}/g,' ')
  282. .replace(/\{\s{1,}/g,"{")
  283. .replace(/\}\s{1,}/g,"}")
  284. .replace(/\;\s{1,}/g,";")
  285. .replace(/\/\*\s{1,}/g,"/*")
  286. .replace(/\*\/\s{1,}/g,"*/");
  287. }
  288. pp.prototype.sqlmin = function(text) {
  289. return text.replace(/\s{1,}/g," ").replace(/\s{1,}\(/,"(").replace(/\s{1,}\)/,")");
  290. }
  291. // --------------------------------------------------------------------------------------------
  292. exports.pd= new pp;