< Summary

Information
Class: MyNet.IO.FileExtensions.FileExtensionFilter
Assembly: MyNet.IO
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.IO/FileExtensions/FileExtensionFilter.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 45
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_AllExtensions()100%11100%
ToFilterString(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.IO/FileExtensions/FileExtensionFilter.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="FileExtensionFilter.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 System.Linq;
 10
 11namespace MyNet.IO.FileExtensions;
 12
 13/// <summary>
 14/// Represents a file extension filter, which is a collection of file extension groups. Each group has a key and a list 
 15/// </summary>
 16/// <param name="groups">The collection of file extension groups to be included in the filter.</param>
 17public sealed class FileExtensionFilter(IEnumerable<FileExtensionGroup> groups)
 18{
 19    /// <summary>
 20    /// Gets the collection of file extension groups included in the filter. Each group represents a category of files a
 21    /// </summary>
 2722    public IReadOnlyList<FileExtensionGroup> Groups { get; } = [.. groups];
 23
 24    /// <summary>
 25    /// Gets a combined list of all file extensions across all groups in the filter. This property flattens the list of 
 26    /// </summary>
 1527    public IEnumerable<string> AllExtensions => Groups.SelectMany(g => g.Extensions)
 1528        .Select(e => e.Value)
 1529        .Distinct();
 30
 31    /// <summary>
 32    /// Generates a filter string that can be used in file dialogs to filter files based on their extensions. The filter
 33    /// </summary>
 34    /// <param name="localize">A function to localize the group names. If null, the group keys are used as-is.</param>
 35    /// <returns>A filter string that can be used in file dialogs.</returns>
 36    public string ToFilterString(Func<string, string>? localize = null)
 637        => string.Join("|",
 638            Groups.Select(g =>
 639            {
 640                var name = localize?.Invoke(g.Key) ?? g.Key;
 641                var exts = string.Join(";", g.Extensions.Select(e => $"*{e.Value}"));
 642                return $"{name} ({exts})";
 643            }));
 44}
 45