Skip to content

Commit 3de549f

Browse files
committed
fix: capitalization logic
1 parent 9ec3e21 commit 3de549f

4 files changed

Lines changed: 12 additions & 4 deletions

File tree

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ packages:
1515
path: ".."
1616
relative: true
1717
source: path
18-
version: "1.2.1"
18+
version: "1.2.2"
1919
sdks:
2020
dart: ">=3.4.0 <4.0.0"

lib/src/case/case.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ extension Case on String {
1515
? preserveAcronym
1616
? (word.isUpperCase() ? word : word.toLowerCase())
1717
: word.toLowerCase()
18-
: word.capitalize(),
18+
: word.isUpperCase()
19+
? word
20+
: word.capitalize(),
1921
)
2022
.toList()
2123
.join();
@@ -26,7 +28,11 @@ extension Case on String {
2628
/// uppercase. It is set to `false` by default
2729
String capitalize({bool capitalizeAll = false}) => switch (capitalizeAll) {
2830
true => toUpperCase(),
29-
false => isEmpty ? '' : this[0].toUpperCase() + substring(1),
31+
false => isEmpty
32+
? ''
33+
: this[0] == this[0].toUpperCase()
34+
? this[0].toUpperCase() + substring(1).toLowerCase()
35+
: this[0].toUpperCase() + substring(1),
3036
};
3137

3238
/// Decapitalizes the first character of the string

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: stringr
22
description: Comprehensive string manipulation plugin for dart. Handles operations on latin, non latin and grapheme clusters alike! Features inspured from VocaJs
3-
version: 1.2.1
3+
version: 1.2.2
44
homepage: https://github.com/Chinmay-KB/stringr
55

66
environment:

test/case/case_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ void main() {
2626
expect("excuse moi".capitalize(capitalizeAll: true), "EXCUSE MOI");
2727
expect("".capitalize(), "");
2828
expect("macBook".capitalize(), "MacBook");
29+
expect("HELLO".capitalize(), "Hello");
30+
expect("HeLLo".capitalize(), "Hello");
2931
expect("яблоко".capitalize(), "Яблоко");
3032
expect("420".capitalize(), "420");
3133
expect("".capitalize(), "");

0 commit comments

Comments
 (0)