sweet-data
Advanced tools
Comparing version 0.0.9 to 0.1.0
function json(text) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
if (typeof text === "string") { | ||
return resolve(JSON.stringify(JSON.parse(text), null, this.step)); | ||
} | ||
return new Promise((resolve, reject) => { | ||
try { | ||
if (typeof text === "string") { | ||
resolve(JSON.stringify(JSON.parse(text), null, this.step)); | ||
} | ||
if (typeof text === "object") { | ||
return resolve(JSON.stringify(text, null, this.step)); | ||
} | ||
if (typeof text === "object") { | ||
resolve(JSON.stringify(text, null, this.step)); | ||
} | ||
throw new TypeError('Input must of type "object" or "string"'); | ||
} catch (e) { | ||
return reject(e); | ||
} | ||
}); | ||
throw new TypeError('Input must of type "object" or "string"'); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
@@ -22,29 +22,29 @@ | ||
return new Promise((resolve, reject) => { | ||
try { | ||
return resolve( | ||
text | ||
.toString() | ||
.replace(/\s{0,}\{\s{0,}/g, "{") | ||
.replace(/\s{0,}\[$/g, "[") | ||
.replace(/\[\s{0,}/g, "[") | ||
.replace(/:\s{0,}\[/g, ':[') | ||
.replace(/\s{0,}\}\s{0,}/g, "}") | ||
.replace(/\s{0,}\]\s{0,}/g, "]") | ||
.replace(/\"\s{0,}\,/g, '",') | ||
.replace(/\,\s{0,}\"/g, ',"') | ||
.replace(/\"\s{0,}:/g, '":') | ||
.replace(/:\s{0,}\"/g, ':"') | ||
.replace(/:\s{0,}\[/g, ':[') | ||
.replace(/\,\s{0,}\[/g, ',[') | ||
.replace(/\,\s{2,}/g, ', ') | ||
.replace(/\]\s{0,},\s{0,}\[/g, '],[') | ||
); | ||
} catch (e) { | ||
return reject(e); | ||
} | ||
}); | ||
return new Promise((resolve, reject) => { | ||
try { | ||
resolve( | ||
text | ||
.toString() | ||
.replace(/\s{0,}\{\s{0,}/g, "{") | ||
.replace(/\s{0,}\[$/g, "[") | ||
.replace(/\[\s{0,}/g, "[") | ||
.replace(/:\s{0,}\[/g, ':[') | ||
.replace(/\s{0,}\}\s{0,}/g, "}") | ||
.replace(/\s{0,}\]\s{0,}/g, "]") | ||
.replace(/\"\s{0,}\,/g, '",') | ||
.replace(/\,\s{0,}\"/g, ',"') | ||
.replace(/\"\s{0,}:/g, '":') | ||
.replace(/:\s{0,}\"/g, ':"') | ||
.replace(/:\s{0,}\[/g, ':[') | ||
.replace(/\,\s{0,}\[/g, ',[') | ||
.replace(/\,\s{2,}/g, ', ') | ||
.replace(/\]\s{0,},\s{0,}\[/g, '],[') | ||
); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
exports.json = json; | ||
exports.jsonmin = jsonmin; | ||
exports.jsonmin = jsonmin; |
function xml(text) { | ||
var ar = text.replace(/>\s{0,}</g,"><") | ||
.replace(/</g,"~::~<") | ||
.replace(/xmlns\:/g,"~::~xmlns:") | ||
.replace(/xmlns\=/g,"~::~xmlns=") | ||
.split('~::~'), | ||
len = ar.length, | ||
inComment = false, | ||
deep = 0, | ||
str = '', | ||
ix = 0; | ||
return new Promise((resolve, reject) => { | ||
try { | ||
let ar = text.replace(/>\s{0,}</g,"><") | ||
.replace(/</g,"~::~<") | ||
.replace(/xmlns\:/g,"~::~xmlns:") | ||
.replace(/xmlns\=/g,"~::~xmlns=") | ||
.split('~::~'), | ||
len = ar.length, | ||
inComment = false, | ||
deep = 0, | ||
str = '', | ||
ix = 0; | ||
for(ix=0;ix<len;ix++) { | ||
// start comment or <![CDATA[...]]> or <!DOCTYPE // | ||
if(ar[ix].search(/<!/) > -1) { | ||
str += this.shift[deep]+ar[ix]; | ||
inComment = true; | ||
// end comment or <![CDATA[...]]> // | ||
if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1 || ar[ix].search(/!DOCTYPE/) > -1 ) { | ||
inComment = false; | ||
} | ||
} else | ||
// end comment or <![CDATA[...]]> // | ||
if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1) { | ||
str += ar[ix]; | ||
inComment = false; | ||
} else | ||
// <elm></elm> // | ||
if( /^<\w/.exec(ar[ix-1]) && /^<\/\w/.exec(ar[ix]) && | ||
/^<[\w:\-\.\,]+/.exec(ar[ix-1]) == /^<\/[\w:\-\.\,]+/.exec(ar[ix])[0].replace('/','')) { | ||
str += ar[ix]; | ||
if(!inComment) deep--; | ||
} else | ||
// <elm> // | ||
if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) == -1 && ar[ix].search(/\/>/) == -1 ) { | ||
str = !inComment ? str += this.shift[deep++]+ar[ix] : str += ar[ix]; | ||
} else | ||
// <elm>...</elm> // | ||
if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) > -1) { | ||
str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix]; | ||
} else | ||
// </elm> // | ||
if(ar[ix].search(/<\//) > -1) { | ||
str = !inComment ? str += this.shift[--deep]+ar[ix] : str += ar[ix]; | ||
} else | ||
// <elm/> // | ||
if(ar[ix].search(/\/>/) > -1 ) { | ||
str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix]; | ||
} else | ||
// <? xml ... ?> // | ||
if(ar[ix].search(/<\?/) > -1) { | ||
str += this.shift[deep]+ar[ix]; | ||
} else | ||
// xmlns // | ||
if( ar[ix].search(/xmlns\:/) > -1 || ar[ix].search(/xmlns\=/) > -1) { | ||
str += this.shift[deep]+ar[ix]; | ||
} | ||
else { | ||
str += ar[ix]; | ||
} | ||
} | ||
return (str[0] == '\n') ? str.slice(1) : str; | ||
for(ix=0;ix<len;ix++) { | ||
// start comment or <![CDATA[...]]> or <!DOCTYPE // | ||
if(ar[ix].search(/<!/) > -1) { | ||
str += this.shift[deep]+ar[ix]; | ||
inComment = true; | ||
// end comment or <![CDATA[...]]> // | ||
if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1 || ar[ix].search(/!DOCTYPE/) > -1 ) { | ||
inComment = false; | ||
} | ||
} else | ||
// end comment or <![CDATA[...]]> // | ||
if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1) { | ||
str += ar[ix]; | ||
inComment = false; | ||
} else | ||
// <elm></elm> // | ||
if( /^<\w/.exec(ar[ix-1]) && /^<\/\w/.exec(ar[ix]) && | ||
/^<[\w:\-\.\,]+/.exec(ar[ix-1]) == /^<\/[\w:\-\.\,]+/.exec(ar[ix])[0].replace('/','')) { | ||
str += ar[ix]; | ||
if(!inComment) deep--; | ||
} else | ||
// <elm> // | ||
if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) == -1 && ar[ix].search(/\/>/) == -1 ) { | ||
str = !inComment ? str += this.shift[deep++]+ar[ix] : str += ar[ix]; | ||
} else | ||
// <elm>...</elm> // | ||
if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) > -1) { | ||
str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix]; | ||
} else | ||
// </elm> // | ||
if(ar[ix].search(/<\//) > -1) { | ||
str = !inComment ? str += this.shift[--deep]+ar[ix] : str += ar[ix]; | ||
} else | ||
// <elm/> // | ||
if(ar[ix].search(/\/>/) > -1 ) { | ||
str = !inComment ? str += this.shift[deep]+ar[ix] : str += ar[ix]; | ||
} else | ||
// <? xml ... ?> // | ||
if(ar[ix].search(/<\?/) > -1) { | ||
str += this.shift[deep]+ar[ix]; | ||
} else | ||
// xmlns // | ||
if( ar[ix].search(/xmlns\:/) > -1 || ar[ix].search(/xmlns\=/) > -1) { | ||
str += this.shift[deep]+ar[ix]; | ||
} | ||
else { | ||
str += ar[ix]; | ||
} | ||
} | ||
resolve((str[0] == '\n') ? str.slice(1) : str); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
@@ -70,8 +76,15 @@ | ||
var str = preserveComments ? text | ||
: text.replace(/\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>/g,""); | ||
return str.replace(/>\s{0,}</g,"><"); | ||
return new Promise((resolve, reject) => { | ||
try { | ||
let str = preserveComments ? text | ||
: text.replace(/\<![ \r\n\t]*(--([^\-]|[\r\n]|-[^\-])*--[ \r\n\t]*)\>/g,""); | ||
resolve(str.replace(/>\s{0,}</g,"><")); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
} | ||
exports.xml = xml; | ||
exports.xmlmin = xmlmin; | ||
exports.xmlmin = xmlmin; |
{ | ||
"name": "sweet-data", | ||
"version": "0.0.9", | ||
"version": "0.1.0", | ||
"author": "Brian Gerhards (https://github.com/bgerhards)", | ||
@@ -5,0 +5,0 @@ "description": "plugin to pretty-print or minify XML, JSON, CSS and SQL files", |
@@ -42,3 +42,3 @@ # sweet-data - Nodejs plugin | ||
`@Return` - String; | ||
`@Return` - String | Promise(String); | ||
@@ -58,2 +58,6 @@ `@indentationCount` - Numeric (optional, used to set number of indentation (per level)) | ||
[XML](#xml) | ||
[XML Minification](#xml-minification) | ||
`var sd = require('sweet-data'); ` | ||
@@ -76,5 +80,5 @@ | ||
#### json() | ||
#### json(json: string | object) | ||
Returns a promise which is resolved asynchronously when the json input is parsed and beautified. The promise resolves a string accessed via `.then()`. Errors can be caught/handled via `.catch()`. | ||
Returns a promise which is resolved asynchronously when the json input (string or object) is parsed and beautified. The promise resolves a string accessed via `.then()`. Errors can be caught/handled via `.catch()`. | ||
@@ -89,3 +93,3 @@ **ES6** | ||
sd.json(json) | ||
.then(parsedJson => console.log(parsedJson)) | ||
.then(beautifiedJSON => console.log(beautifiedJSON)) | ||
.catch(error => console.log(error)); | ||
@@ -108,5 +112,5 @@ ``` | ||
#### jsonmin() | ||
#### jsonmin(json: string | object) | ||
Returns a promise which is resolved asynchronously when the json input is parsed and minified. The promise resolves a string accessed via `.then()`. Errors can be caught/handled via `.catch()`. | ||
Returns a promise which is resolved asynchronously when the json input (string or object) is parsed and minified. The promise resolves a string accessed via `.then()`. Errors can be caught/handled via `.catch()`. | ||
@@ -133,6 +137,67 @@ **ES6** | ||
sd.jsonmin(json) | ||
.then(parsedJson => console.log(parsedJson)) | ||
.then(minifiedJSON => console.log(minifiedJSON)) | ||
.catch(error => console.log(error)); | ||
``` | ||
### XML | ||
#### xml(xml: string) | ||
Returns a promise which is resolved asynchronously when the xml string input is beautified. The promise resolves a string accessed via `.then()`. Errors can be caught/handled via `.catch()`. | ||
**ES6** | ||
``` | ||
import { SweetData as sd } from 'sweet-data'; | ||
const xml = '<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE foo SYSTEM "Foo.dtd"><a> <b>bbb</b> <!-- comment --><c/><d><soapenv:Envelope xmlns:soapenv="http://xxx" xmlns:xsd="http://yyy" xmlns:xsi="http://zzz"></soapenv> </d><e> <![CDATA[ <z></z> ]]></e><f><g></g></f></a>'; | ||
sd.xml(xml) | ||
.then(beautifiedXML => console.log(beautifiedXML)) | ||
.catch(error => console.log(error)); | ||
``` | ||
**CommonJS** | ||
``` | ||
const sd = require('sweet-data'); | ||
const xml = '<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE foo SYSTEM "Foo.dtd"><a> <b>bbb</b> <!-- comment --><c/><d><soapenv:Envelope xmlns:soapenv="http://xxx" xmlns:xsd="http://yyy" xmlns:xsi="http://zzz"></soapenv> </d><e> <![CDATA[ <z></z> ]]></e><f><g></g></f></a>'; | ||
sd.xml(xml) | ||
.then(beautifiedXML => console.log(beautifiedXML)) | ||
.catch(error => console.log(error)); | ||
``` | ||
### XML Minification | ||
#### xmlmin(xml: string) | ||
Returns a promise which is resolved asynchronously when the xml string input is minified. The promise resolves a string accessed via `.then()`. Errors can be caught/handled via `.catch()`. | ||
**ES6** | ||
``` | ||
import { SweetData as sd } from 'sweet-data'; | ||
const xml = '<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE foo SYSTEM "Foo.dtd"><a> <b>bbb</b> <!-- comment --><c/><d><soapenv:Envelope xmlns:soapenv="http://xxx" xmlns:xsd="http://yyy" xmlns:xsi="http://zzz"></soapenv> </d><e> <![CDATA[ <z></z> ]]></e><f><g></g></f></a>'; | ||
sd.xmlmin(xml) | ||
.then(minifiedXML => console.log(minifiedXML)) | ||
.catch(error => console.log(error)); | ||
``` | ||
**CommonJS** | ||
``` | ||
const sd = require('sweet-data'); | ||
const xml = '<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE foo SYSTEM "Foo.dtd"><a> <b>bbb</b> <!-- comment --><c/><d><soapenv:Envelope xmlns:soapenv="http://xxx" xmlns:xsd="http://yyy" xmlns:xsi="http://zzz"></soapenv> </d><e> <![CDATA[ <z></z> ]]></e><f><g></g></f></a>'; | ||
sd.xmlmin(xml) | ||
.then(minifiedXML => console.log(minifiedXML)) | ||
.catch(error => console.log(error)); | ||
``` | ||
### Tests | ||
@@ -139,0 +204,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17365
303
212