From 9e10c954797f25b0e5de640a03c5ad122a468df9 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 11 Jun 2024 16:13:55 +0800 Subject: [PATCH] Add NullableObjects --- sugar/lib/core.dart | 4 +++- sugar/lib/math.dart | 8 +++++++- sugar/lib/src/core/nullable.dart | 18 ++++++++++++++++++ .../{nullable.dart => nullable_numbers.dart} | 0 sugar/test/src/core/nullable_test.dart | 17 +++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 sugar/lib/src/core/nullable.dart rename sugar/lib/src/math/{nullable.dart => nullable_numbers.dart} (100%) create mode 100644 sugar/test/src/core/nullable_test.dart diff --git a/sugar/lib/core.dart b/sugar/lib/core.dart index a6e643bf..1acc355f 100644 --- a/sugar/lib/core.dart +++ b/sugar/lib/core.dart @@ -48,7 +48,7 @@ /// * [Strings] /// /// ## Miscellaneous -/// +/// * [NullableObjects] /// * [Disposable] /// * [StringBuffers] library sugar.core; @@ -59,6 +59,7 @@ import 'package:sugar/src/core/comparables.dart'; import 'package:sugar/src/core/disposable.dart'; import 'package:sugar/src/core/equality.dart'; import 'package:sugar/src/core/functions.dart'; +import 'package:sugar/src/core/nullable.dart'; import 'package:sugar/src/core/result.dart'; import 'package:sugar/src/core/string_buffers.dart'; import 'package:sugar/src/core/strings.dart'; @@ -69,6 +70,7 @@ export 'src/core/comparables.dart'; export 'src/core/disposable.dart'; export 'src/core/equality.dart'; export 'src/core/functions.dart'; +export 'src/core/nullable.dart'; export 'src/core/result.dart'; export 'src/core/string_buffers.dart'; export 'src/core/strings.dart'; diff --git a/sugar/lib/math.dart b/sugar/lib/math.dart index 7cd46f71..cfe6f306 100644 --- a/sugar/lib/math.dart +++ b/sugar/lib/math.dart @@ -6,6 +6,11 @@ /// * [Integers] - functions that ceil, floor and round numbers. /// * [Doubles] - function for fuzzy equality /// +/// ## Nullable numeric operations +/// * [NullableNumbers] - overloaded operators for working with nullable numbers. +/// * [NullableIntegers] - overloaded operators for working with nullable integers. +/// * [NullableDoubles] - overloaded operators for working with nullable doubles. +/// /// ## Random /// * [Randoms] - functions for using [Random] /// * [FakeRandom] - a fake [Random] implementation for testing @@ -13,10 +18,11 @@ library sugar.math; import 'dart:math'; +import 'package:sugar/src/math/nullable_numbers.dart'; import 'package:sugar/src/math/numbers.dart'; import 'package:sugar/src/math/random.dart'; export 'src/math/arithmetic_exception.dart'; -export 'src/math/nullable.dart'; +export 'src/math/nullable_numbers.dart'; export 'src/math/numbers.dart'; export 'src/math/random.dart'; diff --git a/sugar/lib/src/core/nullable.dart b/sugar/lib/src/core/nullable.dart new file mode 100644 index 00000000..94273031 --- /dev/null +++ b/sugar/lib/src/core/nullable.dart @@ -0,0 +1,18 @@ +/// Provides functions for casting nullable [Object]s. +extension NullableObjects on Object? { + + /// Casts `this` to [T] if it is a [T], and returns `null` otherwise. + /// + /// ```dart + /// final num foo = 1; + /// foo.as(); // 1 + /// + /// 'string'.as(); // null + /// null.as(); // null + /// ``` + T? as() { + final self = this; + return self is T ? self : null; + } + +} diff --git a/sugar/lib/src/math/nullable.dart b/sugar/lib/src/math/nullable_numbers.dart similarity index 100% rename from sugar/lib/src/math/nullable.dart rename to sugar/lib/src/math/nullable_numbers.dart diff --git a/sugar/test/src/core/nullable_test.dart b/sugar/test/src/core/nullable_test.dart new file mode 100644 index 00000000..d9a2fd85 --- /dev/null +++ b/sugar/test/src/core/nullable_test.dart @@ -0,0 +1,17 @@ +import 'package:test/test.dart'; +import 'package:sugar/sugar.dart'; + +void main() { + group('NullableObjects', () { + group('as()', () { + test('exact type', () { + const num foo = 1; + expect(foo.as(), 1); + }); + + test('different type', () => expect('string'.as(), null)); + + test('null', () => expect(null.as(), null)); + }); + }); +}