diff --git a/CHANGELOG.md b/CHANGELOG.md index b3016ee..70d7b1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 1.3.2 + +- Added `isEmptyOrNull` and `isNotEmptyOrNull` extensions for `List` and `String` objects. These extensions are identical to `isNullOrEmpty` and `isNotNullOrEmpty`, respectively. +- Fixed a bug in the `DateTime` extension that caused incorrect results when comparing dates using `isToday`. + ## 1.3.1 - Added the `isNullOrEmpty` and `isNotNullOrEmpty` extensions for `List` objects. diff --git a/lib/src/extensions/date_time.dart b/lib/src/extensions/date_time.dart index 2b53113..34527a3 100644 --- a/lib/src/extensions/date_time.dart +++ b/lib/src/extensions/date_time.dart @@ -89,7 +89,8 @@ extension DaysInMonth on DateTime { /// An extension on `DateTime` to check if a date is today or if it is the same day as another date. extension IsToday on DateTime { /// Returns `true` if the current date is today. - bool get isToday => DateTime.now().difference(this).inDays == 0; + bool get isToday => + DateTime.now().startOfDay.difference(this.startOfDay).inMilliseconds == 0; /// Returns `true` if the current date is the same day as [other]. bool isSameDayAs(DateTime other) => diff --git a/lib/src/extensions/list.dart b/lib/src/extensions/list.dart index 27c20b0..dd5d11e 100644 --- a/lib/src/extensions/list.dart +++ b/lib/src/extensions/list.dart @@ -52,7 +52,7 @@ extension Unique on List { extension ListNullability on List? { /// Returns `true` if the list is either not null and not empty. /// - /// This is the inverse of [isNullOrEmpty]. + /// This is the inverse of [isNotEmptyOrNull]. /// /// Example usage: /// ```dart @@ -65,8 +65,29 @@ extension ListNullability on List? { /// list = null; /// print(list.isNotNullOrEmpty); // Output: false /// ``` + /// + /// This is identical to [isNotNullOrEmpty]. bool get isNotNullOrEmpty => !isNullOrEmpty; + /// Returns `true` if the list is either not null and not empty. + /// + /// This is the inverse of [isEmptyOrNull]. + /// + /// Example usage: + /// ```dart + /// List? list = [1, 2, 3]; + /// print(list.isNotEmptyOrNull); // Output: true + /// + /// list = []; + /// print(list.isNotEmptyOrNull); // Output: false + /// + /// list = null; + /// print(list.isNotEmptyOrNull); // Output: false + /// ``` + /// + /// This is identical to [isNotNullOrEmpty]. + bool get isNotEmptyOrNull => !isNullOrEmpty; + /// Returns `true` if the list is either null or empty. /// /// Example usage: @@ -80,5 +101,24 @@ extension ListNullability on List? { /// list = [1, 2, 3]; /// print(list.isNullOrEmpty); // Output: false /// ``` + /// + /// This is identical to [isEmptyOrNull]. bool get isNullOrEmpty => this == null || this!.isEmpty; + + /// Returns `true` if the list is either null or empty. + /// + /// Example usage: + /// ```dart + /// List? list = null; + /// print(list.isEmptyOrNull); // Output: true + /// + /// list = []; + /// print(list.isEmptyOrNull); // Output: true + /// + /// list = [1, 2, 3]; + /// print(list.isEmptyOrNull); // Output: false + /// ``` + /// + /// This is identical to [isEmptyOrNull]. + bool get isEmptyOrNull => isNullOrEmpty; } diff --git a/lib/src/extensions/string.dart b/lib/src/extensions/string.dart index 378fab8..1e65f78 100644 --- a/lib/src/extensions/string.dart +++ b/lib/src/extensions/string.dart @@ -29,8 +29,26 @@ extension Nullability on String? { /// print("The string is not null and not empty"); /// } /// ``` + /// + /// This is identical to [isNotEmptyOrNull]. bool get isNotNullOrEmpty => !isNullOrEmpty; + /// Returns `true` if the `String?` is neither `null` nor contains only whitespace. + /// + /// This is useful for cases where you want to check if a nullable string has a valid + /// non-empty, non-whitespace value. + /// + /// Example usage: + /// ```dart + /// String? example = "Hello"; + /// if (example.isNotEmptyOrNull) { + /// print("The string is not null and not empty"); + /// } + /// ``` + /// + /// This is identical to [isNotNullOrEmpty]. + bool get isNotEmptyOrNull => isNotNullOrEmpty; + /// Returns `true` if the `String?` is either `null` or contains only whitespace. /// /// This is a handy utility to check if a nullable string is either not assigned @@ -43,7 +61,25 @@ extension Nullability on String? { /// print("The string is null or empty"); /// } /// ``` + /// + /// This is identical to [isEmptyOrNull]. bool get isNullOrEmpty => this == null || (this ?? "").trim().isEmpty; + + /// Returns `true` if the `String?` is either `null` or contains only whitespace. + /// + /// This is a handy utility to check if a nullable string is either not assigned + /// or effectively empty (including strings with only whitespace characters). + /// + /// Example usage: + /// ```dart + /// String? example = null; + /// if (example.isEmptyOrNull) { + /// print("The string is null or empty"); + /// } + /// ``` + /// + /// This is identical to [isNullOrEmpty]. + bool get isEmptyOrNull => isNullOrEmpty; } /// An extension on `String` to split the string into parts of a specified length. diff --git a/pubspec.yaml b/pubspec.yaml index 520d178..03bb249 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: arcane_helper_utils description: Provides a variety of helpful utilities and extensions for Flutter and Dart. -version: 1.3.1 +version: 1.3.2 repository: https://github.com/hanskokx/arcane_helper_utils issue_tracker: https://github.com/hanskokx/arcane_helper_utils/issues diff --git a/test/extensions/list_test.dart b/test/extensions/list_test.dart index 27b06f0..f094fc1 100644 --- a/test/extensions/list_test.dart +++ b/test/extensions/list_test.dart @@ -33,16 +33,19 @@ void main() { group("ListNullability", () { test("isNullOrEmpty returns true for null list", () { List? list; + expect(list.isEmptyOrNull, true); expect(list.isNullOrEmpty, true); }); test("isNullOrEmpty returns true for empty list", () { final List list = []; + expect(list.isEmptyOrNull, true); expect(list.isNullOrEmpty, true); }); test("isNullOrEmpty returns false for non-empty list", () { final list = [1, 2, 3]; + expect(list.isEmptyOrNull, false); expect(list.isNullOrEmpty, false); }); @@ -51,9 +54,13 @@ void main() { final emptyList = []; final nonEmptyList = [1, 2, 3]; - expect(nullList.isNotNullOrEmpty, false); - expect(emptyList.isNotNullOrEmpty, false); - expect(nonEmptyList.isNotNullOrEmpty, true); + expect(nullList.isEmptyOrNull, !nullList.isNotEmptyOrNull); + expect(emptyList.isEmptyOrNull, !emptyList.isNotEmptyOrNull); + expect(nonEmptyList.isEmptyOrNull, !nonEmptyList.isNotEmptyOrNull); + + expect(nullList.isNullOrEmpty, !nullList.isNotNullOrEmpty); + expect(emptyList.isNullOrEmpty, !emptyList.isNotNullOrEmpty); + expect(nonEmptyList.isNullOrEmpty, !nonEmptyList.isNotNullOrEmpty); }); }); }