< Summary

Information
Class: MyNet.Humanizer.Temporal.HumanFriendlyTimeSpanQuantizer
Assembly: MyNet.Humanizer
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Temporal/HumanFriendlyTimeSpanQuantizer.cs
Tag: 323_28699572109
Line coverage
77%
Covered lines: 21
Uncovered lines: 6
Coverable lines: 27
Total lines: 71
Line coverage: 77.7%
Branch coverage
61%
Covered branches: 11
Total branches: 18
Branch coverage: 61.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Quantize(...)90%1010100%
GetThreshold(...)25%18845.45%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Temporal/HumanFriendlyTimeSpanQuantizer.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="HumanFriendlyTimeSpanQuantizer.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System.Collections.Generic;
 8using System.Linq;
 9using MyNet.Primitives;
 10using MyNet.Temporal.Decomposition;
 11
 12namespace MyNet.Humanizer.Temporal;
 13
 14/// <summary>
 15/// Rounds decomposed values to friendlier thresholds (for instance 59 seconds to 1 minute).
 16/// </summary>
 17/// <example>
 18/// [59 seconds] becomes [1 minute].
 19/// [1 hour, 59 minutes] can become [2 hours].
 20/// </example>
 21public sealed class HumanFriendlyTimeSpanQuantizer : ITimeSpanQuantizer
 22{
 23    /// <summary>
 24    /// Gets the default quantizer instance.
 25    /// </summary>
 326    public static HumanFriendlyTimeSpanQuantizer Default { get; } = new();
 27
 28    /// <inheritdoc/>
 29    public IReadOnlyList<TimeUnitValue> Quantize(IReadOnlyList<TimeUnitValue> values)
 30    {
 1531        if (values.Count <= 1)
 932            return values;
 33
 634        var ordered = values.OrderByDescending(x => x.Unit).ToList();
 635        var dict = ordered.ToDictionary(x => x.Unit, x => x.Value);
 36
 3037        for (var i = ordered.Count - 1; i > 0; i--)
 38        {
 939            var currentUnit = ordered[i].Unit;
 940            var nextUnit = ordered[i - 1].Unit;
 941            var currentValue = dict[currentUnit];
 942            var threshold = GetThreshold(currentUnit);
 43
 944            if (threshold > 0 && currentValue >= threshold)
 45            {
 646                dict[nextUnit]++;
 647                dict[currentUnit] = 0;
 48
 49                // Drop smaller details after promoting to a bigger unit.
 1850                for (var j = i + 1; j < ordered.Count; j++)
 351                    dict[ordered[j].Unit] = 0;
 52            }
 53        }
 54
 655        return ordered.ConvertAll(x => x with { Value = dict[x.Unit] });
 56    }
 57
 58    private static int GetThreshold(TimeUnit unit)
 959        => unit switch
 960        {
 061            TimeUnit.Millisecond => 500,
 662            TimeUnit.Second => 59,
 363            TimeUnit.Minute => 59,
 064            TimeUnit.Hour => 23,
 065            TimeUnit.Day => 6,
 066            TimeUnit.Week => 3,
 067            TimeUnit.Month => 11,
 068            _ => 0
 969        };
 70}
 71