Skip to content
This repository was archived by the owner on Oct 12, 2024. It is now read-only.

Commit 7a5ea24

Browse files
committed
Update
1 parent 16ec4c4 commit 7a5ea24

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

lib/src/strings/src/string_case_conversions_on_string_extension.dart

+26-24
Original file line numberDiff line numberDiff line change
@@ -39,49 +39,53 @@ extension StringCaseConversionsOnStringExtension on String {
3939
String toUpperDotCase() => this.toDotCase().toUpperCase();
4040

4141
/// Converts the string to path/case.
42-
String toPathCase([String separator = '/']) =>
43-
this.extractLowercaseComponents().join(separator);
42+
String toPathCase([String separator = '/']) => this.extractLowercaseComponents().join(separator);
4443

4544
/// Converts the string to camelCase.
4645
String toCamelCase() => this.toPascalCase().withFirstLetterAsLowerCase();
4746

4847
/// Converts the string to PascalCase.
49-
String toPascalCase() =>
50-
this.extractLowercaseComponents().map((e) => e.capitalize()).join();
48+
String toPascalCase() => this.extractLowercaseComponents().map((e) => e.capitalize()).join();
5149

5250
/// Extracts and returns a list of lowercase components from the string.
5351
///
5452
/// This method identifies components based on transitions between lowercase
5553
/// and uppercase letters, between letters and digits, within sequences of
56-
/// uppercase letters, and at any non-alphanumeric characters.
57-
/// Each identified component is converted to lowercase.
54+
/// uppercase letters (including [special] characters), and at any
55+
/// non-alphanumeric characters. Each identified component is converted to
56+
/// lowercase.
5857
///
5958
/// The method is useful for parsing strings formatted in camelCase, PascalCase,
6059
/// snake_case, kebab-case, or other mixed-case styles into a list of lowercase words or segments.
6160
///
6261
/// Example:
6362
/// ```dart
64-
/// var example = 'HelloWorld123';
65-
/// var components = example.extractLowercaseComponents();
66-
/// print(components); // Output: ['hello', 'world', '123']
63+
/// var example = 'HelloWorld123+456';
64+
/// var components = example.extractLowercaseComponents(special = const {'+'});
65+
/// print(components); // Output: ['hello', 'world', '123+456']
6766
/// ```
68-
List<String> extractLowercaseComponents() {
67+
List<String> extractLowercaseComponents({
68+
Set<String> special = const {'+'},
69+
}) {
6970
if (this.isEmpty) return [this];
7071
final words = <String>[];
7172
var currentWord = StringBuffer();
7273
String? a;
7374
for (var n = 0; n < this.length; n++) {
7475
final b = this[n];
75-
if (b.isLetter || b.isDigit) {
76-
if (a != null &&
77-
((a.isLowerCase && b.isUpperCase) ||
78-
(a.isDigit && b.isLetter) ||
79-
(a.isLetter && b.isDigit) ||
80-
(a.isUpperCase &&
81-
b.isUpperCase &&
82-
(n + 1 < this.length && this[n + 1].isLowerCase)))) {
83-
words.add(currentWord.toString().toLowerCase());
84-
currentWord = StringBuffer();
76+
final bIsLetter = b.isLetter || special.contains(b);
77+
if (bIsLetter || b.isDigit) {
78+
if (a != null) {
79+
final aIsLetter = a.isLetter || special.contains(a);
80+
if ((a.isLowerCase && b.isUpperCase) ||
81+
(a.isDigit && bIsLetter) ||
82+
(aIsLetter && b.isDigit) ||
83+
(a.isUpperCase &&
84+
b.isUpperCase &&
85+
(n + 1 < this.length && this[n + 1].isLowerCase))) {
86+
words.add(currentWord.toString().toLowerCase());
87+
currentWord = StringBuffer();
88+
}
8589
}
8690
currentWord.write(b);
8791
} else if (currentWord.isNotEmpty) {
@@ -103,12 +107,10 @@ extension StringCaseConversionsOnStringExtension on String {
103107
bool get isLetter => RegExp(r'^[a-zA-Z]$').hasMatch(this);
104108

105109
/// Returns `true` if the string is all uppercase.
106-
bool get isUpperCase =>
107-
this == this.toUpperCase() && this != this.toLowerCase();
110+
bool get isUpperCase => this == this.toUpperCase() && this != this.toLowerCase();
108111

109112
/// Returns `true` if the string is all lowercase.
110-
bool get isLowerCase =>
111-
this == this.toLowerCase() && this != this.toUpperCase();
113+
bool get isLowerCase => this == this.toLowerCase() && this != this.toUpperCase();
112114

113115
/// Capitalizes the first letter of the string.
114116
///

0 commit comments

Comments
 (0)