Code Monkey home page Code Monkey logo

grails-smart-case's Introduction

Smart Case Grails Plugin

Build Status

This is a Grails plugin for converting between cases for Strings values. This is an overload of one of my favorite Grails plugins, the slug-generator.

You can perform convertions to and from these cases:

  • human case
  • lowerCamelCase
  • UpperCamelCase
  • snake_case
  • SCREAMING_SNAKE_CASE
  • slug-case

##Case convertion

The plugin adds encoding codecs such as:

String example = 'some example phrase'
example.encodeAsCamel() == 'someExamplePhrase'
example.encodeAsUpperCamel() == 'SomeExamplePhrase'
example.encodeAsSnake() == 'some_example_phrase'
example.encodeAsScreamingSnake() == 'SOME_EXAMPLE_PHRASE'
example.encodeAsSlug() == 'some-example-phrase' //borrowed from slug-generator plugin

Also, you can convert between cases or even going back to human case:

String camelExample = 'thisIsCamel'
camelExample.encodeAsSnake() == 'this_is_camel'
camelExample.encodeAsScreamingSnake() == 'THIS_IS_CAMEL'
camelExample.encodeAsUpperCamel() == 'ThisIsCamel'
camelExample.encodeAsSlug() == 'this-is-camel'
camelExample.encodeAsHuman() == 'this is camel'

The codecs need to detect the original casing in order to convert. In case you find an error (please report an issue) or want to make sure you get the right convertion, you can inject and use the smartCaseService directly like this:

smartCaseService.convert(Case.CAMEL, Case.SNAKE, 'thisIsCamel') == 'this_is_camel'

##String formatting

We are all used to methods like toLowerCase(), toUpperCase() and even capitalize(). And some combination of these lets you format your Strings the way you usually want it (like capitalizing each word). But sometimes you end up having a capital letter where you don't want it or missing one where there should be one. For example, you don't want works like "of" or "and" to be capitalized (also works for Spanish). So here is how you do the magic:

String foo = bar.encodeAsTitle()

And here is a comparison with some example phrases:

Phrase toLowerCase() toUpperCase() capitalize() encodeAsTitle()
HELLO, MY NAME IS MATHIAS hello, my name is mathias HELLO, MY NAME IS MATHIAS HELLO, MY NAME IS MATHIAS Hello, my name is Mathias
united states of america united states of america UNITED STATES OF AMERICA United states of america United States of America
Avenida 8 De Octubre avenida 8 de octubre AVENIDA 8 DE OCTUBRE Avenida 8 de octubre Avenida 8 de Octubre

grails-smart-case's People

Contributors

burtbeckwith avatar mathifonseca avatar

Watchers

 avatar  avatar  avatar

Forkers

burtbeckwith

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.