js-beautify
Beautifier for javascript
Online JavaScript beautifier
This line is breaked by js-beautify into multiple lines. Is it possible to prevent that? (using JsFormat Sublime Text plugin, or command-line)
I tried all options from http://jsbeautifier.org/
if(err) return new ApiError({ innerError: err }).respond(this.res);
becomes
if (err) return new ApiError({
innerError: err
}).respond(this.res);
Source: (StackOverflow)
I would like to use HTML Beautify against an Angular partial to allow me to present to my client a code snippet of the partial.
The issue I'm running into is that when I beautify the partial to ensure formatting is correct, my directives, that have no arguments are being changed to have a blank ="".
For example:
<div my-directive></div>
Is beautified into
<div my-directive=""></div>
Is there an option I can send to HTML Beautify that will not force the addition of ="" to my directive?
Source: (StackOverflow)
I'm looking for a tool that unminify a JS file, while restoring the variable names in the whole document.
for example, after running the minified code at unminify.com, the code itself contains:
W.find("li.patternItem").click(function() {
and at the end of the code there is a reference what is W:
W = $("#pattern1")
So I am looking for a simple script to restore the name inside the code, but can't find one. I understand the auto replace might be harmful for the code, but I guess that there should be some smart way to restore. Right now I'm doing it manually and replacing "W." with "$("#pattern1")." (match case, with dot at the end) so it won't be mixed with any W letter.
Thanks
Source: (StackOverflow)
I have the code:
var foo = 1, bar = 2
when use jsFormat, it becomes to: (I use 2 whitespaces to indent)
var foo = 1,
bar = 2
I want it to be:
var foo = 1,
bar = 2
Could I get what I want by setting jsFormat, and how?
Thanks a lot.
Source: (StackOverflow)
I want to format some json files from Java code, and I want to use the famous http://jsbeautifier.org library.
I followed this question: Call external javascript functions from java code , but can't find a correct function to call.
I tried inv.invokeFunction("js_beautify", json)
, but it reports:
java.lang.NoSuchMethodException: no such method: js_beautify
My code (actually it's Scala, but just using Java API):
val manager = new ScriptEngineManager()
val engine = manager.getEngineByName("JavaScript")
// read script file
engine.eval(FileUtils.readFileToString(new File("beautify.js")))
val inv = engine.asInstanceOf[Invocable]
// call function from script file
inv.invokeFunction("js_beautify", json).asInstanceOf[String]
And the structure of file beautify.js
is:
(function() {
// a lot of js code
// ...
if (typeof define === "function" && define.amd) {
// Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- )
define([], function() {
return { js_beautify: js_beautify };
});
} else if (typeof exports !== "undefined") {
// Add support for CommonJS. Just put this file somewhere on your require.paths
// and you will be able to `var js_beautify = require("beautify").js_beautify`.
exports.js_beautify = js_beautify;
} else if (typeof window !== "undefined") {
// If we're running a web page and don't have either of the above, add our one global
window.js_beautify = js_beautify;
} else if (typeof global !== "undefined") {
// If we don't even have window, try global.
global.js_beautify = js_beautify;
}
}());
Maybe I need to provide some global
or window
context from Java?
Update:
I followed the "JSBeautify NetBeans plugin" from @tiblu's answer:
class FormatJson {
def apply(json: String): String = {
val context = Context.enter()
context.setLanguageVersion(Context.VERSION_1_6)
val scope = context.initStandardObjects()
val reader = new FileReader(new File("beautify.js"))
context.evaluateReader(scope, reader, "Beautify", 1, null)
val fct = scope.get("js_beautify", scope).asInstanceOf[org.mozilla.javascript.Function]
fct.call(context, scope, scope, Array[AnyRef](json)).toString
}
}
But it reports:
Exception in thread "main" java.lang.ClassCastException: org.mozilla.javascript.UniqueTag cannot be cast to org.mozilla.javascript.Function
And the value of scope.get("js_beautify", scope)
actually is NOT_FOUND
.
I'm using "org.mozilla" % "rhino" % "1.7R5"
Is there anything wrong?
Source: (StackOverflow)
I'm using Sublime Text 3 with the JavaScript Beautify Package.
The code after beautifying looks like that:
var jobDimension = ndx.dimension(function(d) {
return d.status;
});
But I want that it looks like that:
var jobDimension = ndx.dimension(function(d) { return d.status; });
That are the available settings:
{
"indent_size": 4,
"indent_char": " ",
"indent_level": 0,
"indent_with_tabs": false,
"preserve_newlines": true,
"max_preserve_newlines": 10,
"jslint_happy": false,
"space_after_anon_function": false,
"brace_style": "collapse",
"keep_array_indentation": false,
"keep_function_indentation": false,
"space_before_conditional": true,
"break_chained_methods": false,
"eval_code": false,
"unescape_strings": false,
"wrap_line_length": 0
}
I tried different settings without success. Maybe you know how to achieve it with this package or an alternative.
Source: (StackOverflow)
I'm trying to use git diff to ensure that my JavaScript has been beautified before committing.
js-beautify src.js | git -c core.fileMode=false diff --no-index -- src.js -
I've followed the instructions here for ignoring file mode but I still get this output.
diff --git a/src.js b/-
old mode 100755
new mode 100644
index 13158aa..0000000
I also tried git config core.fileMode false
to no avail. If I change permissions of the file and diff against HEAD, the command works as expected (diff is empty).
chmod 644 src.js
git -c core.fileMode=false diff -- src.js
Is it possible to ignore the permissions of files that aren't indexed? I'm using git version 1.9.1.
Source: (StackOverflow)
JS noob here. I'm currently using js-beautify(https://github.com/beautify-web/js-beautify) plugin to properly indent/format a long string of HTML code. This is how I'm using it
html_beautify(HTML);
HTML is a variable containing regular HTML code.
How can I pass options like disabling word-wrap or removing empty lines?
Source: (StackOverflow)
I want to format code below (Note new lines), using jsbeautify
Which option do I use? Here is my code:
import jsbeautifier
opts = jsbeautifier.default_options()
opts.indent_size = 2
opts.preserve_newlines = False
opts.jslint_happy = True
beauty = jsbeautifier.beautify(pe.editor.GetText(), opts)
function
f(var x,
var y) {
return
x + y;
}
to something like
function f(var x, var y) {
return x + y;
}
Source: (StackOverflow)
Unable to install js-beautify, what is happening here. Not sure why it is failing here, can someone help
nvm
Node Version Manager
Usage:
nvm help Show this message
nvm install [-s] <version> Download and install a <version>
nvm uninstall <version> Uninstall a version
nvm use <version> Modify PATH to use <version>
nvm run <version> [<args>] Run <version> with <args> as arguments
nvm ls List installed versions
nvm ls <version> List versions matching a given description
nvm ls-remote List remote versions available for install
nvm deactivate Undo effects of NVM on current shell
nvm alias [<pattern>] Show all aliases beginning with <pattern>
nvm alias <name> <version> Set an alias named <name> pointing to <version>
nvm unalias <name> Deletes the alias named <name>
nvm copy-packages <version> Install global NPM packages contained in <version> to current version
Example:
nvm install v0.4.12 Install a specific version number
nvm use 0.2 Use the latest available 0.2.x release
nvm run 0.4.12 myApp.js Run myApp.js using node v0.4.12
nvm alias default 0.4 Auto use the latest installed v0.4.x version
This is how we are installing js-beautify
jeff@paris:~$ nvm install js-beautify
Warning: Failed to create the file
Warning: /home/jeff/.nvm/bin/node-N/A-linux-x64/node-N/A-linux-x64.tar.gz:
Warning: No such file or directory
######################################################################## 100.0%
curl: (23) Failed writing body (0 != 162)
Binary download failed, trying source.
Additional options while compiling:
nvm: install N/A failed!
Source: (StackOverflow)
I installed successfully js-beautify and I am using it from within xemacs on Debian Linux. By default, when formatting a string concatenated line, I get the following:
var astr = "head of string" + avarstring + ...
I cannot find how to configure the auto-formatting of a string to respect
the following indentations:
var astr = "head of string"
+ avarstring
+ ...
I do not really care about having the +
sign at the end of each line or
at the beginning, but I really need, for readability and maintenance activities,
to have each separated substring to be on a single line.
Question:
- Which option should I use to configure this behavior?
- Where should I position the configuration JSON file to load this configuration?
Source: (StackOverflow)
I understand that the Brackets Beautify extension is based on JS Beautify, and that I have a suite of preference settings available because of that, but I have no idea of how to alter them through the Brackets settings.json file. Does anyone have any idea?
Here are my current settings:
{
"debug.showErrorsInStatusBar": false,
"linting.collapsed": true,
"wordWrap": false,
"me.drewh.jsbeautify.on_save": false,
"styleActiveLine": true,
"themes.theme": "explicit-brackets-style",
"fonts.fontSize": "12px"
}
I'm assuming I just need to add new "me.drewh.jsbeautify" lines, but that doesn't seem to work...
I'm specifically looking at HTML settings, BTW.
Source: (StackOverflow)
I am trying to use js-beautify
for html
in a node.js
application:
var htmlBeautifier = require('js-beautify').html;
...
res = htmlBeautifier.beautify(html);
...
But I get:
...
res = htmlBeautifier.beautify(html,{});
^
TypeError: Object function (html_source, options) {
return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify);
} has no method 'beautify'
The documentation about using js-beautify
for html
does not provide much information. How is one supposed to use js-beautify
for html
?
Source: (StackOverflow)
I like jsbeautifier.org and I see they have a github repo with their code.
The readme has two examples of how to use this tool via the command line:
import jsbeautifier
res = jsbeautifier.beautify('your javascript string')
res = jsbeautifier.beautify_file('some_file.js')
and:
opts = jsbeautifier.default_options()
opts.indent_size = 2
res = jsbeautifier.beautify('some javascript', opts)
How can I incorporate this into a script (myjsbeautify.py
) so that it can accept either stdin
or an argument (file name) and output to stdout
? I also want to use the option to keep array indentation
.
Desired syntaxes
cat ugly.js | myjsbeautify.py
or
myjsbeautify.py ugly.js
Source: (StackOverflow)
I'm currently using the js-beautify plugin for SublimeText2 and editing the .jsbeautifyrc
. I was unable to find an attribute in the config docs that I think is manipulable: preserve comment indentation.
My desired output matches my initial format:
// This comment isn't indented so I can fold all of the code it refers to
function fooBar() {
... do stuff
};
Comments may be nested in another function:
var fooBar = fooBar || (function() {
// Unindented comment for folding
function fooBar() {
... do stuff
};
}());
Or in a nested object/array:
foo: {
// Another unindented comment for folding
bar: {
... more stuff
},
// Yet another unindented comment
baz: [
... even more stuff
]
}
I tried keep_comment_indentation
to no avail.
Source: (StackOverflow)