Skip to content

Commit

Permalink
v1.0.22
Browse files Browse the repository at this point in the history
- Added extension `NumericTypeExtension`:
  - Methods: `isNumericType`, `isNumericOrDynamicNumberType`, `isDynamicNumberType`.
- Added extension `NumericTypeExtension`:
  - Methods: `isNumericValue`, `isNumericOrDynamicNumberValue`, `isDynamicNumberValue`, `whenNull`.
  • Loading branch information
gmpassos committed Jul 30, 2022

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent d43d845 commit f133ac2
Showing 5 changed files with 159 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.0.22

- Added extension `NumericTypeExtension`:
- Methods: `isNumericType`, `isNumericOrDynamicNumberType`, `isDynamicNumberType`.
- Added extension `NumericTypeExtension`:
- Methods: `isNumericValue`, `isNumericOrDynamicNumberValue`, `isDynamicNumberValue`, `whenNull`.

## 1.0.21

- Added `StandardDeviationComputer`:
44 changes: 44 additions & 0 deletions lib/src/statistics_extension_num.dart
Original file line number Diff line number Diff line change
@@ -8,6 +8,50 @@ import 'statistics_base.dart';
import 'statistics_dynamic_int.dart';
import 'statistics_decimal.dart';

/// Extension for [Type].
extension NumericTypeExtension on Type {
/// Returns `true` if `this` [Type] is an [int], [double], [num] or [BigInt].
bool get isNumericType {
var self = this;
return self == int || self == double || self == num || self == BigInt;
}

/// Returns `true` if `this` [Type] is an [int], [double], [num], [BigInt] or a [DynamicNumber]
/// See [isNumericType] and [isDynamicNumberType].
bool get isNumericOrDynamicNumberType => isNumericType || isDynamicNumberType;

/// Returns `true` if `this` [Type] is a [DynamicNumber], [DynamicInt] or [Decimal].
bool get isDynamicNumberType {
var self = this;
return self == DynamicNumber || self == DynamicInt || self == Decimal;
}
}

/// Extension for [Object] nullable.
extension NumberObjectExtension on Object? {
/// Returns `true` if `this` object is a number ([num] or [BigInt]).
bool get isNumericValue {
var self = this;
return self is num || self is BigInt;
}

/// Returns `true` if `this` object is a number ([num], [BigInt] or [DynamicNumber]).
/// See [isNumericValue] and [isDynamicNumberValue].
bool get isNumericOrDynamicNumberValue =>
isNumericValue || isDynamicNumberValue;

/// Returns `true` if `this` object is a [DynamicNumber].
bool get isDynamicNumberValue => this is DynamicNumber;

/// Returns [defaultValue] if `this == null`.
T whenNull<T>(T defaultValue) {
if (this == null) {
return defaultValue;
}
return this as T;
}
}

/// extension for `Iterable<N>` (`N` extends `num`).
extension IterableNExtension<N extends num> on Iterable<N> {
bool get castsToDouble => N == double;
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: statistics
description: Statistics package for easy and efficient data manipulation with built-in Bayesian Network (Bayes Net), many mathematical functions and tools.
version: 1.0.21
version: 1.0.22
homepage: https://github.com/gmpassos/statistics

environment:
2 changes: 1 addition & 1 deletion statistics.iml
Original file line number Diff line number Diff line change
@@ -3,10 +3,10 @@
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/coverage" />
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/coverage" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
106 changes: 106 additions & 0 deletions test/statistics_extension_num_test.dart
Original file line number Diff line number Diff line change
@@ -5,6 +5,112 @@ import 'package:statistics/statistics.dart';
import 'package:test/test.dart';

void main() {
group('NumericTypeExtension', () {
test('basic', () {
{
var t = int;
expect(t.isNumericType, isTrue);
expect(t.isNumericOrDynamicNumberType, isTrue);
expect(t.isDynamicNumberType, isFalse);
}

{
var t = double;
expect(t.isNumericType, isTrue);
expect(t.isNumericOrDynamicNumberType, isTrue);
expect(t.isDynamicNumberType, isFalse);
}

{
var t = num;
expect(t.isNumericType, isTrue);
expect(t.isNumericOrDynamicNumberType, isTrue);
expect(t.isDynamicNumberType, isFalse);
}

{
var t = BigInt;
expect(t.isNumericType, isTrue);
expect(t.isNumericOrDynamicNumberType, isTrue);
expect(t.isDynamicNumberType, isFalse);
}

{
var t = DynamicInt;
expect(t.isNumericType, isFalse);
expect(t.isNumericOrDynamicNumberType, isTrue);
expect(t.isDynamicNumberType, isTrue);
}

{
var t = Decimal;
expect(t.isNumericType, isFalse);
expect(t.isNumericOrDynamicNumberType, isTrue);
expect(t.isDynamicNumberType, isTrue);
}

{
var t = DynamicNumber;
expect(t.isNumericType, isFalse);
expect(t.isNumericOrDynamicNumberType, isTrue);
expect(t.isDynamicNumberType, isTrue);
}

{
var t = String;
expect(t.isNumericType, isFalse);
expect(t.isNumericOrDynamicNumberType, isFalse);
expect(t.isDynamicNumberType, isFalse);
}
});
});

group('NumberObjectExtension', () {
test('basic', () {
{
var v = 123;
expect(v.isNumericValue, isTrue);
expect(v.isNumericOrDynamicNumberValue, isTrue);
expect(v.isDynamicNumberValue, isFalse);
}

{
var v = 12.3;
expect(v.isNumericValue, isTrue);
expect(v.isNumericOrDynamicNumberValue, isTrue);
expect(v.isDynamicNumberValue, isFalse);
}

{
var v = BigInt.from(100);
expect(v.isNumericValue, isTrue);
expect(v.isNumericOrDynamicNumberValue, isTrue);
expect(v.isDynamicNumberValue, isFalse);
}

{
var v = DynamicInt.fromInt(123);
expect(v.isNumericValue, isFalse);
expect(v.isNumericOrDynamicNumberValue, isTrue);
expect(v.isDynamicNumberValue, isTrue);
}

{
var v = Decimal.fromDouble(12.3);
expect(v.isNumericValue, isFalse);
expect(v.isNumericOrDynamicNumberValue, isTrue);
expect(v.isDynamicNumberValue, isTrue);
}

{
var v = 'abc';
expect(v.isNumericValue, isFalse);
expect(v.isNumericOrDynamicNumberValue, isFalse);
expect(v.isDynamicNumberValue, isFalse);
}
});
});

group('int', () {
setUp(() {});

0 comments on commit f133ac2

Please sign in to comment.