-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathcamel_case.dart
54 lines (46 loc) · 1.2 KB
/
camel_case.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
extension StringExt on String {
String capitalize() {
return '${substring(0, 1).toUpperCase()}${substring(1)}';
}
String camelCase() {
final words = _intoWords(this)
.map((w) =>
'${w.substring(0, 1).toUpperCase()}${w.substring(1).toLowerCase()}')
.toList();
words[0] = words[0].toLowerCase();
return words.join();
}
}
List<String> _intoWords(String path) {
final _symbols = [
' ',
'.',
'/',
'_',
'\\',
'-',
];
final _upperAlphaRegex = RegExp(r'[A-Z]');
final _lowerAlphaRegex = RegExp(r'[a-z]');
final buffer = StringBuffer();
final words = <String>[];
for (var i = 0; i < path.length; i++) {
final char = String.fromCharCode(path.codeUnitAt(i));
final nextChar = i + 1 == path.length
? null
: String.fromCharCode(path.codeUnitAt(i + 1));
if (_symbols.contains(char)) {
continue;
}
buffer.write(char);
final isEndOfWord = nextChar == null ||
(_upperAlphaRegex.hasMatch(nextChar) &&
path.contains(_lowerAlphaRegex)) ||
_symbols.contains(nextChar);
if (isEndOfWord) {
words.add(buffer.toString());
buffer.clear();
}
}
return words;
}