user5047085
user5047085

Reputation:

JS-Beautify programmatic options API

I am currently using this package https://www.npmjs.com/package/js-beautify

like this:

import {js_beautify} from 'js-beautify';

const beautifiedCode = js_beautify(`
    const foo = 'bar';



    console.log(foo);
`)

however, imagine if I want to format the above, so that the maximum number of blank lines is one, so I want this:

  const foo = 'bar';

  console.log(foo);

unfortunately, I cannot find the docs on how to pass options to the programmatic API, I would guess it's something like this:

   const beautifiedCode = js_beautify(`
        const foo = 'bar';



        console.log(foo);
    `, 
     {maxBlankLines: 1}
    )

but that's just a guess. How can I pass options to the programmatic API?

Upvotes: 0

Views: 610

Answers (1)

BitwiseMan
BitwiseMan

Reputation: 1947

Beautifier can remove blank lines between tokens using the preserve_newlines: false setting or limit the number of newlines using the max_preserve_newlines with a number value such as max_preserve_newlines: 3.

An example of API level usage is show in the Javascript section of the project README.md .

The configuration option names are the same as the CLI names but with underscores instead of dashes.

So to remove all empty lines:

import {js_beautify} from 'js-beautify';

const originalCode = `
    const foo = 'bar';



    console.log(foo);
`)


const beautifiedCode = js_beautify(originalCode, { preserve_newlines: false});

Upvotes: 1

Related Questions