Skip to content

Commit 1923885

Browse files
author
Jens Becker
committed
feat(string_extensions): add addHttps()
1 parent 9ab0c6f commit 1923885

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ All features with links to their page in the documentation:
5050
Whether the string is a valid strong password.
5151
- #### [toNullIfBlank()](https://pub.dev/documentation/fleasy/latest/fleasy/NullableStringExtensions/toNullIfBlank.html)
5252
Returns null if the string `isBlank` or it's text if it `isNotBlank`.
53+
- #### [addHttps()](https://pub.dev/documentation/fleasy/latest/fleasy/StringExtensions/addHttps.html)
54+
Adds https:// to the link if it does not contain https:// or http:// already. This is helpful to make a link openable when using the [url_launcher](https://pub.dev/packages/url_launcher) package for example.
5355

5456

5557
- ### Extensions on `DateTime`:

lib/src/extensions/string_extensions.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ extension StringExtensions on String {
5252
/// - at least 1 number
5353
/// - at least 1 special character
5454
bool get isStrongPassword => _strongPasswordRegExp.hasMatch(this);
55+
56+
/// Adds https:// to the link if it does not contain https:// or http:// already..
57+
///
58+
/// This is helpful to make a link openable when using the [url_launcher](https://pub.dev/packages/url_launcher) package for example.
59+
String addHttps() {
60+
return contains('https://') || contains('http://') ? this : 'https://$this';
61+
}
5562
}
5663

5764
extension NullableStringExtensions on String? {

test/string_extensions_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,24 @@ void main() {
159159
expect(password.isStrongPassword, equals(false));
160160
});
161161
});
162+
163+
group('addHttps', () {
164+
test('adds https:// when there is no http:// or https://.', () {
165+
const url = 'www.jensbecker.dev';
166+
167+
expect(url.addHttps(), equals('https://www.jensbecker.dev'));
168+
});
169+
170+
test('does not add https:// when there is http:// already.', () {
171+
const url = 'http://www.jensbecker.dev';
172+
173+
expect(url.addHttps(), equals(url));
174+
});
175+
176+
test('does not add https:// when there is https:// already.', () {
177+
const url = 'https://www.jensbecker.dev';
178+
179+
expect(url.addHttps(), equals(url));
180+
});
181+
});
162182
}

0 commit comments

Comments
 (0)