vkiryukhin / pretty-data Goto Github PK
View Code? Open in Web Editor NEWplugin to pretty-print or minify data in XML, JSON and CSS formats
Home Page: http://www.eslinstructor.net/pretty-data/
plugin to pretty-print or minify data in XML, JSON and CSS formats
Home Page: http://www.eslinstructor.net/pretty-data/
It seems that version 0.50.0 is not published. And version 0.40.0 has a problem: ERROR Error: Uncaught (in promise): ReferenceError: maxdeep is not defined
This value
{"key": "{ "string": "that looks like json" }"}
Should print like:
{
"key": "{ \"string\": \"that looks like json\" }"
}
But looks like this instead
{
"key": "
{
\"string\": \"that looks like json\"
}"
}
Why not just use the pretty-printer that's built into the language? I don't see any advantage to the regex over the native prettifier.
JSON.stringify(JSON.parse(str), null, ' ');
If you install pretty-data via npm, it still contains the global leak that was fixed in 7131e11. pretty-data needs a version bump and a publish to npm that actually contains the fix, as it will cause any unit testing that looks for global leaks to fail.
The prettify works great until it meets self closing tag.
Seems the module treats the self closing tag as an ordinary open tag. The immediate tag following it will be unnecessarily indented.
Affected version: 0.40.1
Expected result:
<body rid="1534525716"
xmlns="http://jabber.org/protocol/httpbind" sid="2c944b0e-b4a2-4bde-ab49-18f5e6527a72">
<message to="[email protected]" type="chat"
xmlns="jabber:client">
<active
xmlns="http://jabber.org/protocol/chatstates"/>
</message>
<message from="[email protected]" to="[email protected]" type="chat"
xmlns="jabber:client" id="[email protected]:1462355916836">
<active
xmlns="http://jabber.org/protocol/chatstates"/>
<body>1</body>
<request
xmlns="urn:xmpp:receipts"/>
<request
xmlns="urn:maaii:receipts:server"/>
</message>
</body>
Actual result:
<body rid="1534525716"
xmlns="http://jabber.org/protocol/httpbind" sid="2c944b0e-b4a2-4bde-ab49-18f5e6527a72">
<message to="[email protected]" type="chat"
xmlns="jabber:client">
<active
xmlns="http://jabber.org/protocol/chatstates"/>
</message>
<message from="[email protected]" to="[email protected]" type="chat"
xmlns="jabber:client" id="[email protected]:1462355916836">
<active
xmlns="http://jabber.org/protocol/chatstates"/>
<body>1</body>
<request
xmlns="urn:xmpp:receipts"/>
<request
xmlns="urn:maaii:receipts:server"/>
</message>
</body>
See test:
it('should trim xml input', function () {
var xml = '\n<a>data</a>\n';
var result = pd.xmlmin(xml);
expect(result).toBe('<a>data</a>');
});
It doesn't pass because xmlmin doesn't trim newline characters.
It's due to the namespace attribute: if a XML file contains a namespace attribute then each iteration of a pretty-print run adds a newline \n
to the result file. I've adjusted the pretty-data/test/test_xml.js
to show the issue:
var 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>';
var pp_xml = require('../pretty-data').pd.xml(xml);
var pp2_xml = require('../pretty-data').pd.xml(pp_xml);
var pp3_xml = require('../pretty-data').pd.xml(pp2_xml);
console.log('\n==============================================================================\n');
console.log('\n/*------- Original XML string: -------*/\n\n' + xml + '\n');
console.log('\n/*------- Beautified XML -------------*/\n\n' + pp_xml + '\n');
console.log('\n/*------- Beautified twice XML -------------*/\n\n' + pp2_xml + '\n');
console.log('\n/*------- Beautified 3 times XML -------------*/\n\n' + pp3_xml + '\n');
console.log('\n===============================================================================\n');
Result:
/*------- Beautified 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>
/*------- Beautified twice 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>
/*------- Beautified 3 times 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>
I expect that the 2nd and 3rd application of the pretty-print to this XML does not change the XML anymore. One application should be sufficient.
I've found this issue in version 0.40.0.
The current JSON beautifier implementation relies on special strings used in regular expressions.
This makes it not generic as shown by the following test case.
Test case:
{ "a~#~{~#~b": "a~#~{~#~b" }
Currently broken output:
{
"a~#~
{
~#~b":"a~#~
{
~#~b"
}
If you use characters like square brackets ([]
) or curly braces ({}
) within a string, the surrounding whitespaces will be trimmed even tho strings shouldn't be altered.
e.g.
const s = '{ "foo": " {{ bar }} " }'
pd.jsonmin(s)
// '{"foo":"{{bar}}"}'
The following formats incorrectly:
var prettyData = require("pretty-data")
console.log(prettyData.pd.xml("<foo><foo/></foo>"));
<foo>
<foo/></foo>
That should be:
<foo>
<foo/>
</foo>
Hi there!
I've played around with gulp-pretty-data which wraps this library and I found an issue when minifying json files when a string contains brackets like this:
{
"welcome": "text before {{variable}} text after"
}
Is converted to:
{"welcome":"text before{{variable}}text after"}
Instead of (note spaces between {{variable}}
):
{"welcome":"text before {{variable}} text after"}
Is there a way to fix this? I've created an issue on gulp plugin, but I agree with author that the issue should be created here.
Thank you!
For some reason the following code will lead to extra new line:
var prettyData = require("pretty-data")
console.log(prettyData.pd.xml("<foo><![CDATA[X\n<? <]]></foo>"));
This should format as
<foo>
<![CDATA[X
<? <]]>
</foo>
but instead formats as
<foo>
<![CDATA[X
<? <]]>
</foo>
Changes required:
As you can see maxdeep and ix need to be setup a little better. No big deal.
function pp() {
this.shift = ['\n']; // array of shifts
this.step = ' ', // 2 spaces
this.maxdeep = 100; // nesting level
var ix = 0;
// initialize array with shifts //
for(ix=0;ix<this.maxdeep;ix++){
this.shift.push(this.shift[ix]+this.step);
}
};
Hello,
I'm currently using your library which is great! ๐
Unfortunately, the latest published version on npmjs is "0.40.0" and does not contain the fixes you made in commits 7131e11f701fc28bbea6ee093cdaaa06b1434890 and b309cd45841e7d3cbe0babcf16752da53cffe7b4
Could you please tell me if the library still being maintained ? ๐
First off, a major THANKS for your module. It is super useful!
<StandardBusinessDocument
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
Running this through:
const formattedXml = pd.xml(xmlOut);
console.log(formattedXml);
Gives:
<StandardBusinessDocument
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
This might not be against XML specifications per se but it is not very "pretty"....
pretty-data currently does not seem to strip whitespace within XML Elements.
Example:
<Something xmlns="a.b.c"
xmlns:a="a.b.c.d"
xmlns:b="a.b.c.d.e.f.g">
</Something>
ends up as:
<Something xmlns="a.b.c"
xmlns:a="a.b.c.d"
xmlns:b="a.b.c.d.e.f.g"></Something>
instead of
<Something xmlns="a.b.c" xmlns:a="a.b.c.d" xmlns:b="a.b.c.d.e.f.g"></Something>
I can't see any valid restriction on this. Should be savely possible.
BR
Chris
Could you provide a CLI binary for your package, that would be neat.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.