/
recase.dart
134 lines (93 loc) · 3.3 KB
/
recase.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/// An instance of text to be re-cased.
class ReCase {
final RegExp _upperAlphaRegex = RegExp(r'[A-Z]');
final symbolSet = {' ', '.', '/', '_', '\\', '-'};
late String originalText;
late List<String> _words;
ReCase(String text) {
this.originalText = text;
this._words = _groupIntoWords(text);
}
List<String> _groupIntoWords(String text) {
StringBuffer sb = StringBuffer();
List<String> words = [];
bool isAllCaps = text.toUpperCase() == text;
for (int i = 0; i < text.length; i++) {
String char = text[i];
String? nextChar = i + 1 == text.length ? null : text[i + 1];
if (symbolSet.contains(char)) {
continue;
}
sb.write(char);
bool isEndOfWord = nextChar == null ||
(_upperAlphaRegex.hasMatch(nextChar) && !isAllCaps) ||
symbolSet.contains(nextChar);
if (isEndOfWord) {
words.add(sb.toString());
sb.clear();
}
}
return words;
}
/// camelCase
String get camelCase => _getCamelCase();
/// CONSTANT_CASE
String get constantCase => _getConstantCase();
/// Sentence case
String get sentenceCase => _getSentenceCase();
/// snake_case
String get snakeCase => _getSnakeCase();
/// dot.case
String get dotCase => _getSnakeCase(separator: '.');
/// param-case
String get paramCase => _getSnakeCase(separator: '-');
/// path/case
String get pathCase => _getSnakeCase(separator: '/');
/// PascalCase
String get pascalCase => _getPascalCase();
/// Header-Case
String get headerCase => _getPascalCase(separator: '-');
/// Title Case
String get titleCase => _getPascalCase(separator: ' ');
String _getCamelCase({String separator = ''}) {
List<String> words = this._words.map(_upperCaseFirstLetter).toList();
if (_words.isNotEmpty) {
words[0] = words[0].toLowerCase();
}
return words.join(separator);
}
String _getConstantCase({String separator = '_'}) {
List<String> words = this._words.map((word) => word.toUpperCase()).toList();
return words.join(separator);
}
String _getPascalCase({String separator = ''}) {
List<String> words = this._words.map(_upperCaseFirstLetter).toList();
return words.join(separator);
}
String _getSentenceCase({String separator = ' '}) {
List<String> words = this._words.map((word) => word.toLowerCase()).toList();
if (_words.isNotEmpty) {
words[0] = _upperCaseFirstLetter(words[0]);
}
return words.join(separator);
}
String _getSnakeCase({String separator = '_'}) {
List<String> words = this._words.map((word) => word.toLowerCase()).toList();
return words.join(separator);
}
String _upperCaseFirstLetter(String word) {
return '${word.substring(0, 1).toUpperCase()}${word.substring(1).toLowerCase()}';
}
}
extension StringReCase on String {
String get camelCase => ReCase(this).camelCase;
String get constantCase => ReCase(this).constantCase;
String get sentenceCase => ReCase(this).sentenceCase;
String get snakeCase => ReCase(this).snakeCase;
String get dotCase => ReCase(this).dotCase;
String get paramCase => ReCase(this).paramCase;
String get pathCase => ReCase(this).pathCase;
String get pascalCase => ReCase(this).pascalCase;
String get headerCase => ReCase(this).headerCase;
String get titleCase => ReCase(this).titleCase;
}