Skip to content

Commit

Permalink
Merge pull request #151 from komape/master
Browse files Browse the repository at this point in the history
#150 add String.isNullOrBlank and String.isNotNullOrBlank
  • Loading branch information
passsy committed Apr 12, 2022
2 parents b781ef5 + 430cb04 commit cd6c4e2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,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

0 comments on commit cd6c4e2

Please sign in to comment.