πŸ«πŸπŸ’πŸ…Ώ Multiplatform Kotlin library to convert strings between various case formats including Camel Case, Snake Case, Pascal Case and Kebab Case

Overview

KaseChange Maven Central Awesome Kotlin Badge

License Discord Test & Deploy | develop Test & Deploy | master

Multiplatform Kotlin library to convert strings between various case formats

Supported Case Formats

  • SCREAMING_SNAKE_CASE
  • snake_case
  • PascalCase
  • camelCase
  • TRAIN-CASE
  • kebab-case
  • UPPER SPACE CASE
  • Title Case
  • lower space case
  • UPPER.DOT.CASE
  • dot.case
  • custom one :)

Setup

  1. Add the lib to your project's dependencies:
  • Groovy (replace $kasechange_version with the version you want):
 // universal dependency for Gradle 5.3 and above
 // in case of multiplatform project, just specify the dependency for commonMain/commonTest source set
 implementation "net.pearx.kasechange:kasechange:$kasechange_version" 
 // for Gradle versions below 5.3:
 implementation "net.pearx.kasechange:kasechange-metadata:$kasechange_version" // for Common
 // or
 implementation "net.pearx.kasechange:kasechange-jvm:$kasechange_version" // for JVM
 // or
 implementation "net.pearx.kasechange:kasechange-js:$kasechange_version" // for JS
 // or
 implementation "net.pearx.kasechange:kasechange-android:$kasechange_version" // for Android
 // or
 implementation "net.pearx.kasechange:kasechange-PLATFORM_YOU_WANT:$kasechange_version" // for Native
  • Kotlin (replace $kasechangeVersion with the version you want):
 // universal dependency for Gradle 5.3 and above
 // in case of multiplatform project, just specify the dependency for commonMain/commonTest source set
 implementation("net.pearx.kasechange:kasechange:$kasechangeVersion") 
 // for Gradle versions below 5.3:
 implementation("net.pearx.kasechange:kasechange-metadata:$kasechangeVersion") // for Common
 // or
 implementation("net.pearx.kasechange:kasechange-jvm:$kasechangeVersion") // for JVM
 // or
 implementation("net.pearx.kasechange:kasechange-js:$kasechangeVersion") // for JS
 // or
 implementation("net.pearx.kasechange:kasechange-android:$kasechangeVersion") // for Android
 // or
 implementation("net.pearx.kasechange:kasechange-PLATFORM_YOU_WAN:$kasechangeVersion") // for Native
  1. Use the library and have fun!

Examples:

  • Transforming string from any case to specific one:
 "IAmAPascalCasedString123".toSnakeCase() // i_am_a_pascal_cased_string_123
 "IAmAPascalCasedString123".toCase(CaseFormat.LOWER_UNDERSCORE)  // i_am_a_pascal_cased_string_123
 "IAmAPascalCasedString123".toCase(to = CaseFormat.LOWER_UNDERSCORE, from = universalWordSplitter())  // i_am_a_pascal_cased_string_123
 "IAmAPascalCasedString123".toSnakeCase(universalWordSplitter())  // i_am_a_pascal_cased_string_123
 "IAmAPascalCasedString123".toCase(to = CaseFormat.LOWER_UNDERSCORE, from = universalWordSplitter(treatDigitsAsUppercase = false))  // i_am_a_pascal_cased_string123
 "IAmAPascalCasedString123".toSnakeCase(universalWordSplitter(treatDigitsAsUppercase = false))  // i_am_a_pascal_cased_string123
  • Transforming string from one case to another:
 "123e4567-e89b-12d3-a456-426655440000".toSnakeCase(CaseFormat.LOWER_HYPHEN) // 123e4567_e89b_12d3_a456_426655440000
 "123e4567-e89b-12d3-a456-426655440000".toCase(to = CaseFormat.LOWER_UNDERSCORE, from = CaseFormat.LOWER_HYPHEN)  // 123e4567_e89b_12d3_a456_426655440000
  • Transforming string to a custom case:
"some_string".toCase(CaseFormatterConfig(false, "..", wordCapitalize = true, firstWordCapitalize = true)) // Some..String
  • Splitting a string in any case into words:
 "XMLExtendedParser".splitToWords() // [XML, Extended, Parser]
 "XMLExtendedParser2".splitToWords(universalWordSplitter(treatDigitsAsUppercase = true)) // [XML, Extended, Parser, 2]
 "XMLExtendedParser2".splitToWords(universalWordSplitter(treatDigitsAsUppercase = false)) // [XML, Extended, Parser2]
  • Splitting a string in specific case into words:
 "s0meth1ng_in_snake_case".splitToWords(CaseFormat.LOWER_UNDERSCORE) // [s0meth1ng, in, snake, case]
Comments
  • Adding option to not treat digits as word boundaries

    Adding option to not treat digits as word boundaries

    The latest version of the library changed the behaviour when it comes to how digits are treated (commit https://github.com/pearxteam/kasechange/commit/e8e3222745956738aa8c584d9fc199ea2b37cb77).

    It used to be that something like "a_word1_with_number" == "a_word1_with_number".toSnakeCase() but it no longer holds true.

    To allow clients to maintain that behaviour if they need to so that they can still update to later versions of the library (my project is one that needs this behaviour for various reasons but I also would like to use the latest version), I raised https://github.com/pearxteam/kasechange/pull/5

    This PR maintains API compatibility as it introduces one parameter to the public API that has a default value so existing code should not be affected

    enhancement 
    opened by savvasdalkitsis 3
  • Enhance handling for digit characters

    Enhance handling for digit characters

    Description

    This PR enhances digit characters handling, and it handles digit characters same as uppercase characters. There are some samples:

    before:
    UInt32Value => U|Int32Value
    
    after:
    UInt32Value => U|Int|32|Value
    
    enhancement 
    opened by devkanro 3
  • Make config in CaseFormat publicly accessible

    Make config in CaseFormat publicly accessible

    This is related to the earlier enhancement I raised #3

    In CaseFormat, I had intentionally made the config value publicly accessible but didn't explain my reasoning behind it (my apologies):

    https://github.com/pearxteam/kasechange/blob/21ea6a12a0b7fc4b2aea66e9679d10c41cdb7716/src/commonMain/kotlin/net/pearx/kasechange/CaseFormat.kt#L13

    Since the API is now extensible, clients might want to mix and match predefined formats with their own and the only way to do it in a concise manner would be something like this:

    val formats = mapOf(
        "SCREAMING_SNAKE_CASE" to CaseFormat.UPPER_UNDERSCORE.config,
        "snake_case" to CaseFormat.LOWER_UNDERSCORE.config,
        "Pascal.Dot.Case" to CaseFormatterConfig(false, ".", wordCapitalize = true, firstWordCapitalize = true)
    )
    

    which can then be used like so:

    val format : String = parseFormatFromConfigurationFile()
    "Some words".toCase(formats["format"] ?: throw IllegalArgumentException("Unrecognised format"))
    

    If the config is not exposed, the only way is to keep two constructs and depending on the value you might want to use, know which construct to pull the formatter from.

    Any thoughts on this?

    opened by savvasdalkitsis 2
  • Allow clients to extend the library without needing to wait for an update

    Allow clients to extend the library without needing to wait for an update

    Hello,

    I really like the library, found a good use for it recently on one of my projects.

    I realised that I had need for what I'd call Pascal.Dot.Case though and realised the library was not extensible since it relies on the enums themselves performing the formatting.

    I decided to raise a PR that, in my view, fixes the problem while maintaining API compatibility: https://github.com/pearxteam/kasechange/pull/2

    Please have a look and tell me if you think it's worth merging or, if not, any changes you'd propose if you think it's worth creating an extensible API for the library.

    enhancement 
    opened by savvasdalkitsis 0
  • Custom case formatter and preserving char casing

    Custom case formatter and preserving char casing

    I have a use case where I want to format a string away from camel_case etc. but not remove the casing of each word.

    E.g. If someone wrote this_is_Kotlin, I can change it to this is Kotlin.

    Current solution is "this_is_Kotlin".splitToWords().joinToString(" "), however not able to do it with the custom case formatter, or at least haven't figured out how.

    opened by olivernybroe 0
  • Add support for detecting case

    Add support for detecting case

    Thanks for this library, works really well.

    It would be cool if it could be used to return the casing of a string also.

    "my_string".detectCase() // snake_case
    
    opened by olivernybroe 0
  • Treat digits as a continuous word

    Treat digits as a continuous word

    If [WordSplitterConfig.handleCase] is set to true: if a lowercase character is followed by an uppercase character, a word boundary is considered to be prior to the uppercase character. If [WordSplitterConfig.handleCase] is set to true: if multiple uppercase characters are followed by a lowercase character, a word boundary is considered to be prior to the last uppercase character.

    Prior to the last digit is an odd place to be considered a boundary. As such any digits which are followed by letters get chopped up.

    | Input | Expected | Actual | |---|---|---| | option14b | option 14 b | option 1 4b | | 123abc | 123 abc | 12 3abc |

    Numbers should be considered continuous

    opened by GregHib 1
Releases(v1.3.0)
Owner
PearX Team
PearX Team
Map strings from csv to xml file

CsvToXmlMapper While translating your app, your translator provides you with a .csv file containing the translated strings and you wonder how boring a

Abhriya Roy 6 Sep 25, 2021
SharedPreference Library to save all types including your custom types and observe them if need be.

A SharedPreference Library that can be used to store all types including your custom classes and observe them too if needed.

Ehma Ugbogo 18 Nov 10, 2021
Convert OkHttp requests into curl logs.

Ok2Curl Convert OkHttp requests into curl logs. Usage Add library to project dependencies. Library is hosted on jcenter. repositories { jcenter()

Michal Moczulski 373 Jan 1, 2023
Access and process various types of personal data in Android with a set of easy, uniform, and privacy-friendly APIs.

PrivacyStreams PrivacyStreams is an Android library for easy and privacy-friendly personal data access and processing. It offers a functional programm

null 269 Dec 1, 2022
A collection of Android build scripts for various third-party libraries and the tooling to build them

ndkports A collection of Android build scripts for various third-party libraries and the tooling to build them. If you're an Android app developer loo

reSIProcate project 0 Jan 17, 2022
Handy library to send & receive command with payload between subscribers for Android.

Commander Handy library to send & receive command with payload between subscribers for Android. Features Subscription based No dependency on Framework

Romman Sabbir 3 Oct 19, 2021
Various useful utilities for Android apps development

Android Commons Various useful utilities for Android apps development. API documentation provided as Javadoc. Usage Add dependency to your build.gradl

Alex Vasilkov 112 Nov 14, 2022
Secure Preference Manager for android. It uses various Encryption to protect your application's Shared Preferences.

Secure-Pref-Manager ##Secure Preference Manager is a simple Library to help you protect your Shared Preferences. Secure Preference Manager for android

Prashant Solanki 72 Nov 25, 2022
Markdown renderer for Kotlin Compose Multiplatform (Android, Desktop)

Markdown renderer for Kotlin Compose Multiplatform (Android, Desktop)

Mike Penz 129 Jan 1, 2023
KDoctor - A command-line tool that helps to set up the environment for Kotlin Multiplatform Mobile app development

KDoctor is a command-line tool that helps to set up the environment for Kotlin Multiplatform Mobile app development.

Kotlin 331 Dec 29, 2022
ASN.1 encoder/decoder for Kotlin/Multiplatform

ASN.1 encoder/decoder for Kotlin/Multiplatform. Supports Android, iOS, JavaScript and plain JVM environments

Sascha Peilicke 2 Mar 10, 2022
Base64 encoder/decoder for Kotlin/Multiplatform

Base64 encoder/decoder for Kotlin/Multiplatform. Supports Android, iOS, JavaScript and plain JVM environments.

Sascha Peilicke 11 Oct 25, 2022
Multiplaform kotlin library for calculating text differences. Based on java-diff-utils, supports JVM, JS and native targets.

kotlin-multiplatform-diff This is a port of java-diff-utils to kotlin with multiplatform support. All credit for the implementation goes to original a

Peter Trifanov 51 Jan 3, 2023
A Kotlin-based testing/scraping/parsing library providing the ability to analyze and extract data from HTML

A Kotlin-based testing/scraping/parsing library providing the ability to analyze and extract data from HTML (server & client-side rendered). It places particular emphasis on ease of use and a high level of readability by providing an intuitive DSL. It aims to be a testing lib, but can also be used to scrape websites in a convenient fashion.

null 603 Jan 1, 2023
Native Kotlin library for time-based TOTP and HMAC-based HOTP one-time passwords

A kotlin implementation of HOTP (RFC-4226) and TOTP (RFC-6238). Supports validation and generation of 2-factor authentication codes, recovery codes and randomly secure secrets.

Robin Ohs 6 Dec 19, 2022
A library for fast and safe delivery of parameters for Activities and Fragments.

MorbidMask - 吸葀青具 Read this in other languages: δΈ­ζ–‡, English, Change Log A library for fast and safe delivery of parameters for Activities and Fragment

Season 67 Mar 29, 2022
A simple and easy to use stopwatch and timer library for android

TimeIt Now with Timer support! A simple and easy to use stopwatch and timer library for android Introduction A stopwatch can be a very important widge

Yashovardhan Dhanania 35 Dec 10, 2022
Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platform the code is running.

Trail Trail is a simple logging system for Java and Android. Create logs using the same API and the library will detect automatically in which platfor

Mauricio Togneri 13 Aug 29, 2022
nestegg - Very simple Kotlin caching library

nestegg - Very simple Kotlin caching library

Runner-be 4 Jun 15, 2022