diff --git a/lib/primitive/string.dart b/lib/primitive/string.dart index 7809482..e778f58 100644 --- a/lib/primitive/string.dart +++ b/lib/primitive/string.dart @@ -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; diff --git a/test/string_test.dart b/test/string_test.dart index c8a9f31..7275cb4 100644 --- a/test/string_test.dart +++ b/test/string_test.dart @@ -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);