Skip to content

Commit

Permalink
add custom function
Browse files Browse the repository at this point in the history
  • Loading branch information
warrenrhodes committed Jun 17, 2024
1 parent c0e2fb0 commit 6af81b9
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 1.1.2

- Updates some function.

## 1.1.1

- Downgrade the `intl`version to be compatible with many package.

## 1.1.0

- Adds custom print function `fprint`.
Expand Down
29 changes: 20 additions & 9 deletions lib/src/string_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ extension StringFormat on String {
/// ```
String get toCamelCase {
if (isEmpty) return this;
String camelCaseString = toLowerCase();
String camelCaseString =
replaceAllMapped(RegExp(r'\s'), (match) => '').toLowerCase();
final regExp = RegExp(r'[_-](\w)');
camelCaseString = camelCaseString.replaceAllMapped(regExp, (match) {
return match.group(1)!.toUpperCase();
Expand All @@ -285,10 +286,12 @@ extension StringFormat on String {
/// ```
String get toKebabCase {
if (isEmpty) return this;
return replaceAllMapped(
RegExp(r'([a-z])([A-Z])'),
(match) => '${match[1]}-${match[2]}',
).toLowerCase();
return replaceAllMapped(RegExp(r'\s'), (match) => '')
.replaceAllMapped(
RegExp(r'([a-z])([A-Z])'),
(match) => '${match[1]}-${match[2]}',
)
.toLowerCase();
}

/// Converts a camelCase string to snake_case.
Expand All @@ -299,7 +302,8 @@ extension StringFormat on String {
/// print(str.toSnakeCase); // "camel_case_example"
/// ```
String get toSnakeCase {
return replaceAllMapped(RegExp(r'[A-Z]'), (match) {
return replaceAllMapped(RegExp(r'\s'), (match) => '')
.replaceAllMapped(RegExp(r'[A-Z]'), (match) {
return '_${match.group(0)!.toLowerCase()}';
}).replaceFirstMapped(RegExp(r'^_'), (match) => '');
}
Expand Down Expand Up @@ -481,13 +485,20 @@ extension StringFormat on String {
///
/// final String url3 = 'example.com';
/// final bool isValidUrl3 = url3.isValidUrl;
/// print(isValidUrl3); // Prints false
/// print(isValidUrl3); // Prints true
/// ```
///
/// Returns:
/// - `true` if the string is a valid URL.
/// - `false` otherwise.
bool get isUrl => RegExp(
r"^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$")
bool get isUrl => RegExp(r'^(([\w+-.]+):\/\/)?'
r'((([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})|'
r'localhost|'
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|'
r'\[[0-9a-fA-F:]+\])'
r'(:\d+)?'
r'(\/[-a-zA-Z0-9@:%._\+~#=]*)*'
r'(\?[;&a-zA-Z0-9%_.~+=-]*)?'
r'(\#[-a-zA-Z0-9_]*)?$')
.hasMatch(this);
}
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dart_advanced_utils
description: A Dart advanced utils package enhances the default dart object by adding powerful and flexible methods.
version: 1.1.0
version: 1.1.2
repository: https://github.com/warrenrhodes/dart_utils
issue_tracker: https://github.com/warrenrhodes/dart_utils/issues

Expand All @@ -10,7 +10,7 @@ environment:
dependencies:
clock: ^1.1.1
collection: ^1.18.0
intl: ^0.19.0
intl: ^0.18.1

dev_dependencies:
lints: ^3.0.0
Expand Down
9 changes: 9 additions & 0 deletions test/string_extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,14 @@ void main() {
expect('Hello world, this is a test.'.wordCount, 6);
expect('Hello'.wordCount, 1);
});
test('isValidUrl', () {
expect('https://www.example.com'.isUrl, true);
expect('ftp://example.com'.isUrl, true);
expect('example.com'.isUrl, true);
expect('invalid_url'.isUrl, false);
expect('http://[2001:db8::1]:8080'.isUrl, true);
expect('192.168.1'.isUrl, false);
expect('192.168.1.1'.isUrl, true);
});
});
}

0 comments on commit 6af81b9

Please sign in to comment.