Skip to content

Commit 16b09e1

Browse files
Add String::capitalize and String::decapitalize`
1 parent bb7d279 commit 16b09e1

File tree

5 files changed

+41
-2
lines changed

5 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
### Added
66

7+
- `String::capitalize`
8+
- `String::decapitalize`
9+
710
### Changed
811

912
### Deprecated

kotlin-stdlib/api/android/kotlin-stdlib.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ public final class com/javiersc/kotlin/stdlib/AnsiColorsKt {
142142
}
143143

144144
public final class com/javiersc/kotlin/stdlib/CollectionsKt {
145+
public static final fun capitalize (Ljava/lang/String;)Ljava/lang/String;
146+
public static final fun decapitalize (Ljava/lang/String;)Ljava/lang/String;
145147
public static final fun fifth (Ljava/lang/Iterable;)Ljava/lang/Object;
146148
public static final fun fifthOrNull (Ljava/lang/Iterable;)Ljava/lang/Object;
147149
public static final fun forth (Ljava/lang/Iterable;)Ljava/lang/Object;

kotlin-stdlib/api/jvm/kotlin-stdlib.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ public final class com/javiersc/kotlin/stdlib/AnsiColorsKt {
142142
}
143143

144144
public final class com/javiersc/kotlin/stdlib/CollectionsKt {
145+
public static final fun capitalize (Ljava/lang/String;)Ljava/lang/String;
146+
public static final fun decapitalize (Ljava/lang/String;)Ljava/lang/String;
145147
public static final fun fifth (Ljava/lang/Iterable;)Ljava/lang/Object;
146148
public static final fun fifthOrNull (Ljava/lang/Iterable;)Ljava/lang/Object;
147149
public static final fun forth (Ljava/lang/Iterable;)Ljava/lang/Object;

kotlin-stdlib/common/main/kotlin/com/javiersc/kotlin/stdlib/Collections.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
package com.javiersc.kotlin.stdlib
44

5+
/**
6+
* Returns a copy of this string having its first letter title-cased using the rules of the default
7+
* locale, or the original string if it's empty or already starts with a title case letter.
8+
*
9+
* The title case of a character is usually the same as its upper case with several exceptions. The
10+
* particular list of characters with the special title case form depends on the underlying
11+
* platform.
12+
*/
13+
public inline fun String.capitalize(): String = replaceFirstChar {
14+
if (it.isLowerCase()) it.titlecase() else it.toString()
15+
}
16+
17+
/**
18+
* Returns a copy of this string having its first letter lowercased using the rules of the default
19+
* locale, or the original string if it's empty or already starts with a lower case letter.
20+
*/
21+
public inline fun String.decapitalize(): String = replaceFirstChar { it.lowercase() }
22+
523
/**
624
* Returns second element.
725
*

kotlin-stdlib/common/test/kotlin/com/javiersc/kotlin/stdlib/StringsTest.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ import kotlin.test.assertTrue
66

77
class StringsTest {
88

9+
@Test
10+
fun string_capitalize() {
11+
assertTrue { "hello".capitalize() == "Hello" }
12+
assertTrue { "Hello".capitalize() == "Hello" }
13+
assertTrue { "hElLo".capitalize() == "HElLo" }
14+
}
15+
16+
@Test
17+
fun string_decapitalize() {
18+
assertTrue { "hello".decapitalize() == "hello" }
19+
assertTrue { "Hello".decapitalize() == "hello" }
20+
assertTrue { "HElLo".decapitalize() == "hElLo" }
21+
}
22+
923
@Test
1024
fun string_remove() {
1125
assertTrue { "Hello, World".remove("Hello, ") == "World" }
@@ -41,15 +55,15 @@ class StringsTest {
4155
}
4256

4357
@Test
44-
fun remove_duplicate_empty_lines() {
58+
fun string_remove_duplicate_empty_lines() {
4559
assertTrue { "a\nb\n\n\nc\n".removeDuplicateEmptyLines() == "a\nb\n\nc\n" }
4660
assertTrue { "a\n\nb\n\n\nc\n".removeDuplicateEmptyLines() == "a\n\nb\n\nc\n" }
4761
assertTrue { "a\n\nb\n\n\nc\n\n".removeDuplicateEmptyLines() == "a\n\nb\n\nc\n" }
4862
assertTrue { "a\n\n\n\n\nb\n\n".removeDuplicateEmptyLines() == "a\n\nb\n" }
4963
}
5064

5165
@Test
52-
fun end_with_new_line() {
66+
fun string_end_with_new_line() {
5367
assertTrue { "a".endWithNewLine() == "a\n" }
5468
assertTrue { "a\n".endWithNewLine() == "a\n" }
5569
assertTrue { "".endWithNewLine() == "" }

0 commit comments

Comments
 (0)