Giter Club home page Giter Club logo

kase-format's Introduction

KaseFormat

Download CircleCI Awesome Kotlin Badge

Multiplatform kotlin string case conversion and detection library.

examples

(Inspired by Guava's CaseFormat)

Setup

Declare repository

Include the following in your respositories block within the build.gradle.kts:

repositories {
    maven("https://dl.bintray.com/dotcipher/maven")
}

Or in groovy syntax for a build.gradle:

repositories {
    maven {
        url 'https://dl.bintray.com/dotcipher/maven'
    }   
}

Include dependency

Include the following in your dependencies block (replacing <version> with the latest release above:

dependencies {
        // JVM implementation
        implementation("io.dotcipher.kase:kase-format-jvm:<version>")

        // Native / JS implementations coming soon
}

Usage

KaseFormat is the main entrypoint for library usage, and it's defined as an enum of the following formats:

  • LOWER_HYPHEN (ie. hello-world)
  • CAPITAL_HYPHEN (ie. Hello-World)
  • UPPER_HYPHEN (ie. HELLO-WORLD)
  • LOWER_UNDERSCORE (ie. hello_world)
  • CAPITAL_UNDERSCORE (ie. Hello_World)
  • UPPER_UNDERSCORE (ie. HELLO_WORLD)
  • LOWER_CAMEL (ie. helloWorld)
  • CAPITAL_CAMEL (ie. HelloWorld)

Conversion

The most efficient way to convert is if the source format is known before conversion. For example, if the intent is to convert from LOWER_UNDERSCORE (ie. hello_world) to CAPITAL_CAMEL (ie. HelloWorld) then you could use the following syntax:

val str = "hello_world"
val output = KaseFormat.LOWER_UNDERSCORE.convert(KaseFormat.CAPITAL_CAMEL, str)
// output == "HelloWorld"

If the source format isn't known before conversion, then a KaseConverter needs to be created targeting the destination format. The KaseConverter can be reused and is thread-safe. For example if the intent is to convert into the LOWER_HYPHEN format from any unknown format then you can use the following syntax:

val strings = listOf("string_one", "STRING-TWO", "stringThree", "StringFour")
val converter = KaseFormat.LOWER_HYPHEN.converter()

val firstOutput = converter.convert(strings[0])
// firstOutput == "string-one"

val listOutput = converter.convert(strings.sublist(1, strings.size))
// listOutput == listOf("string-two", "string-three", "string-four")

Detection

Detection of what format a string might be (by best guess), can be accomplished using the following syntax:

val str = "someString"
val format = KaseFormat.determine(str)
// format == LOWER_CAMEL

kase-format's People

Contributors

dotcipher avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

cybernetics

kase-format's Issues

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.