< Summary

Information
Class: Utility.Components.MemoryConverter.MemoryConverter
Assembly: Utility
File(s): /home/runner/work/Utility-Blazor/Utility-Blazor/src/Utility/Components/MemoryConverter/MemoryConverter.razor
Tag: 231_14069517506
Line coverage
68%
Covered lines: 22
Uncovered lines: 10
Coverable lines: 32
Total lines: 105
Line coverage: 68.7%
Branch coverage
50%
Covered branches: 4
Total branches: 8
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Input()100%11100%
get_Output()100%11100%
OnInitialized()100%11100%
Convert()100%11100%
Clear()100%210%
Copy()100%210%
.cctor()100%11100%
SizeSuffix(...)50%9873.33%

File(s)

/home/runner/work/Utility-Blazor/Utility-Blazor/src/Utility/Components/MemoryConverter/MemoryConverter.razor

#LineLine coverage
 1@using System.Text
 2@inject IJSRuntime JSRuntime
 3
 4<div class="container">
 5
 6    <div class="row">
 7        <div class="col">
 8            <h2>Memory Converter</h2>
 9        </div>
 10    </div>
 11
 12    <div class="row">
 13        <div class="col">
 14            <div class="input-group">
 15                <div class="input-group-prepend">
 16                    <span class="input-group-text">Bytes:</span>
 17                </div>
 18                <input type="number" id="input" class="form-control" @bind="Input" placeholder="1030" />
 19                <span class="input-group-btn">
 20                    <button id="btnClear" name="btnBinaryClear" class="btn btn-danger float-right" @onclick="Clear"><i c
 21                </span>
 22            </div>
 23        </div>
 24    </div>
 25
 26    <div class="row">
 27        <div class="col">
 28            <button id="btnConvert" name="btnConvert" class="btn btn-success float-right" @onclick="Convert">Convert</bu
 29        </div>
 30    </div>
 31
 32    <div class="row">
 33        <div class="col">
 34            <div class="input-group">
 35                <input id="output" class="form-control" @bind="Output" placeholder="KB / MB / GB / TB / PB / EB / ZB / Y
 36                <span class="input-group-btn">
 37                    <button id="btnCopy" name="btnCopy" class="btn btn-info float-right" @onclick="Copy"><i class="far f
 38                </span>
 39            </div>
 40
 41        </div>
 42    </div>
 43
 44</div>
 45
 46@code {
 47    [Parameter]
 748    public long Input { get; set; }
 49
 50    [Parameter]
 751    public string? Output { get; set; }
 52
 53    protected override void OnInitialized()
 154    {
 155        Input = 1030;
 156        Output = string.Empty;
 157    }
 58
 59    void Convert()
 160    {
 161        var data = SizeSuffix(Input);
 162        Output = data;
 163    }
 64
 65    private void Clear()
 066    {
 067        Input = 0;
 068    }
 69
 70    async Task Copy()
 071    {
 072        await JSRuntime.InvokeVoidAsync("navigator.clipboard.writeText", Output);
 073    }
 74
 75    #region Convert
 76    // https://stackoverflow.com/a/14488941/2895831
 177    static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
 78
 79    static string SizeSuffix(Int64 value, int decimalPlaces = 1)
 180    {
 181        if (decimalPlaces < 0) { throw new ArgumentOutOfRangeException("decimalPlaces"); }
 182        if (value < 0) { return "-" + SizeSuffix(-value, decimalPlaces); }
 183        if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); }
 84
 85        // mag is 0 for bytes, 1 for KB, 2, for MB, etc.
 186        int mag = (int)Math.Log(value, 1024);
 87
 88        // 1L << (mag * 10) == 2 ^ (10 * mag)
 89        // [i.e. the number of bytes in the unit corresponding to mag]
 190        decimal adjustedSize = (decimal)value / (1L << (mag * 10));
 91
 92        // make adjustment when the value is large enough that
 93        // it would round up to 1000 or more
 194        if (Math.Round(adjustedSize, decimalPlaces) >= 1000)
 095        {
 096            mag += 1;
 097            adjustedSize /= 1024;
 098        }
 99
 1100        return string.Format("{0:n" + decimalPlaces + "} {1}",
 1101            adjustedSize,
 1102            SizeSuffixes[mag]);
 1103    }
 104    #endregion Convert
 105}