From f133ac2d94ed31c0d3d697dae6ccaf69afc09bb2 Mon Sep 17 00:00:00 2001 From: gmpassos Date: Fri, 29 Jul 2022 22:53:00 -0300 Subject: [PATCH] v1.0.22 - Added extension `NumericTypeExtension`: - Methods: `isNumericType`, `isNumericOrDynamicNumberType`, `isDynamicNumberType`. - Added extension `NumericTypeExtension`: - Methods: `isNumericValue`, `isNumericOrDynamicNumberValue`, `isDynamicNumberValue`, `whenNull`. --- CHANGELOG.md | 7 ++ lib/src/statistics_extension_num.dart | 44 ++++++++++ pubspec.yaml | 2 +- statistics.iml | 2 +- test/statistics_extension_num_test.dart | 106 ++++++++++++++++++++++++ 5 files changed, 159 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 473f625..cae258e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`: diff --git a/lib/src/statistics_extension_num.dart b/lib/src/statistics_extension_num.dart index 20c79ba..2f29a9b 100644 --- a/lib/src/statistics_extension_num.dart +++ b/lib/src/statistics_extension_num.dart @@ -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 defaultValue) { + if (this == null) { + return defaultValue; + } + return this as T; + } +} + /// extension for `Iterable` (`N` extends `num`). extension IterableNExtension on Iterable { bool get castsToDouble => N == double; diff --git a/pubspec.yaml b/pubspec.yaml index 1a2e77e..02c662e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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: diff --git a/statistics.iml b/statistics.iml index 2038a80..6c7fd59 100644 --- a/statistics.iml +++ b/statistics.iml @@ -3,10 +3,10 @@ + - diff --git a/test/statistics_extension_num_test.dart b/test/statistics_extension_num_test.dart index 599afd9..11b886a 100644 --- a/test/statistics_extension_num_test.dart +++ b/test/statistics_extension_num_test.dart @@ -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(() {});