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
- Features
- Humanize date
- Ordinalize
- [Truncate] (#truncate)
- [Inflector] (#inflector)
- [Number to words] (#numbertowords)
- [To quantity] (#toquantity)
- [Humanize] (#humanize)
- [DeHumanize] (#dehumanize)
- [Roman numerals] (#romannumerals)
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:
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:
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"
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.
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"
You can Ordinalize
an instance of Int
and get back a string with the ordinalized number:
var num = 1
num.ordinalize() => "1st"
num = 100
num.ordinalize() => "100th"
You can Ordinalize
an instance of String
and get back a string with the ordinalized number:
var num = "1"
num.ordinalize() => "1st"
num = "100"
num.ordinalize() => "100th"
You can Truncate
an instance of String
and get back a string truncated with … at the end if needed:
var t = "a"
t.truncate(10) => "a"
t = "longer text then the length"
t.truncate(4) => "long…"
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.
var t = "a"
t.truncate(10, truncator = Truncator.FixedLength) => "a"
t = "longer text then the length"
t.truncate(4, truncator = Truncator.FixedLength) => "long…"
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…"
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…"
You can Truncate
an instance of String
and get back a string truncated with a custom string at the end if needed:
var t = "a"
t.truncate(10, truncationString = "...") => "a"
t = "Text with more words than truncate length"
t.truncate(10, truncationsString = "...") => "Text wi..."
You can Truncate
an instance of String
and get back a string truncated from the left:
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.
Replaces underscores with hyphens in as String
Decapitalizes first word and removes underscores while capitalizing the next letter in a String
"customer_first_name".camelize() => "customerFirstName"
Capitalizes first word and removes underscores while capitalizing the next letter in a String
"customer_first_name".pascalize() => "CustomerFirstName"
Replaces spaces with underscores and makes everything lowercase or looks for capitalized words and replaces them with underscore and lowercase in a String
"SomeTitleThatWillBeUnderscored".underscore() => "some_title_that_will_be_underscored"
Replaces underscores and dashes with spaces and capitalizes each word in a String
"some-title: The begining".titleize() => "Some Title: The Begining"
Replaces underscores with dashes in a String.
"some_title".dasherize() => "some-title"
Replaces underscores with hyphens in a String.
"some_title".hyphenate() => "some-title"
Gives the plural of a certain word.
"test".pluralize() => "tests"
"test".pluralize(Plurality.CouldBeEither) => "tests"
"tests".pluralize(Plurality.CouldBeEither) => "tests"
gives the singular of a certain word.
"tests".singularize() => "test"
"test".singularize(Plurality.CouldBeEither) => "test"
"tests".singularize(Plurality.CouldBeEither) => "test"
Gives the value in words.
1.toWords() => "one"
10.toWords() => "ten"
Gives the value in ordinal words.
1.toOrdinalWords() => "first"
10.toOrdinalWords() => "tenth"
"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"
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"
Turns pascalcased strings into sentences.
"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"
Turns sentences into pascalcased strings.
"Pascal case input string is turned into sentence".dehumanize() => "PascalCaseInputStringIsTurnedIntoSentencePascal case input string is turned into sentence"
Turns an Int into Roman numeral. Limit from 1 to 3999.
1.toRoman() => "I"
2.toRoman() => "II"
4.toRoman() => "IV"
Turns a Roman numeral into an Int. Limit from 1 to 3999.
"I".toRoman() => 1
"II".toRoman() => 2
"IV".toRoman() => 4