Skip to content

Commit bdfb244

Browse files
committed
Merge pull request #13 from chrissie1/master
Made humanize work, the one Hadi started.
2 parents c507bb4 + a82ad0c commit bdfb244

File tree

3 files changed

+183
-15
lines changed

3 files changed

+183
-15
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ The current build status on the CI server is <a href="http://teamcity.ginnivan.n
1212
- [Ordinalize](#ordinalize)
1313
- [Truncate] (#truncate)
1414
- [Inflector] (#inflector)
15+
- [Number to words] (#numbertowords)
16+
- [To quantity] (#toquantity)
17+
- [Humanize] (#humanize)
1518

1619
# <a id="features">Features</a>
1720

@@ -259,3 +262,23 @@ Gives the value in ordinal words.
259262
"cases".toQuantity(1, showAsQuantity = ShowQuantityAs.Words) => "one case"
260263
"cases".toQuantity(2, showAsQuantity = ShowQuantityAs.Words) => "two cases"
261264
```
265+
266+
## <a id="humanize">Humanize</a>
267+
268+
### Extension method humanize for String objects
269+
270+
Turns pascalcased strings into sentences.
271+
272+
```kotlin
273+
"PascalCaseInputStringIsTurnedIntoSentence".humanize() => "Pascal case input string is turned into sentence"
274+
"Underscored_input_String_is_turned_INTO_sentence".humanize() => "Underscored input String is turned INTO sentence"
275+
""HTMLIsTheLanguage".humanize() => "HTML is the language"
276+
277+
"CanReturnTitleCase".humanize(LetterCasing.Title) => "Can Return Title Case"
278+
279+
"CanReturnLowerCase".humanize(LetterCasing.LowerCase) => "can return lower case"
280+
281+
"CanReturnSentenceCase".humanize(LetterCasing.Sentence) => "Can return sentence case"
282+
283+
"CanHumanizeIntoUpperCase".humanize(LetterCasing.AllCaps) => "CAN HUMANIZE INTO UPPER CASE"
284+
```

src/main/kotlin/org/humanizer/jvm/PascalCaseHumanizer.kt

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,42 @@ package org.humanizer.jvm
22

33

44

5-
fun String.humanize(): String {
6-
// this is just a template. needs a lot of work.
7-
return this.replaceAll("[A-Z]", " $0").trim()
5+
fun String.humanize(letterCasing : LetterCasing = LetterCasing.Sentence): String {
6+
val regex = "(?<=[a-z])(?=[A-Z0-9])|(?<=[0-9])(?=[A-Za-z])|(?<=[A-Z])(?=[0-9])|(?<=[A-Z])(?=[A-Z][a-z])"
7+
8+
if(letterCasing == LetterCasing.LowerCase) return this.replaceAll("${regex}|(_)|(-)", " $0").trim().replace("_","").toLowerCase()
9+
if(letterCasing == LetterCasing.Title) {
10+
return this
11+
.split("${regex}|(_)|(-)")
12+
.let{ it.map{ if(it.checkAllCaps() && it.length() > 1) it else it.capitalize()} }
13+
.makeString(" ")}
14+
if(letterCasing == LetterCasing.AllCaps) return this.replaceAll("${regex}|(_)|(-)", " $0").trim().replace("_","").toUpperCase()
15+
if(letterCasing == LetterCasing.Sentence) {
16+
if(this.contains("_"))
17+
return this.replace("_"," ")
18+
else
19+
return this
20+
.split(regex)
21+
.let{ it.map{ if(it.checkAllCaps() && it.length() > 1) it else it.toLowerCase()} }
22+
.makeString(" ")
23+
.capitalize()
24+
.replace(" i ", " I ")}
25+
return this
826
}
927

28+
fun String.checkAllCaps(): Boolean
29+
{
30+
return this.all{it.isUpperCase()}
31+
}
32+
33+
fun String.removerUnderscore()
34+
{}
35+
36+
enum class LetterCasing
37+
{
38+
Title
39+
AllCaps
40+
LowerCase
41+
Sentence
42+
}
43+

test/main/kotlin/org/humanizer/jvm/tests/PascalCaseHumanizerTests.kt

Lines changed: 123 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,135 @@ import java.util.ArrayList
66
import java.util.HashMap
77
import org.spek.*
88
import org.humanizer.jvm.humanize
9+
import org.humanizer.jvm.LetterCasing
10+
import org.humanizer.jvm.checkAllCaps
911

1012
public class PascalCaseHumanizerTests(): Spek() {
1113
{
1214

13-
val data = listOf(
14-
"ThisIsATest" to "This Is A Test",
15-
"ThisIsAnotherTest" to "This Is Another Test"
16-
)
15+
var data = listOf(
16+
"PascalCaseInputStringIsTurnedIntoSentence" to"Pascal case input string is turned into sentence",
17+
"WhenIUseAnInputAHere" to"When I use an input a here",
18+
"10IsInTheBegining" to"10 is in the begining",
19+
"NumberIsAtTheEnd100" to"Number is at the end 100",
20+
"XIsFirstWordInTheSentence" to"X is first word in the sentence")
1721

18-
givenData(data) {
19-
val (input, expected) = it
20-
on("calling humanize", {
21-
val actual = input.humanize()
22-
it("should become ${it.component2()}", {
23-
shouldEqual(expected, actual)
22+
givenData(data) {
23+
val (input ,expected) = it
24+
on("calling humanize on $input",{
25+
val actual = input.humanize()
26+
it("should become ${it.component2()}",{
27+
shouldEqual(expected, actual)
28+
})
2429
})
25-
})
26-
}
30+
}
31+
32+
data = listOf(
33+
"Underscored_input_string_is_turned_into_sentence" to"Underscored input string is turned into sentence",
34+
"Underscored_input_String_is_turned_INTO_sentence" to"Underscored input String is turned INTO sentence")
35+
36+
givenData(data) {
37+
val (input ,expected) = it
38+
on("calling humanize on $input",{
39+
val actual = input.humanize()
40+
it("should become ${it.component2()}",{
41+
shouldEqual(expected, actual)
42+
})
43+
})
44+
}
45+
46+
data = listOf(
47+
"HTML" to"HTML",
48+
"TheHTMLLanguage" to"The HTML language",
49+
"HTMLIsTheLanguage" to"HTML is the language",
50+
"TheLanguageIsHTML" to"The language is HTML",
51+
"HTML5" to"HTML 5",
52+
"1HTML" to"1 HTML")
53+
54+
givenData(data) {
55+
val (input ,expected) = it
56+
on("calling humanize on $input",{
57+
val actual = input.humanize()
58+
it("should become ${it.component2()}",{
59+
shouldEqual(expected, actual)
60+
})
61+
})
62+
}
63+
64+
data = listOf(
65+
"CanReturnTitleCase" to"Can Return Title Case",
66+
"Can_return_title_Case" to"Can Return Title Case",
67+
"Title_humanization_Honors_ALLCAPS" to"Title Humanization Honors ALLCAPS")
68+
69+
givenData(data) {
70+
val (input ,expected) = it
71+
on("calling humanize on $input",{
72+
val actual = input.humanize(LetterCasing.Title)
73+
it("should become ${it.component2()}",{
74+
shouldEqual(expected, actual)
75+
})
76+
})
77+
}
78+
79+
data = listOf(
80+
"CanReturnLowerCase" to"can return lower case",
81+
"Can_Return_Lower_Case" to"can return lower case",
82+
"LOWERCASE" to"lowercase")
83+
84+
givenData(data) {
85+
val (input ,expected) = it
86+
on("calling humanize on $input",{
87+
val actual = input.humanize(LetterCasing.LowerCase)
88+
it("should become ${it.component2()}",{
89+
shouldEqual(expected, actual)
90+
})
91+
})
92+
}
93+
94+
data = listOf(
95+
"CanReturnSentenceCase" to"Can return sentence case",
96+
"Can_Return_Sentence_Case" to"Can Return Sentence Case",
97+
"" to"")
98+
99+
givenData(data) {
100+
val (input ,expected) = it
101+
on("calling humanize on $input",{
102+
val actual = input.humanize(LetterCasing.Sentence)
103+
it("should become ${it.component2()}",{
104+
shouldEqual(expected, actual)
105+
})
106+
})
107+
}
108+
109+
data = listOf(
110+
"CanHumanizeIntoUpperCase" to"CAN HUMANIZE INTO UPPER CASE",
111+
"Can_Humanize_into_Upper_case" to"CAN HUMANIZE INTO UPPER CASE")
112+
113+
givenData(data) {
114+
val (input ,expected) = it
115+
on("calling humanize on $input",{
116+
val actual = input.humanize(LetterCasing.AllCaps)
117+
it("should become ${it.component2()}",{
118+
shouldEqual(expected, actual)
119+
})
120+
})
121+
}
122+
123+
given("A word in all caps"){
124+
on("calling checkAllCaps", {
125+
it("should be true", {
126+
shouldEqual(true, "HTML".checkAllCaps())
127+
})
128+
})
129+
}
130+
131+
given("A word in all lowercase"){
132+
on("calling checkAllCaps", {
133+
it("should be false", {
134+
shouldEqual(false, "html".checkAllCaps())
135+
})
136+
})
137+
}
27138

28139
}}
29140

0 commit comments

Comments
 (0)