is_dart_empty_or_not 0.2.2 copy "is_dart_empty_or_not: ^0.2.2" to clipboard
is_dart_empty_or_not: ^0.2.2 copied to clipboard

A Dart package that provides extensions for checking empty or zero values

is_dart_empty_or_not #

License: MIT Coverage Status Pub Version

A minimal Dart package providing concise extension methods for handling empty and zero values on core types (String, List, Set, Map, num, int, double, Duration).

  • Purpose: Simplifies fallback logic and conditional actions for empty/zero values.
  • Origin: Inspired by Dart empty patterns [String.isEmpty, List.isEmpty etc..], see more about it Dart non-nullable initialization pattern

Features #

  • Extension methods for String, List, Set, Map, num, int, double, Duration
  • Fallback and conditional logic in a single call
  • Zero dependencies

Quick Start #

# pubspec.yaml
dependencies:
  is_dart_empty_or_not: ^0.1.0
import 'package:is_dart_empty_or_not/is_dart_empty_or_not.dart';

final name = ''.whenEmptyUse('Anonymous'); // 'Anonymous'
final items = <int>[].whenEmptyUse([1, 2]); // [1, 2]
final value = 0.whenZeroUse(42); // 42

''.onNotEmpty((s) => print('Not empty!')); // (does nothing)

API Overview #

Type Method Signature / Description
String whenEmptyUse String whenEmptyUse(String value)
Returns the provided value if empty
String onNotEmpty void onNotEmpty(void Function(String))
Runs callback if not empty
String onEmpty void onEmpty(void Function(String))
Runs callback if empty
List whenEmptyUse List<T> whenEmptyUse(List<T> values)
Returns the provided value if empty
Set whenEmptyUse Set<T> whenEmptyUse(Set<T> values)
Returns the provided value if empty
Map whenEmptyUse Map<K,V> whenEmptyUse(Map<K,V> map)
Returns the provided value if empty
num/int/double whenZeroUse num whenZeroUse(num value)
Returns fallback if zero
num isZero bool get isZero
True if value is zero
num isPositive bool get isPositive
True if value is positive
Duration isZero bool get isZero
True if duration is zero
Duration whenZeroUse Duration whenZeroUse(Duration duration)
Returns fallback if zero
Duration isNegative bool get isNegative
True if duration is negative
Duration whenNegativeUse Duration whenNegativeUse(Duration duration)
Returns fallback if negative
Duration isPositive bool get isPositive
True if duration is positive

Examples #

String #

''.whenEmptyUse('default'); // 'default'
'foo'.whenEmptyUse('default'); // 'foo'

'bar'.onNotEmpty((s) => print(s)); // prints 'bar'
''.onEmpty(() => print('foo')); // prints 'foo'

List #

[].whenEmptyUse([1, 2]); // [1, 2]
[3].whenEmptyUse([1, 2]); // [3]

Set #

<Set<int>>{}.whenEmptyUse({1, 2}); // {1, 2}
{3}.whenEmptyUse({1, 2}); // {3}

Map #

<Map<int, String>>{}.whenEmptyUse({1: 'a'}); // {1: 'a'}
{2: 'b'}.whenEmptyUse({1: 'a'}); // {2: 'b'}

num/int/double #

0.whenZeroUse(42); // 42
1.whenZeroUse(42); // 1

0.0.whenZeroUse(3.14); // 3.14
2.7.whenZeroUse(3.14); // 2.7

(0).isZero; // true
(1).isZero; // false

(5).isPositive; // true
(-2).isPositive; // false

Duration #

Duration.zero.isZero; // true
Duration(seconds: 1).isZero; // false

Duration.zero.whenZeroUse(Duration(seconds: 5)); // 0:00:05.000000
Duration(seconds: 2).whenZeroUse(Duration(seconds: 5)); // 0:00:02.000000

Duration(seconds: -1).isNegative; // true
Duration(seconds: 1).isNegative; // false

Duration(seconds: -1).whenNegativeUse(Duration(seconds: 3)); // 0:00:03.000000
Duration(seconds: 2).whenNegativeUse(Duration(seconds: 3)); // 0:00:02.000000

Duration(seconds: 2).isPositive; // true
Duration(seconds: -1).isPositive; // false

Changelog #

See CHANGELOG.md

License #

This package is licensed under the MIT License - see the LICENSE file for details.

1
likes
150
points
238
downloads

Publisher

unverified uploader

Weekly Downloads

A Dart package that provides extensions for checking empty or zero values

Homepage

Documentation

API reference

License

MIT (license)

More

Packages that depend on is_dart_empty_or_not