Giter VIP home page Giter VIP logo

groovy-beautify's Introduction

groovy-beautify

A simple zero-dependency beautifier which allows formatting and indenting Groovy scripts

Demo

You can test how it works on the Demo Website

Installation

npm install groovy-beautify

Usage

import groovyBeautify from "groovy-beautify";

const groovy = `
    def bumpVersion(String target,    String version_type, Boolean reset =   false) {    def
    versionMap =
    ['major':0, 'minor' : 1, 'patch':   2]
                def versionArray = target.findAll(/\d+\.\d+\.\d+/)[0].tokenize('.')
            try
    {        def   index =     versionMap.get(version_type);
    versionArray[index] =versionArray[index].toInteger() + 1
    if(   reset )
    {
        for(int i=2;i>index;     i--) {
            versionArray[i]    =    0            }        }
    } catch(       Exception e) {        println("Unrecognized version type \\"version_type\\" (should be major, minor or patch)")    }
    return             versionArray.join(                   '.'                       )
    }
    println(bumpVersion('1.2.3', 'minor', true))
`;

const formatted = groovyBeautify(groovy);

This will produce the following output

def bumpVersion(String target, String version_type, Boolean reset = false) {
    def versionMap = ['major': 0, 'minor': 1, 'patch': 2]
    def versionArray = target.findAll(/d+.d+.d+/)[0].tokenize('.')
    try {
        def index = versionMap.get(version_type);
        versionArray[index] = versionArray[index].toInteger() + 1
        if (reset)
        {
            for (int i = 2; i > index; i--) {
                versionArray[i] = 0
            }
        }
    } catch (Exception e) {
        println("Unrecognized version type \"version_type\" (should be major, minor or patch)")
    }
    return versionArray.join('.')
}

println(bumpVersion('1.2.3', 'minor', true))

License

You are free to use this in any way you want, in case you find this useful or working for you but you must keep the copyright notice and license. (MIT)

Credits

groovy-beautify's People

Contributors

sergei-kasoverskij-amelia avatar joycollector avatar

Stargazers

Daniel Bodnar avatar Felipe Santos avatar Konstantin Vyatkin avatar Alexey avatar Francis avatar yuezhuangshi avatar  avatar  avatar Leo Bartoloni avatar

Watchers

 avatar

groovy-beautify's Issues

Option to print closure parameters on single line

First of all, thank you for open sourcing this library, it could be a great help for us!

Given code like:

[].each { item ->
println item
}

It would be nice to format it as:

[].each { item ->
    println item
}

Right now, the closure parameters are moved down a line by default:

[].each {
    item -> println item
}

Keywords are not properly formatted when used as methods

When it needs to use a keyword as a method, such as .with(), the script encloses it in whitespace and the code next to the keyword in curly braces.

LocalDate
    .parse("2018-12-06")
    .with(Month.MARCH)
    .with(TemporalAdjusters.lastDayOfMonth())

produces

LocalDate
    .parse("2018-12-06")
    . with (Month.MARCH) {
    . with (TemporalAdjusters.lastDayOfMonth())
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.