Humanizer.jvm is an adaptation of the humanizer framework for .Net which is made for the jvm and is written in Kotlin.

Related tags

Kotlin Humanizer.jvm
Overview

Logo

Humanizer.jvm is an adaptation of the humanizer framework for .Net which is made for the jvm and is written in Kotlin. Humanizer.jvm meets all your jvm needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities.

The current build status on the CI server is status

Table of contents

  • Features
    • Humanize date
    • Ordinalize
    • [Truncate] (#truncate)
    • [Inflector] (#inflector)
    • [Number to words] (#numbertowords)
    • [To quantity] (#toquantity)
    • [Humanize] (#humanize)
    • [DeHumanize] (#dehumanize)
    • [Roman numerals] (#romannumerals)

Features

Humanize date

Extension methods Humanize for Java.Util.Date objects

You can Humanize an instance of Java.Util.Date and get back a string telling how far back or forward in time that is compared to a given date:

"2 hours ago" cal.add(Calendar.HOURS, 30) cal.Humanize(dateToUse) => "tomorrow" cal.getTime.add(Calendar.HOURS, 2) cal.getTime().Humanize(dateToUse) => "2 hours from now"">
val cal = GregorianCalendar()
val dateToUse = GregorianCalendar(2014,Calendar.JANUARY, 5).getTime()
cal.setTime(dateToUse)
cal.add(Calendar.DATE, -1)
cal.getTime().Humanize(dateToUse) => "yesterday"
cal.add(Calendar.HOURS, -2)
cal.getTime.Humanize(dateToUse) => "2 hours ago"

cal.add(Calendar.HOURS, 30)
cal.Humanize(dateToUse) => "tomorrow"
cal.getTime.add(Calendar.HOURS, 2)
cal.getTime().Humanize(dateToUse) => "2 hours from now"

Or you can Humanize an instance of Java.Util.Date and get back a string telling how far back or forward in time that is compared to the current date:

"2 hours ago" cal.add(Calendar.HOURS, 30) cal.Humanize() => "tomorrow" cal.getTime.add(Calendar.HOURS, 2) cal.getTime().Humanize() => "2 hours from now"">
val cal = GregorianCalendar()
cal.add(Calendar.DATE, -1)
cal.getTime().Humanize() => "yesterday"
cal.add(Calendar.HOURS, -2)
cal.getTime.Humanize() => "2 hours ago"

cal.add(Calendar.HOURS, 30)
cal.Humanize() => "tomorrow"
cal.getTime.add(Calendar.HOURS, 2)
cal.getTime().Humanize() => "2 hours from now"

Milliseconds to timespan

Extension method millisecondsToTimespan for Int and Long objects

Since difference between dates are returned as milliseconds this method turns them into a readable form/ This is a naive implementation and does not account for leapseconds, this is the nature of this method. By default the milliseconds precision is turned off but you can add it by passing true.

"0 seconds and 1 millisecond" 99999999.milliSecondsToTimespan() => "1 day and 3 hours and 46 minutes and 39 seconds" 99999999.milliSecondsToTimespan(true) => "1 day and 3 hours and 46 minutes and 39 seconds and 999 milliseconds"">
1.milliSecondsToTimespan() => "0 seconds"
1.milliSecondsToTimespan(true) => "0 seconds and 1 millisecond"

99999999.milliSecondsToTimespan() => "1 day and 3 hours and 46 minutes and 39 seconds"
99999999.milliSecondsToTimespan(true) => "1 day and 3 hours and 46 minutes and 39 seconds and 999 milliseconds"

Ordinalize

Extension method Ordinalize for Int objects

You can Ordinalize an instance of Int and get back a string with the ordinalized number:

"100th"">
var num = 1
num.ordinalize() => "1st"
num = 100
num.ordinalize() => "100th"

Extension method Ordinalize for String objects

You can Ordinalize an instance of String and get back a string with the ordinalized number:

"1st" num = "100" num.ordinalize() => "100th"">
var num = "1"
num.ordinalize() => "1st"
num = "100"
num.ordinalize() => "100th"

Truncate

Extension method truncate for String objects

You can Truncate an instance of String and get back a string truncated with … at the end if needed:

"a" t = "longer text then the length" t.truncate(4) => "long…"">
var t = "a"
t.truncate(10) => "a"
t = "longer text then the length"
t.truncate(4) => "long…"

Extension method truncate for String objects with Truncator option

You can Truncate an instance of String and get back a string truncated with … at the end if needed: Truncator Enum * FixedLength = counts all characters * FixedNumberOfCharacters = counts only letters and digits * FixedNumberOfWords = counts the words by splitting on whitespace.

FixedLength

"a" t = "longer text then the length" t.truncate(4, truncator = Truncator.FixedLength) => "long…"">
var t = "a"
t.truncate(10, truncator = Truncator.FixedLength) => "a"
t = "longer text then the length"
t.truncate(4, truncator = Truncator.FixedLength) => "long…"

FixedNumberOfCharacters

"a" t = "Text with more characters than truncate length" t.truncate(10, truncator = Truncator.FixedNumberOfCharacters) => "Text with m…"">
var t = "a"
t.truncate(10, truncator = Truncator.FixedNumberOfCharacters) => "a"
t = "Text with more characters than truncate length"
t.truncate(10, truncator = Truncator.FixedNumberOfCharacters) => "Text with m…"

FixedNumberOfWords

"a" t = "Text with more words than truncate length" t.truncate(4, truncator = Truncator.FixedNumberOfWords) => "Text with more words…"">
var t = "a"
t.truncate(10, truncator = Truncator.FixedNumberOfWords) => "a"
t = "Text with more words than truncate length"
t.truncate(4, truncator = Truncator.FixedNumberOfWords) => "Text with more words…"

Extension method truncate for String objects with TruncatorString option

You can Truncate an instance of String and get back a string truncated with a custom string at the end if needed:

"a" t = "Text with more words than truncate length" t.truncate(10, truncationsString = "...") => "Text wi..."">
var t = "a"
t.truncate(10, truncationString = "...") => "a"
t = "Text with more words than truncate length"
t.truncate(10, truncationsString = "...") => "Text wi..."

Extension method truncate for String objects with TruncatorString option

You can Truncate an instance of String and get back a string truncated from the left:

"a" t = "Text with more words than truncate length" t.truncate(10, truncateFrom = TruncateFrom.Left) => "…te length"">
var t = "a"
t.truncate(10, truncateFrom = TruncateFrom.Left) => "a"
t = "Text with more words than truncate length"
t.truncate(10, truncateFrom = TruncateFrom.Left) => "…te length"

Or you can use a combination of the above parameters length, truncationString, truncator, truncateFrom.

Inflector

Extension method camelize for String objects

Replaces underscores with hyphens in as String

Decapitalizes first word and removes underscores while capitalizing the next letter in a String

"customerFirstName"">
"customer_first_name".camelize() => "customerFirstName"

Extension method pascalize for String objects

Capitalizes first word and removes underscores while capitalizing the next letter in a String

"CustomerFirstName"">
"customer_first_name".pascalize() => "CustomerFirstName"

Extension method underscore for String objects

Replaces spaces with underscores and makes everything lowercase or looks for capitalized words and replaces them with underscore and lowercase in a String

"some_title_that_will_be_underscored"">
"SomeTitleThatWillBeUnderscored".underscore() => "some_title_that_will_be_underscored"

Extension method titleize for String objects

Replaces underscores and dashes with spaces and capitalizes each word in a String

"Some Title: The Begining"">
"some-title: The begining".titleize() => "Some Title: The Begining"

Extension method dasherize for String objects

Replaces underscores with dashes in a String.

"some-title"">
"some_title".dasherize() => "some-title"

Extension method hyphenate for String objects

Replaces underscores with hyphens in a String.

"some-title"">
"some_title".hyphenate() => "some-title"

Extension method pluralize for String objects

Gives the plural of a certain word.

"tests" "test".pluralize(Plurality.CouldBeEither) => "tests" "tests".pluralize(Plurality.CouldBeEither) => "tests"">
"test".pluralize() => "tests"

"test".pluralize(Plurality.CouldBeEither) => "tests"
"tests".pluralize(Plurality.CouldBeEither) => "tests"

Extension method singularize for String objects

gives the singular of a certain word.

"test" "test".singularize(Plurality.CouldBeEither) => "test" "tests".singularize(Plurality.CouldBeEither) => "test"">
"tests".singularize() => "test"

"test".singularize(Plurality.CouldBeEither) => "test"
"tests".singularize(Plurality.CouldBeEither) => "test"

Number to words

Extension method toWords for String objects

Gives the value in words.

"ten"">
1.toWords() => "one"
10.toWords() => "ten"

Extension method toOrdinalWords for String objects

Gives the value in ordinal words.

"tenth"">
1.toOrdinalWords() => "first"
10.toOrdinalWords() => "tenth"

To quantity

Extension method toQuantity for String objects

"1 case" "cases".toQuantity(1) => "1 case" "case".toQuantity(10) => "10 cases" "cases".toQuantity(1, showAsQuantity = ShowQuantityAs.None) => "case" "cases".toQuantity(2, showAsQuantity = ShowQuantityAs.None) => "cases" "cases".toQuantity(1, showAsQuantity = ShowQuantityAs.Numeric) => "1 case" "cases".toQuantity(2, showAsQuantity = ShowQuantityAs.Numeric) => "1 case" "cases".toQuantity(1, showAsQuantity = ShowQuantityAs.Words) => "one case" "cases".toQuantity(2, showAsQuantity = ShowQuantityAs.Words) => "two cases"">
"case".toQuantity(1) => "1 case"
"cases".toQuantity(1) => "1 case"
"case".toQuantity(10) => "10 cases"

"cases".toQuantity(1, showAsQuantity = ShowQuantityAs.None) => "case"
"cases".toQuantity(2, showAsQuantity = ShowQuantityAs.None) => "cases"

"cases".toQuantity(1, showAsQuantity = ShowQuantityAs.Numeric) => "1 case"
"cases".toQuantity(2, showAsQuantity = ShowQuantityAs.Numeric) => "1 case"

"cases".toQuantity(1, showAsQuantity = ShowQuantityAs.Words) => "one case"
"cases".toQuantity(2, showAsQuantity = ShowQuantityAs.Words) => "two cases"

Extension method toQuantity for Int objects

"1 case" 1.toQuantity("cases") => "1 case" 10.toQuantity("case") => "10 cases" 1.toQuantity("cases", showAsQuantity = ShowQuantityAs.None) => "case" 2.toQuantity("cases", showAsQuantity = ShowQuantityAs.None) => "cases" 1.toQuantity("cases", showAsQuantity = ShowQuantityAs.Numeric) => "1 case" 2.toQuantity("cases", showAsQuantity = ShowQuantityAs.Numeric) => "1 case" 1.toQuantity("cases", showAsQuantity = ShowQuantityAs.Words) => "one case" 2.toQuantity("cases", showAsQuantity = ShowQuantityAs.Words) => "two cases"">
1.toQuantity("case") => "1 case"
1.toQuantity("cases") => "1 case"
10.toQuantity("case") => "10 cases"

1.toQuantity("cases", showAsQuantity = ShowQuantityAs.None) => "case"
2.toQuantity("cases", showAsQuantity = ShowQuantityAs.None) => "cases"

1.toQuantity("cases", showAsQuantity = ShowQuantityAs.Numeric) => "1 case"
2.toQuantity("cases", showAsQuantity = ShowQuantityAs.Numeric) => "1 case"

1.toQuantity("cases", showAsQuantity = ShowQuantityAs.Words) => "one case"
2.toQuantity("cases", showAsQuantity = ShowQuantityAs.Words) => "two cases"

Humanize

Extension method humanize for String objects

Turns pascalcased strings into sentences.

"Pascal case input string is turned into sentence" "Underscored_input_String_is_turned_INTO_sentence".humanize() => "Underscored input String is turned INTO sentence" "HTMLIsTheLanguage".humanize() => "HTML is the language" "CanReturnTitleCase".humanize(LetterCasing.Title) => "Can Return Title Case" "CanReturnLowerCase".humanize(LetterCasing.LowerCase) => "can return lower case" "CanReturnSentenceCase".humanize(LetterCasing.Sentence) => "Can return sentence case" "CanHumanizeIntoUpperCase".humanize(LetterCasing.AllCaps) => "CAN HUMANIZE INTO UPPER CASE"">
"PascalCaseInputStringIsTurnedIntoSentence".humanize() => "Pascal case input string is turned into sentence"
"Underscored_input_String_is_turned_INTO_sentence".humanize() => "Underscored input String is turned INTO sentence"
"HTMLIsTheLanguage".humanize() => "HTML is the language"

"CanReturnTitleCase".humanize(LetterCasing.Title) => "Can Return Title Case"

"CanReturnLowerCase".humanize(LetterCasing.LowerCase) => "can return lower case"

"CanReturnSentenceCase".humanize(LetterCasing.Sentence) => "Can return sentence case"

"CanHumanizeIntoUpperCase".humanize(LetterCasing.AllCaps) => "CAN HUMANIZE INTO UPPER CASE"

Dehumanize

Extension method dehumanize for String objects

Turns sentences into pascalcased strings.

"PascalCaseInputStringIsTurnedIntoSentencePascal case input string is turned into sentence"">
"Pascal case input string is turned into sentence".dehumanize() => "PascalCaseInputStringIsTurnedIntoSentencePascal case input string is turned into sentence"

RomanNumerals

Extension method toRoman for Int objects

Turns an Int into Roman numeral. Limit from 1 to 3999.

"II" 4.toRoman() => "IV"">
1.toRoman() => "I"
2.toRoman() => "II"
4.toRoman() => "IV"

Extension method fromRoman for String objects

Turns a Roman numeral into an Int. Limit from 1 to 3999.

1 "II".toRoman() => 2 "IV".toRoman() => 4">
"I".toRoman() => 1
"II".toRoman() => 2
"IV".toRoman() => 4
You might also like...
Android Kotlin paged endpoints made easy
Android Kotlin paged endpoints made easy

A smart and simple way to work with paged endpoints. To see an example of how to use it, check out the introducing Fountain posts: part one and part t

A curated collection of splendid gradients made in Kotlin
A curated collection of splendid gradients made in Kotlin

Gradients A curated collection of splendid gradients made in Kotlin (port of https://webgradients.com for Android). Only linear gradients included for

SharedPreference usage made fun in Kotlin

PreferenceHolder Kotlin Android Library, that makes preference usage simple and fun. To stay up-to-date with news about library This library is younge

Open source Crypto Currency Tracker Android App made fully in Kotlin
Open source Crypto Currency Tracker Android App made fully in Kotlin

CoinBit CoinBit is a beautiful CryptoCurrency app, completely open sourced and 100% in kotlin. It supports following features Track prices of over 300

Native android app made with Kotlin & Compose with example usage of Ktor, SqlDelight.
Native android app made with Kotlin & Compose with example usage of Ktor, SqlDelight.

Delight-Playground 🎉 Native Android application built with Kotlin and Jetpack Compose. This app also illustrates the usage of advance libraries such

Lightweight compiler plugin intended for Kotlin/JVM library development and symbol visibility control.

Restrikt A Kotlin/JVM compiler plugin to restrict symbols access, from external project sources. This plugin offers two ways to hide symbols: An autom

BlurHash support for iOS, Android and JVM via Kotlin Multiplatform
BlurHash support for iOS, Android and JVM via Kotlin Multiplatform

blurhash A Kotlin Multiplatform library to use blurhash in your Android App, iOS / Mac App & JVM Backend. Android iOS JVM Why? If you've tried using b

Filmesflix - Project made during the NTT DATA Android Developer bootcamp. Developing knowledge in MVVM and Clear Architecture
Filmesflix - Project made during the NTT DATA Android Developer bootcamp. Developing knowledge in MVVM and Clear Architecture

FilmesFlix Projeto criado para o módulo de MVVM e Clean Architecture no Bootcamp

A personal project made using Jetpack Compose, Clean-MVVM architecture and other jetpack libraries

A basic CRUD for recording your personal credit and debit transactions. Made using Jetpack Compose, Clean-MVVM and other Jetpack libraries, incorporated with Unit Tests.

Comments
  • Datehumanizertests and implementation

    Datehumanizertests and implementation

    Same tests as in the .net version apart from one extra one. Might be a slightly different implementation no localization yet, all just English.

    Not sure why intellij changed the idea files so you should probably not merge those. But what do I know.

    Happy building.

    opened by chrissie1 8
  • Pluralize templating breaks when string includes an apostrophe

    Pluralize templating breaks when string includes an apostrophe

    I've been using Humanizer in a kotlin project recently and was trying to use the pluralize method however I ran into an interesting bug.

    val result = Humanize.pluralize(
            "string with a single thing",
            "string with {1} things",
            "string with no things",
            3,
            "three"
    )
    
    println(result)
    // => "string with three things"
    

    This usage properly works and will template the value three into the many parameter. However, if I add an apostrophe somewhere in the string, the templating no longer works, and the apostrophe is removed.

    val result = Humanize.pluralize(
            "string with a single thing",
            "can't use string with {1} things",
            "string with no things",
            3,
            "three"
    )
    
    println(result)
    // => "cant use string with {1} things"
    
    opened by AnthonyP4312 0
Owner
Mehdi Khalili
Mehdi Khalili
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Practical Kotlin Multiplatform on the Web 본 저장소는 코틀린 멀티플랫폼 기반 웹 프로그래밍 워크숍(강좌)을 위해 작성된 템플릿 프로젝트가 있는 곳입니다. 워크숍 과정에서 코틀린 멀티플랫폼을 기반으로 프론트엔드(front-end)는 Ko

SpringRunner 14 Nov 5, 2022
Create an application with Kotlin/JVM and Kotlin/JS, and explore features around code sharing, serialization, server- and client

Building a Full Stack Web App with Kotlin Multiplatform 본 저장소는 INFCON 2022에서 코틀린 멀티플랫폼 기반 웹 프로그래밍 핸즈온랩을 위해 작성된 템플릿 프로젝트가 있는 곳입니다. 핸즈온 과정에서 코틀린 멀티플랫폼을

Arawn Park 19 Sep 8, 2022
Run Kotlin/JS libraries in Kotlin/JVM and Kotlin/Native programs

Zipline This library streamlines using Kotlin/JS libraries from Kotlin/JVM and Kotlin/Native programs. It makes it possible to do continuous deploymen

Cash App 1.5k Dec 30, 2022
Dead simple EventBus for Android made with Kotlin and RxJava 2

KBus Super lightweight (13 LOC) and minimalistic (post(), subscribe(), unsubscribe()) EventBus written with idiomatic Kotlin and RxJava 2 KBus in 3 st

Adriel Café 46 Dec 6, 2022
Server Sent Events (SSE) client multiplatform library made with Kotlin and backed by coroutines

OkSSE OkSSE is an client for Server Sent events protocol written in Kotlin Multiplatform. The implementation is written according to W3C Recommendatio

BioWink GmbH 39 Nov 4, 2022
⏰ A powerful and simple-to-use guilded wrapper made in Kotlin.

⏰ guilded-kt [WIP] A powerful yet simple-to-use guilded wrapper made entirely in Kotlin with supporting multiplatform. Take a look at an example of th

Gabriel 13 Jul 30, 2022
🎲 A powerful and simple-to-use guilded wrapper made in Kotlin.

?? deck [WIP] Deck is a powerful yet simple-to-use guilded wrapper made entirely in Kotlin with support to multiplatform. Implementating In case you'r

Gabriel 13 Jul 30, 2022
A discord bot made in Kotlin powered by JDA and Realms.

A discord bot made in Kotlin powered by JDA and Realms.

null 1 Jun 30, 2022
RoomJetpackCompose is an app written in Kotlin and shows a simple solution to perform CRUD operations in the Room database using Kotlin Flow in clean architecture.

RoomJetpackCompose is an app written in Kotlin and shows a simple solution to perform CRUD operations in the Room database using Kotlin Flow in clean architecture.

Alex 27 Jan 1, 2023
Utility for developers and QAs what helps minimize time wasting on writing the same data for testing over and over again. Made by Stfalcon

Stfalcon Fixturer A Utility for developers and QAs which helps minimize time wasting on writing the same data for testing over and over again. You can

Stfalcon LLC 31 Nov 29, 2021