Skip to content

Commit

Permalink
v2.5.5
Browse files Browse the repository at this point in the history
- Change `isEmptyValue` to is `isEmptyObject`.
- Added `isNotEmptyObject`.
  • Loading branch information
gmpassos committed Jul 13, 2020
1 parent f6a44dd commit f4c85f2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.5.5

- Change `isEmptyValue` to is `isEmptyObject`.
- Added `isNotEmptyObject`.

## 2.5.4

- Fix `MimeType.parse` when parameter `mimeType` is empty and `defaultMimeType` is null.
Expand Down
37 changes: 26 additions & 11 deletions lib/src/collections.dart
Original file line number Diff line number Diff line change
Expand Up @@ -760,18 +760,33 @@ K findKeyName<K, V>(Map<K, V> map, List<K> keys, [bool ignoreCase]) {
return entry != null ? entry.key : null;
}

/// Returns [true] if [value] is empty. Checks for [String], [List], [Map]
/// [Iterable] and [Set].
bool isEmptyValue<T>(T value) {
if (value == null) return true;
/// Returns [true] if [o] is empty. Checks for [String], [List], [Map]
/// [Iterable], [Set] or `o.toString()`.
bool isEmptyObject<T>(T o) {
if (o == null) return true;

if (value is String && value.isEmpty) return true;
if (value is List && value.isEmpty) return true;
if (value is Map && value.isEmpty) return true;
if (value is Iterable && value.isEmpty) return true;
if (value is Set && value.isEmpty) return true;
if (o is String) {
return o.isEmpty;
}
else if (o is List) {
return o.isEmpty;
}
else if (o is Map) {
return o.isEmpty;
}
else if (o is Iterable) {
return o.isEmpty;
}
else if (o is Set) {
return o.isEmpty;
} else {
return o.toString().isEmpty ;
}
}

return false;
/// Returns ![isEmptyObject].
bool isNotEmptyObject<T>(T value) {
return !isEmptyObject(value) ;
}

typedef ValueValidator<V> = bool Function(V value);
Expand All @@ -781,7 +796,7 @@ T resolveValue<T>(T value, T def, [ValueValidator valueValidator]) {
if (value == null) return def;
if (def == null) return value;

valueValidator ??= isEmptyValue;
valueValidator ??= isNotEmptyObject;
var valid = valueValidator(value) ?? true;
return valid ? value : def;
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: swiss_knife
description: Dart Useful Tools - collections, math, date, uri, json, events, resources, regexp, etc...
version: 2.5.4
version: 2.5.5
homepage: https://github.com/gmpassos/swiss_knife

environment:
Expand Down

0 comments on commit f4c85f2

Please sign in to comment.