Skip to content

Commit

Permalink
Add NullableObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed Jun 11, 2024
1 parent 3bf7d55 commit 9e10c95
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
4 changes: 3 additions & 1 deletion sugar/lib/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
/// * [Strings]
///
/// ## Miscellaneous
///
/// * [NullableObjects]
/// * [Disposable]
/// * [StringBuffers]
library sugar.core;
Expand All @@ -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';
Expand All @@ -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';
8 changes: 7 additions & 1 deletion sugar/lib/math.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
/// * [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
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';
18 changes: 18 additions & 0 deletions sugar/lib/src/core/nullable.dart
Original file line number Diff line number Diff line change
@@ -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<int>(); // 1
///
/// 'string'.as<int>(); // null
/// null.as<int>(); // null
/// ```
T? as<T extends Object>() {
final self = this;
return self is T ? self : null;
}

}
File renamed without changes.
17 changes: 17 additions & 0 deletions sugar/test/src/core/nullable_test.dart
Original file line number Diff line number Diff line change
@@ -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<int>(), 1);
});

test('different type', () => expect('string'.as<int>(), null));

test('null', () => expect(null.as<int>(), null));
});
});
}

0 comments on commit 9e10c95

Please sign in to comment.