From 7ea883b0721fd3bdb209f88a2c9870715aab6c09 Mon Sep 17 00:00:00 2001 From: Dennis Date: Fri, 18 Aug 2023 18:29:28 +0200 Subject: [PATCH] Add illuminance units (#38) * Add illuminance units * Fix description * Format + adapted code style --------- Co-authored-by: Dennis Geldhof Co-authored-by: Damiano Ferrari --- lib/models/property.dart | 1 + lib/properties/illuminance.dart | 38 +++++++++++++++++++++++++++++++++ lib/units_converter.dart | 1 + lib/utils/utils.dart | 3 +++ test/conversion_test.dart | 8 +++++++ test/extension_test.dart | 2 ++ test/getters_test.dart | 8 +++++++ 7 files changed, 61 insertions(+) create mode 100644 lib/properties/illuminance.dart diff --git a/lib/models/property.dart b/lib/models/property.dart index d64e371..32dd75c 100644 --- a/lib/models/property.dart +++ b/lib/models/property.dart @@ -9,6 +9,7 @@ enum PROPERTY { energy, force, fuelConsumption, + illuminance, length, mass, molarmass, diff --git a/lib/properties/illuminance.dart b/lib/properties/illuminance.dart new file mode 100644 index 0000000..75de5af --- /dev/null +++ b/lib/properties/illuminance.dart @@ -0,0 +1,38 @@ +import 'package:units_converter/models/conversion_node.dart'; +import 'package:units_converter/models/double_property.dart'; +import 'package:units_converter/models/property.dart'; +import 'package:units_converter/models/unit.dart'; + +enum ILLUMINANCE { + lux, + footCandle, +} + +class Illuminance extends DoubleProperty { + ///Class for light conversions, e.g. if you want to convert 1 lux in to foot-candle: + ///```dart + ///var illuminance = Illuminance(removeTrailingZeros: false); + ///illuminance.convert(Unit(ILLUMINANCE.lux, value: 1)); + ///print(illuminance.footCandle); + /// ``` + Illuminance( + {super.significantFigures, + super.removeTrailingZeros, + super.useScientificNotation, + name}) + : super( + name: name ?? PROPERTY.illuminance, + mapSymbols: { + ILLUMINANCE.lux: 'lx', + ILLUMINANCE.footCandle: 'fc', + }, + conversionTree: ConversionNode(name: ILLUMINANCE.lux, leafNodes: [ + ConversionNode( + coefficientProduct: 10.763910416709722, + name: ILLUMINANCE.footCandle, + ), + ])); + + Unit get lux => getUnit(ILLUMINANCE.lux); + Unit get footCandle => getUnit(ILLUMINANCE.footCandle); +} diff --git a/lib/units_converter.dart b/lib/units_converter.dart index d9012f2..68062bd 100644 --- a/lib/units_converter.dart +++ b/lib/units_converter.dart @@ -26,3 +26,4 @@ export 'package:units_converter/properties/digital_data.dart'; export 'package:units_converter/properties/shoe_size.dart'; export 'package:units_converter/properties/numeral_systems.dart'; export 'package:units_converter/properties/density.dart'; +export 'package:units_converter/properties/illuminance.dart'; diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index 9707a65..0b24485 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -1,6 +1,7 @@ import 'dart:math'; import 'package:units_converter/models/property.dart'; import 'package:units_converter/properties/amount_of_substance.dart'; +import 'package:units_converter/properties/illuminance.dart'; import 'package:units_converter/properties/length.dart'; import 'package:units_converter/properties/area.dart'; import 'package:units_converter/properties/molar_mass.dart'; @@ -41,6 +42,8 @@ Property? getPropertyFromEnum(dynamic propertyEnum) { return Force(); case FUEL_CONSUMPTION: return FuelConsumption(); + case ILLUMINANCE: + return Illuminance(); case LENGTH: return Length(); case MASS: diff --git a/test/conversion_test.dart b/test/conversion_test.dart index b79ad28..9eb2fda 100644 --- a/test/conversion_test.dart +++ b/test/conversion_test.dart @@ -174,6 +174,14 @@ void main() { runConversionTest(expectedResult, FuelConsumption()); }); + group('Illuminance', () { + const Map expectedResult = { + ILLUMINANCE.lux: 1, + ILLUMINANCE.footCandle: 0.09290304, + }; + runConversionTest(expectedResult, Illuminance()); + }); + group('Length', () { const Map expectedResult = { LENGTH.meters: 1, diff --git a/test/extension_test.dart b/test/extension_test.dart index 79ad598..a206e91 100644 --- a/test/extension_test.dart +++ b/test/extension_test.dart @@ -10,6 +10,8 @@ void main() { expect(1.convertFromTo(DIGITAL_DATA.byte, DIGITAL_DATA.nibble), 2); expect(1.convertFromTo(ENERGY.kilocalories, ENERGY.calories), 1e3); expect(1.convertFromTo(FORCE.dyne, FORCE.newton), 1e-5); + expect( + 1.convertFromTo(ILLUMINANCE.lux, ILLUMINANCE.footCandle), 0.09290304); expect( 1.convertFromTo(FUEL_CONSUMPTION.kilometersPerLiter, FUEL_CONSUMPTION.litersPer100km), diff --git a/test/getters_test.dart b/test/getters_test.dart index 4f42e10..9329c5d 100644 --- a/test/getters_test.dart +++ b/test/getters_test.dart @@ -169,6 +169,14 @@ void main() { property.getUnit(FUEL_CONSUMPTION.milesPerImperialGallon)); }); + group('Illuminance test', () { + var property = Illuminance(); + property.convert(ILLUMINANCE.lux, 1); + runGetterTest(property.lux, property.getUnit(ILLUMINANCE.lux)); + runGetterTest( + property.footCandle, property.getUnit(ILLUMINANCE.footCandle)); + }); + group('Length test', () { var property = Length(); property.convert(LENGTH.meters, 1);