Skip to content

Commit e26dced

Browse files
author
Jens Becker
committed
feat(string_extensions): minor improvements
1 parent efcb6ed commit e26dced

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ The goal is that by using this package you have to write less (repeating) and mo
1818
Here is an quick overview over all extensions, helper classes and widgets:
1919

2020
- [Extensions on `String` and `String?`](#extensions-on-string-and-string)
21-
- [Extensions on `String?`](#extensions-on-string)
2221
- [Extensions on `DateTime`](#extensions-on-datetime)
2322
- [Extensions on `List<DateTime>`](#extensions-on-listdatetime)
2423
- [Extensions on `BuildContext` (Adaptive helpers)](#extensions-on-buildcontext-adaptive-helpers)
@@ -35,6 +34,11 @@ Here is an quick overview over all extensions, helper classes and widgets:
3534
All features with links to their page in the documentation are listed below:
3635

3736
- ### Extensions on `String` and `String?`:
37+
Note: All getters also do a null check if used on `String?`.
38+
- #### [isNotBlank](https://pub.dev/documentation/fleasy/latest/fleasy/StringExtensions/isNotBlank.html)
39+
Whether the string contains characters except of whitespace characters.
40+
- #### [isBlank](https://pub.dev/documentation/fleasy/latest/fleasy/StringExtensions/isBlank.html)
41+
Whether the string is either empty or solely made of whitespace characters.
3842
- #### [isEmail](https://pub.dev/documentation/fleasy/latest/fleasy/StringExtensions/isEmail.html)
3943
Whether the string is a valid email.
4044
- #### [isUrl](https://pub.dev/documentation/fleasy/latest/fleasy/StringExtensions/isUrl.html)
@@ -45,15 +49,8 @@ All features with links to their page in the documentation are listed below:
4549
Whether the string is a valid medium password.
4650
- #### [isStrongPassword](https://pub.dev/documentation/fleasy/latest/fleasy/StringExtensions/isStrongPassword.html)
4751
Whether the string is a valid strong password.
48-
49-
50-
- ### Extensions on `String?`:
51-
- #### [isNotBlank](https://pub.dev/documentation/fleasy/latest/fleasy/NullableStringExtensions/isNotBlank.html)
52-
Whether the string is not null nor empty.
53-
- #### [isBlank](https://pub.dev/documentation/fleasy/latest/fleasy/NullableStringExtensions/isBlank.html)
54-
Whether the string is null or empty.
5552
- #### [toNullIfBlank()](https://pub.dev/documentation/fleasy/latest/fleasy/NullableStringExtensions/toNullIfBlank.html)
56-
Returns null if the string is blank (empty or null) or it's text if not.
53+
Returns null if the string `isBlank` or it's text if it `isNotBlank`.
5754

5855

5956
- ### Extensions on `DateTime`:

lib/src/extensions/string_extensions.dart

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ final RegExp _strongPasswordRegExp =
1313
RegExp(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_])\S{8,}$');
1414

1515
extension StringExtensions on String {
16+
/// Whether the string contains characters except of whitespace characters.
17+
bool get isNotBlank => trim().isNotEmpty;
18+
19+
/// Whether the string is either empty or solely made of whitespace characters.
20+
bool get isBlank => trim().isEmpty;
21+
1622
/// Whether the string is a valid email.
1723
bool get isEmail => _emailRegExp.hasMatch(this);
1824

@@ -49,13 +55,13 @@ extension StringExtensions on String {
4955
}
5056

5157
extension NullableStringExtensions on String? {
52-
/// Whether the string is not null nor empty.
53-
bool get isNotBlank => this != null && this!.isNotEmpty;
58+
/// Whether the string is not null and contains characters except of whitespace characters.
59+
bool get isNotBlank => this != null && this!.trim().isNotEmpty;
5460

55-
/// Whether the string is null or empty.
61+
/// Whether the string is either null, empty or is solely made of whitespace characters.
5662
bool get isBlank => !isNotBlank;
5763

58-
/// Returns null if the string is blank (empty or null) or it's text if not.
64+
/// Returns null if the string [isBlank] or it's text if it [isNotBlank].
5965
String? toNullIfBlank() => isNotBlank ? this : null;
6066

6167
/// Whether the string is not null and a valid email.
@@ -64,18 +70,24 @@ extension NullableStringExtensions on String? {
6470
/// Whether the string is not null and a valid url.
6571
bool get isUrl => this != null ? this!.isUrl : false;
6672

67-
/// Whether the string is a valid medium password.
73+
/// Whether the string is not null and a valid easy password.
74+
///
75+
/// Requirements:
76+
/// - minimum 8 characters
77+
/// - no whitespaces
78+
bool get isEasyPassword => this != null ? this!.isEasyPassword : false;
79+
80+
/// Whether the string is not null and a valid medium password.
6881
///
6982
/// Requirements:
7083
/// - minimum 8 characters
7184
/// - no whitespaces
7285
/// - at least 1 uppercase letter
7386
/// - at least 1 lowercase letter
7487
/// - at least 1 number
75-
bool get isMediumPassword =>
76-
this != null ? _mediumPasswordRegExp.hasMatch(this!) : false;
88+
bool get isMediumPassword => this != null ? this!.isMediumPassword : false;
7789

78-
/// Whether the string is a valid strong password.
90+
/// Whether the string is is not null and a valid strong password.
7991
///
8092
/// Requirements:
8193
/// - minimum 8 characters
@@ -84,6 +96,5 @@ extension NullableStringExtensions on String? {
8496
/// - at least 1 lowercase letter
8597
/// - at least 1 number
8698
/// - at least 1 special character
87-
bool get isStrongPassword =>
88-
this != null ? _strongPasswordRegExp.hasMatch(this!) : false;
99+
bool get isStrongPassword => this != null ? this!.isStrongPassword : false;
89100
}

0 commit comments

Comments
 (0)