[analyzer_utilities] Add String.isPascalCase extension.
This will be used in a follow-up CL to recognize changes to the format of `pkg/front_end/messages.yaml` and `pkg/_fe_analyzer_shared/messages.yaml`. Change-Id: I6a6a6964274aa116d9f0051ce7c099a9b8d84277 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/466444 Commit-Queue: Paul Berry <paulberry@google.com> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
extension StringExtension on String {
|
||||
static final _toSnakeCaseRegExp = RegExp('_?[A-Z]');
|
||||
static final _startsWithLowerCaseRegExp = RegExp('^[a-z]');
|
||||
static final _startsWithUpperCaseRegExp = RegExp('^[A-Z]');
|
||||
|
||||
/// Returns `true` if the string is a `camelCase` string.
|
||||
bool get isCamelCase {
|
||||
@@ -13,6 +14,13 @@ extension StringExtension on String {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Returns `true` if the string is a `PascalCase` string.
|
||||
bool get isPascalCase {
|
||||
if (contains('_')) return false;
|
||||
if (_startsWithUpperCaseRegExp.matchAsPrefix(this) == null) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Converts `SCREAMING_SNAKE_CASE` or `snake_case` to `camelCase`.
|
||||
String toCamelCase() {
|
||||
var parts = toLowerCase().split('_');
|
||||
|
||||
@@ -22,11 +22,26 @@ class StringExtensionTest {
|
||||
expect('UPPER_CASE_WITH_UNDERSCORES'.isCamelCase, false);
|
||||
expect('lower_case_with_underscores'.isCamelCase, false);
|
||||
expect('camelCase'.isCamelCase, true);
|
||||
expect('PascalCase'.isCamelCase, false);
|
||||
expect('alllowercase'.isCamelCase, true);
|
||||
expect('ALLUPPERCASE'.isCamelCase, false);
|
||||
expect('foo123Bar'.isCamelCase, true);
|
||||
expect('Foo123Bar'.isCamelCase, false);
|
||||
expect('123'.isCamelCase, false);
|
||||
}
|
||||
|
||||
void test_isPascalCase() {
|
||||
expect('UPPER_CASE_WITH_UNDERSCORES'.isPascalCase, false);
|
||||
expect('lower_case_with_underscores'.isPascalCase, false);
|
||||
expect('camelCase'.isPascalCase, false);
|
||||
expect('PascalCase'.isPascalCase, true);
|
||||
expect('alllowercase'.isPascalCase, false);
|
||||
expect('ALLUPPERCASE'.isPascalCase, true);
|
||||
expect('foo123Bar'.isPascalCase, false);
|
||||
expect('Foo123Bar'.isPascalCase, true);
|
||||
expect('123'.isPascalCase, false);
|
||||
}
|
||||
|
||||
void test_toCamelCase() {
|
||||
expect('CAMEL_CASE'.toCamelCase(), 'camelCase');
|
||||
expect('alreadyCamel_case'.toCamelCase(), 'alreadycamelCase');
|
||||
|
||||
Reference in New Issue
Block a user