diff --git a/lib/src/option.dart b/lib/src/option.dart index 10bd38e..a2479ba 100644 --- a/lib/src/option.dart +++ b/lib/src/option.dart @@ -2,6 +2,8 @@ import 'package:either_option/src/either.dart'; ///Simple Option monad implementation abstract class Option { + const Option(); + /// Return [None] Option static Option empty() => _none(); @@ -17,10 +19,13 @@ abstract class Option { /// True if None else false bool get isEmpty => !isDefined; - /// Return [a] inside [Some] else supplied [caseElse] if None + /// Return [a] inside [Some] else supplied [caseElse] if None A? getOrElse(A? Function() caseElse) => fold(caseElse, (A a) => a); - /// Return inchanged Option if [Some] else supplied [caseElse] if None + /// Return [a] inside [Some] else null if None + A? getOrNull() => fold(() => null, (A a) => a); + + /// Return unchanged Option if [Some] else supplied [caseElse] if None Option orElse(Option Function() caseElse) => fold(caseElse, (A a) => this); // or (A a) => some(a) @@ -78,6 +83,8 @@ class Some extends Option { } class None extends Option { + const None(); + @override bool operator ==(that) => that is None; diff --git a/pubspec.yaml b/pubspec.yaml index 7038a2c..a840301 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,7 +8,7 @@ environment: sdk: ">=2.12.0 <3.0.0" dependencies: - test: ^1.16.4 + test: ">=1.21.0 <2.0.0" # For information on the generic Dart part of this file, see the # following page: https://www.dartlang.org/tools/pub/pubspec diff --git a/test/option_test.dart b/test/option_test.dart index 93ccaab..2c22a2b 100644 --- a/test/option_test.dart +++ b/test/option_test.dart @@ -43,10 +43,14 @@ void main() { expect(a.fold(() => "ko", (_) => "ok"), "ko"); expect(b.fold(() => "ko", (_) => "ok"), "ok"); - /// getorElse + /// getOrElse expect(a.getOrElse(() => 0), 0); expect(b.getOrElse(() => 0), 2); + /// getOrNull + expect(a.getOrNull(), null); + expect(b.getOrNull(), 2); + /// orElse expect(a.orElse(() => Some("0")), Some("0")); expect(b.orElse(() => Some(0)), Some(2));