Skip to content

Commit cdfd5a8

Browse files
committed
Strings cleaner > only capitalize if string was lower-case before
1 parent 147440e commit cdfd5a8

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

src/main/java/org/mtransit/commons/CleanUtils.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,20 @@ public static String cleanLabel(@NotNull String label) {
9191

9292
@NotNull
9393
public static String cleanLabel(@NotNull Locale locale, @NotNull String label) {
94+
return cleanLabel(locale, label, true);
95+
}
96+
97+
@NotNull
98+
public static String cleanLabel(@NotNull Locale locale, @NotNull String label, boolean capitalize) {
9499
label = CLEAN_SPACES.matcher(label).replaceAll(SPACE);
95100
label = CLEAN_P1.matcher(label).replaceAll(CLEAN_P1_REPLACEMENT);
96101
label = CLEAN_P2.matcher(label).replaceAll(CLEAN_P2_REPLACEMENT);
97-
if (locale.getLanguage().equals(Locale.FRENCH.getLanguage())) {
98-
label = WordUtils.capitalize(label, CAPITALIZE_CHARS_FR);
99-
} else {
100-
label = WordUtils.capitalize(label, CAPITALIZE_CHARS_EN);
102+
if (capitalize) {
103+
if (locale.getLanguage().equals(Locale.FRENCH.getLanguage())) {
104+
label = WordUtils.capitalize(label, CAPITALIZE_CHARS_FR);
105+
} else {
106+
label = WordUtils.capitalize(label, CAPITALIZE_CHARS_EN);
107+
}
101108
}
102109
label = removePointsI(label); // after capitalize
103110
return label.trim();

src/main/java/org/mtransit/commons/StringsCleaner.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ object StringsCleaner {
149149
string = CleanUtils.ALL_CHARS_REGEX.replace(string, CleanUtils.ALL_CHARS_REGEX_REPLACEMENT)
150150
}
151151
}
152+
val capitalize = lowerUCStrings || lowerUCWords // only capitalize if lower case was called
152153
languages?.forEach { language ->
153154
if (short && string.length > shortMaxLength) {
154155
string = CleanUtils.cleanBounds(language, string)
155156
}
156-
string = CleanUtils.cleanLabel(language, string)
157+
string = CleanUtils.cleanLabel(language, string, capitalize)
157158
}
158159
return string
159160
}

src/test/java/org/mtransit/commons/StringsCleanerTests.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.mtransit.commons
22

3+
import java.util.Locale
34
import kotlin.test.BeforeTest
45
import kotlin.test.Test
56
import kotlin.test.assertEquals
@@ -39,4 +40,29 @@ class StringsCleanerTests {
3940
assertEquals("Angora/Gascon/Terrebonne/Cégep Terrebonne", result)
4041
}
4142
}
43+
44+
@Test
45+
fun test_cleanRouteLongName() {
46+
"Tunney's Pasture <> Bridlewood".let {
47+
StringsCleaner.cleanRouteLongName(it, languages = listOf(Locale.ENGLISH), routeType = 3)
48+
}.let { result ->
49+
assertEquals("Tunney's Pasture <> Bridlewood", result)
50+
}
51+
"Tunney's Pasture <> Bridlewood".let {
52+
StringsCleaner.cleanRouteLongName(it, languages = listOf(Locale.FRENCH, Locale.ENGLISH), routeType = 3)
53+
}.let { result ->
54+
assertEquals("Tunney's Pasture <> Bridlewood", result)
55+
}
56+
"Tunney's Pasture <> Bridlewood".let {
57+
StringsCleaner.cleanRouteLongName(it, languages = listOf(Locale.ENGLISH, Locale.FRENCH), routeType = 3)
58+
}.let { result ->
59+
assertEquals("Tunney's Pasture <> Bridlewood", result)
60+
}
61+
"Tunney's Pasture <> Bridlewood".let {
62+
StringsCleaner.cleanRouteLongName(it, languages = listOf(Locale.ENGLISH, Locale.FRENCH), routeType = 3, lowerUCWords = true)
63+
}.let { result ->
64+
assertEquals("Tunney'S Pasture <> Bridlewood", result) // too bad
65+
}
66+
67+
}
4268
}

0 commit comments

Comments
 (0)