-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from rishabhdeepsingh/nullable_num
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
part of dartx; | ||
|
||
extension NumArithmeticX<T extends num> 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?; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
} |