简体   繁体   中英

Reformatting a javascript function for printing

So, deciding to be right on the edges of my competency, I have decided to give Nashorn a try, because I have a use case where it would be useful to allow user the ability to write their own filters for a Java app.

I can pretty much make everything work end to end (web page with textarea -> saved Couchbase object -> script engine within Java), but for the UI piece I would like to be able to prettify/format the user defined text field (not necessarily in real time, but maybe upon saving).

I originally thought it would be relatively simple to use JSON.stringify, but apparently life isn't that convenient. Is there a javascript native/library based approach to taking the string representation of a function and applying lipstick to my user generated pigs?

Dave

http://jsbeautifier.org library will format your javascript functions. JSON.stringify 3rd argument allows you to pretty up your JSON.

You can use the js-beautify module that does the beautifying at http://jsbeautifier.org . Usage:

var beautifiedJsString = js_beautify('[YourJsString]', options);

List of options here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM