I have been looking around the web for a good Sublime (3) package to use to auto-format my source code in various languages, namely JavaScript. I came across this SOF post (Sublime Text 2: Auto fix indentation for javascript?) and thus decided to give JSFormat a try. So far, it seems to work pretty good...except for when it handles JSON objects in the JS code. For example, let's say that I have a function like this:
function foo() {
return {name: 'Dave', score: 1000};
}
It returns a JavaScript object in JSON format, prettu much a hash object. I like writing such objects in one line because it is simple and easy to read, especially since it is just a small, ad-hoc object. But, if I were to format this with JSFormat, my function would now look like this:
function foo() {
return {
name: 'Dave',
score: 1000
};
}
Maybe this is just me, but I really don't like representing such simple JSON objects in multiple lines. Yes, normally JavaScript code that requires braces should have its contents on a separate lines from the braces, such as functions, if statements and loops. Maybe if the JSON object was a long object that contained functions inside of it, such as a jQuery Ajax class, then it makes sense to separate the attributes onto multiple lines.
Nonetheless, regardless whether my points about the braces makes sense, I know that JSFormat supports configuration and perhaps there is a way to configure JSFormat to not separate the attributes of a JSON object into multiple lines, if it is not desired. Any ideas?
Syntax of Sublime Pretty JSONSublime Text is an easy option to Pretty format JSON, also we can prettyify JSON in our browser. Based on OS, installation steps might change a bit. If MAC OS, press CMD+SHIFT+P, then select 'Install Package'. Search for 'Pretty JSON' and install.
1 Answer. Show activity on this post. Using Command Palette Ctrl+Shift+P find "Pretty JSON: Minify (compress) JSON" (you can search for part of it like 'json minify') this will make selection or full buffer as single line JSON which later you can use in command lines (curl/httpie) or somewhere else...
JSFormat
uses js-beautify, which does not support single-line function definitions. Everything is broken into "beautified" lines, making it "more readable."
Look at the example given for js-beautify... the example itself is of a single-line function definition. There is no way to distinguish single-line function definitions you do want preserved from those you don't.
If you think about it, the ideal situation to use a beautifier is if you want to take minified code and make it readable... That's just one long line of code too.
I feel your pain, believe me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With