Skip to content

Commit

Permalink
✨ add null check extensions for string
Browse files Browse the repository at this point in the history
  • Loading branch information
BirjuVachhani committed Oct 30, 2020
1 parent 6ca4fae commit 94588eb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/primitive/string.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ extension StringScrewdriver on String {
/// Returns true if [this] contains characters other than white-spaces.
bool get isNotBlank => trim().isNotEmpty;

/// Returns true if [this] is either null or empty string.
bool get isNullOrEmpty => this == null || isEmpty;

/// Returns true if [this] is neither null nor empty string.
bool get isNotNullOrEmpty => this != null && isNotEmpty;

/// Returns true if [this] is either null or blank string.
bool get isNullOrBlank => this == null || isBlank;

/// Returns true if [this] is neither null nor blank string.
bool get isNotNullOrBlank => this != null && isNotBlank;

/// Converts the first character of [this] to upper case.
String get capitalized {
if (isBlank) return this;
Expand Down
36 changes: 36 additions & 0 deletions test/string_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,42 @@ import 'package:screwdriver/screwdriver.dart';
import 'package:test/test.dart';

void main() {
test('isNullOrEmpty tests', () {
expect(''.isNullOrEmpty, true);
expect(' '.isNullOrEmpty, false);
String str;
expect(str.isNullOrEmpty, true);
str = 'abc';
expect(str.isNullOrEmpty, false);
});

test('isNotNullOrEmpty tests', () {
expect(''.isNotNullOrEmpty, false);
expect(' '.isNotNullOrEmpty, true);
String str;
expect(str.isNotNullOrEmpty, false);
str = 'abc';
expect(str.isNotNullOrEmpty, true);
});

test('isNullOrBlank tests', () {
expect(''.isNullOrBlank, true);
expect(' '.isNullOrBlank, true);
String str;
expect(str.isNullOrBlank, true);
str = 'abc';
expect(str.isNullOrBlank, false);
});

test('isNotNullOrBlank tests', () {
expect(''.isNotNullOrBlank, false);
expect(' '.isNotNullOrBlank, false);
String str;
expect(str.isNotNullOrBlank, false);
str = 'abc';
expect(str.isNotNullOrBlank, true);
});

test('isBlank & isNotBlank tests', () {
expect(''.isBlank, true);
expect(' '.isBlank, true);
Expand Down

0 comments on commit 94588eb

Please sign in to comment.