Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#150 add String.isNullOrBlank and String.isNotNullOrBlank #151

Merged
merged 1 commit into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,30 @@ final isBlank = ' '.isNullOrEmpty; // false
final isLineBreak = '\n'.isNullOrEmpty; // false
```

### .isNullOrBlank

Returns `true` if the String is either `null` or blank.

```dart
final isNull = null.isNullOrBlank; // true
final isEmpty = ''.isNullOrBlank; // true
final isBlank = ' '.isNullOrBlank; // true
final isLineBreak = '\n'.isNullOrBlank; // true
final isFoo = ' foo '.isNullOrBlank; // false
```

### .isNotNullOrBlank

Returns `true` if the String is neither `null` nor blank.

```dart
final isNull = null.isNullOrBlank; // true
final isEmpty = ''.isNullOrBlank; // true
final isBlank = ' '.isNullOrBlank; // true
final isLineBreak = '\n'.isNullOrBlank; // true
final isFoo = ' foo '.isNullOrBlank; // true
```

### .isUpperCase

Returns `true` if the entire string is upper case.
Expand Down
10 changes: 10 additions & 0 deletions lib/src/string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,13 @@ extension NullableStringIsNotNullOrEmptyExtension on String? {
/// Returns `true` if the string is neither null nor empty.
bool get isNotNullOrEmpty => !isNullOrEmpty;
}

extension NullableStringIsNullOrBlankExtension on String? {
/// Returns `true` if the string is either `null` or blank.
bool get isNullOrBlank => this?.isBlank ?? true;
}

extension NullableStringIsNotNullOrBlankExtension on String? {
/// Returns `true` if the string is neither null nor blank.
bool get isNotNullOrBlank => !isNullOrBlank;
}
16 changes: 16 additions & 0 deletions test/string_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@ void main() {
expect('\n'.isNotNullOrEmpty, true);
});

test('.isNullOrBlank', () {
expect(null.isNullOrBlank, true);
expect(''.isNullOrBlank, true);
expect(' '.isNullOrBlank, true);
expect('\n'.isNullOrBlank, true);
expect(' foo '.isNullOrBlank, false);
});

test('.isNotNullOrBlank', () {
expect(null.isNotNullOrBlank, false);
expect(''.isNotNullOrBlank, false);
expect(' '.isNotNullOrBlank, false);
expect('\n'.isNotNullOrBlank, false);
expect(' foo '.isNotNullOrBlank, true);
});

test('.toUtf8()', () {
expect(''.toUtf8(), []);
expect('hello'.toUtf8(), [104, 101, 108, 108, 111]);
Expand Down