@@ -39,49 +39,53 @@ extension StringCaseConversionsOnStringExtension on String {
39
39
String toUpperDotCase () => this .toDotCase ().toUpperCase ();
40
40
41
41
/// 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);
44
43
45
44
/// Converts the string to camelCase.
46
45
String toCamelCase () => this .toPascalCase ().withFirstLetterAsLowerCase ();
47
46
48
47
/// 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 ();
51
49
52
50
/// Extracts and returns a list of lowercase components from the string.
53
51
///
54
52
/// This method identifies components based on transitions between lowercase
55
53
/// 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.
58
57
///
59
58
/// The method is useful for parsing strings formatted in camelCase, PascalCase,
60
59
/// snake_case, kebab-case, or other mixed-case styles into a list of lowercase words or segments.
61
60
///
62
61
/// Example:
63
62
/// ```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 ']
67
66
/// ```
68
- List <String > extractLowercaseComponents () {
67
+ List <String > extractLowercaseComponents ({
68
+ Set <String > special = const {'+' },
69
+ }) {
69
70
if (this .isEmpty) return [this ];
70
71
final words = < String > [];
71
72
var currentWord = StringBuffer ();
72
73
String ? a;
73
74
for (var n = 0 ; n < this .length; n++ ) {
74
75
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
+ }
85
89
}
86
90
currentWord.write (b);
87
91
} else if (currentWord.isNotEmpty) {
@@ -103,12 +107,10 @@ extension StringCaseConversionsOnStringExtension on String {
103
107
bool get isLetter => RegExp (r'^[a-zA-Z]$' ).hasMatch (this );
104
108
105
109
/// 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 ();
108
111
109
112
/// 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 ();
112
114
113
115
/// Capitalizes the first letter of the string.
114
116
///
0 commit comments