From 9e878ab7ea445b540ea3b6a1c130eb3f6eb464b9 Mon Sep 17 00:00:00 2001 From: rishabhdeepsingh Date: Wed, 9 Jun 2021 00:05:03 +0530 Subject: [PATCH] added arithmetic plus and minus for num class --- example/main.dart | 2 ++ lib/dartx.dart | 1 + lib/src/arithmetic.dart | 15 ++++++++ test/arithmetic_test.dart | 74 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 lib/src/arithmetic.dart create mode 100644 test/arithmetic_test.dart diff --git a/example/main.dart b/example/main.dart index da45083..954be8d 100644 --- a/example/main.dart +++ b/example/main.dart @@ -28,6 +28,8 @@ void main() { [4, 5, 6] ]; final flattened = nestedList.flatten(); // [1, 2, 3, 4, 5, 6] + + 123.0.minus(null); // 123.0 } class Dog { diff --git a/lib/dartx.dart b/lib/dartx.dart index 4b159f4..2dc98fc 100644 --- a/lib/dartx.dart +++ b/lib/dartx.dart @@ -13,6 +13,7 @@ import 'package:crypto/crypto.dart' as crypto; export 'package:time/time.dart'; export 'package:characters/characters.dart'; +part 'src/arithmetic.dart'; part 'src/comparable.dart'; part 'src/comparator.dart'; part 'src/function.dart'; diff --git a/lib/src/arithmetic.dart b/lib/src/arithmetic.dart new file mode 100644 index 0000000..57620fa --- /dev/null +++ b/lib/src/arithmetic.dart @@ -0,0 +1,15 @@ +part of dartx; + +extension NumArithmeticX on T { + /// Minus val if if it not null else returns `this` + T? minus(T? val) => val == null ? this : this - val as T?; + + /// returns null if val is null else returns `this` - val + T? minusOrNull(T? val) => val == null ? null : this - val as T?; + + /// Adds val if if it not null else returns `this` + T? plus(T? val) => val == null ? this : this + val as T?; + + /// returns null if val is null else returns `this` + val + T? plusOrNull(T? val) => val == null ? null : this + val as T?; +} diff --git a/test/arithmetic_test.dart b/test/arithmetic_test.dart new file mode 100644 index 0000000..7a503d4 --- /dev/null +++ b/test/arithmetic_test.dart @@ -0,0 +1,74 @@ +import 'package:test/test.dart'; +import 'package:dartx/dartx.dart'; + +void main() { + group('num', () { + test('num?.plus() and num?.minus()', () { + const num? value = null; + expect(value?.minus(1), null); + expect(value?.minus(1) ?? -1, -1); + expect((value ?? 0) - 1, -1); + + expect(value?.plus(1), null); + expect(value?.plus(1) ?? 1, 1); + expect((value ?? 0) + 1, 1); + }); + test('num?.plusOrNull() and num?.minusOrNull()', () { + const num? value = null; + const num one = 1; + expect(one.plus(value), 1); + expect(one.plusOrNull(value), null); + expect(one.plusOrNull(value) ?? 1, 1); + + expect(one.minus(value), 1); + expect(one.minusOrNull(value), null); + expect(one.minusOrNull(value) ?? 1, 1); + }); + }); + + group('int', () { + test('int?.plus() and int?.minus()', () { + const int? value = null; + expect(value?.minus(1), null); + expect(value?.minus(1) ?? -1, -1); + expect((value ?? 0) - 1, -1); + + expect(value?.plus(1), null); + expect(value?.plus(1) ?? 1, 1); + expect((value ?? 0) + 1, 1); + }); + test('int?.plusOrNull() and int?.minusOrNull()', () { + const int? value = null; + expect(1.plus(value), 1); + expect(1.plusOrNull(value), null); + expect(1.plusOrNull(value) ?? 1, 1); + + expect(1.minus(value), 1); + expect(1.minusOrNull(value), null); + expect(1.minusOrNull(value) ?? 1, 1); + }); + }); + + group('double', () { + test('double?.plus() and double?.minus()', () { + const double? value = null; + expect(value?.minus(1), null); + expect(value?.minus(1) ?? -1, -1); + expect((value ?? 0) - 1, -1); + + expect(value?.plus(1), null); + expect(value?.plus(1) ?? 1, 1); + expect((value ?? 0) + 1, 1); + }); + test('double?.plusOrNull() and double?.minusOrNull()', () { + const double? value = null; + expect(1.0.plus(value), 1.0); + expect(1.0.plusOrNull(value), null); + expect(1.0.plusOrNull(value) ?? 1.0, 1.0); + + expect(1.0.minus(value), 1.0); + expect(1.0.minusOrNull(value), null); + expect(1.0.minusOrNull(value) ?? 1.0, 1.0); + }); + }); +}