rstuven / curlyfy

Add curly brackets to a whitespace indented string.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

curlyfy

Build Status Coverage Status dependencies Status devDependencies Status

curlyfy adds curly brackets to a whitespace indented string like this:

/* input */
a
    b
        c
            d
            e
    f
/* output */
a {
    b {
        c {
            d
            e
        }
    }
    f
}

It also handles some exceptions:

/* input and output, no curly brackets added */

// trailing opening curly bracket
a = {
  b: c
}

// trailing opening square bracket
a = [
  b,
  c
]

// trailing opening parentheses
fn(
  a,
  b,
  c)

// trailing comma
fn(a,
   b,
   c)

Library usage

npm install --save curlyfy
var curlyfy = require('curlyfy');
var output = curlyfy(input [, options]);

CLI usage

npm install --global curlyfy
curlyfy --help

File input as argument:

curlyfy file [options]

Standard input:

cat file | curlyfy [options]

Options

blockOpeningException (default: '[{\\[\\(,]'):

If this regular expression is found at the end of a block opening line, curly brackets are not added to that block.

insertSemicolons (default: false):

Support [JavaScript semicolon-less style] (https://docs.npmjs.com/misc/coding-style#semicolons) gotchas. Because we guess that if you ditched braces you will probably ditch semicolons too ;)

For example:

var output = curlyfy(input, {insertSemicolons: true})
/* input */
++i
--i
(x || y).doSomething()
[a, b, c].forEach(doSomething)
/* output */
;++i
;--i
;(x || y).doSomething()
;[a, b, c].forEach(doSomething)

TO-DO:

  • Handle trailing comments
  • Handle multiline comments
  • Normalize indentations

About

Add curly brackets to a whitespace indented string.

License:MIT License


Languages

Language:JavaScript 100.0%