< Summary

Information
Class: MyNet.Primitives.SimplificationExtensions
Assembly: MyNet.Primitives
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Conversion/Extensions/SimplificationExtensions.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 102
Line coverage: 100%
Branch coverage
96%
Covered branches: 25
Total branches: 26
Branch coverage: 96.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Simplify(...)100%11100%
Simplify(...)100%11100%
Simplify(...)84.61%2626100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Primitives/Conversion/Extensions/SimplificationExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SimplificationExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using MyNet.Primitives.Conversion;
 10
 11#pragma warning disable IDE0130 // Namespace does not match folder structure
 12namespace MyNet.Primitives;
 13#pragma warning restore IDE0130 // Namespace does not match folder structure
 14
 15public static class SimplificationExtensions
 16{
 17    extension<TUnit>(Quantity<TUnit> quantity)
 18        where TUnit : struct, Enum
 19    {
 20        /// <summary>
 21        /// Simplifies a quantity by converting it to the most appropriate unit within a specified range. The method ite
 22        /// </summary>
 23        /// <param name="min">Optional minimum unit for simplification.</param>
 24        /// <param name="max">Optional maximum unit for simplification.</param>
 25        /// <returns>The simplified quantity.</returns>
 26        public Quantity<TUnit> Simplify(TUnit? min = null, TUnit? max = null)
 27        {
 928            var (value, unit) = quantity.Value.Simplify(quantity.Unit, min, max);
 29
 930            return Quantity.Of(value, unit);
 31        }
 32    }
 33
 34    extension(double value)
 35    {
 36        /// <summary>
 37        /// Simplifies a value by converting it to the most appropriate unit within a specified range. The method iterat
 38        /// </summary>
 39        /// <param name="unit">Current unit of the value.</param>
 40        /// <param name="min">Optional minimum unit for simplification.</param>
 41        /// <param name="max">Optional maximum unit for simplification.</param>
 42        /// <typeparam name="TUnit">Type of the unit.</typeparam>
 43        /// <returns>A tuple containing the simplified value and its corresponding unit.</returns>
 44        public (double NewValue, TUnit NewUnit) Simplify<TUnit>(TUnit unit, TUnit? min = null, TUnit? max = null)
 45            where TUnit : struct, Enum
 2146            => value.Simplify(typeof(TUnit), unit, min, max);
 47
 48        /// <summary>
 49        /// Simplifies a value by converting it to the most appropriate unit within a specified range. The method iterat
 50        /// </summary>
 51        /// <param name="type">Type of unit.</param>
 52        /// <param name="unit">Current unit of the value.</param>
 53        /// <param name="min">Optional minimum unit for simplification.</param>
 54        /// <param name="max">Optional maximum unit for simplification.</param>
 55        /// <typeparam name="TUnit">Type of the unit.</typeparam>
 56        /// <returns>A tuple containing the simplified value and its corresponding unit.</returns>
 57        public (double NewValue, TUnit NewUnit) Simplify<TUnit>(Type type, TUnit unit, TUnit? min = null, TUnit? max = n
 58            where TUnit : struct, Enum
 59        {
 2160            var values = (TUnit[])Enum.GetValues(type);
 61
 2162            var bestValue = value;
 2163            var bestUnit = unit;
 2164            var hasBestCandidate = false;
 2165            var bestUnderOneValue = double.MinValue;
 2166            var bestUnderOneUnit = unit;
 2167            var hasUnderOneCandidate = false;
 68
 34869            foreach (var u in values)
 70            {
 15371                if (min.HasValue && Comparer<TUnit>.Default.Compare(u, min.Value) < 0)
 72                    continue;
 73
 12974                if (max.HasValue && Comparer<TUnit>.Default.Compare(u, max.Value) > 0)
 75                    continue;
 76
 11777                var converted = UnitConverterRegistry.Get<TUnit>().Convert(value, unit, u);
 78
 11779                switch (converted)
 80                {
 9681                    case >= 1 when !hasBestCandidate || converted < bestValue:
 5182                        bestValue = converted;
 5183                        bestUnit = u;
 5184                        hasBestCandidate = true;
 5185                        break;
 2186                    case < 1 when !hasUnderOneCandidate || converted > bestUnderOneValue:
 987                        bestUnderOneValue = converted;
 988                        bestUnderOneUnit = u;
 989                        hasUnderOneCandidate = true;
 90                        break;
 91                }
 92            }
 93
 2194            return hasBestCandidate
 2195                ? (bestValue, bestUnit)
 2196                : hasUnderOneCandidate
 2197                    ? (bestUnderOneValue, bestUnderOneUnit)
 2198                    : (bestValue, bestUnit);
 99        }
 100    }
 101}
 102