This commit is contained in:
2025-03-25 16:20:49 +01:00
parent d2bb65a91e
commit f1586abc86
6 changed files with 95 additions and 6 deletions
+2 -1
View File
@@ -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) =>
+41 -1
View File
@@ -52,7 +52,7 @@ extension Unique<E, Id> on List<E> {
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<int>? 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<int>? 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;
}
+36
View File
@@ -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.