< Summary

Information
Class: MyNet.Humanizer.Facade.ListFormatterExtensions
Assembly: MyNet.Humanizer
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Facade/Extensions/ListFormatterExtensions.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 185
Line coverage: 100%
Branch coverage
64%
Covered branches: 9
Total branches: 14
Branch coverage: 64.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BuildOptions()100%11100%
Humanize(...)100%22100%
Humanize(...)100%11100%
Humanize(...)100%22100%
Humanize(...)100%11100%
HumanizeWith(...)50%44100%
HumanizeWith(...)50%44100%
HumanizeInParentheses(...)100%11100%
HumanizeInBrackets(...)100%11100%
HumanizeInBraces(...)100%11100%
HumanizeNonNull(...)50%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Facade/Extensions/ListFormatterExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ListFormatterExtensions.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.Globalization;
 10using System.Linq;
 11using MyNet.Globalization.Facade;
 12using MyNet.Humanizer.Formatting.Collections;
 13
 14#pragma warning disable IDE0130 // Namespace does not match folder structure
 15namespace MyNet.Humanizer.Facade;
 16#pragma warning restore IDE0130 // Namespace does not match folder structure
 17
 18/// <summary>
 19/// Provides convenient extension methods for list formatting using <see cref="IListFormatter"/>.
 20/// </summary>
 21public static class ListFormatterExtensions
 22{
 23    /// <summary>
 24    /// Gets or creates a <see cref="ListFormattingOptions"/> builder for a fluent configuration.
 25    /// </summary>
 26    /// <returns>A new <see cref="ListFormattingOptionsBuilder"/> instance.</returns>
 327    public static ListFormattingOptionsBuilder BuildOptions() => new();
 28
 29    extension<T>(IEnumerable<T> source)
 30    {
 31        /// <summary>
 32        /// Formats a collection of items into a human-readable list using the default <see cref="IListFormatter"/>.
 33        /// Returns an empty string if the collection is empty or no formatter is available.
 34        /// </summary>
 35        /// <param name="conjunction">The conjunction to use (And, Or, None, or Ampersand). Defaults to And.</param>
 36        /// <param name="separator">The separator between items. Defaults to ", ".</param>
 37        /// <param name="culture">The culture to use when formatting the list.</param>
 38        /// <returns>A formatted string representation of the list.</returns>
 39        /// <example>
 40        /// <code>
 41        /// var items = new[] { "Apple", "Banana", "Cherry" };
 42        /// var formatted = items.Humanize(); // "Apple, Banana and Cherry"
 43        /// var withOr = items.Humanize(ListConjunction.Or); // "Apple, Banana or Cherry"
 44        /// </code>
 45        /// </example>
 46        public string Humanize(ListConjunction conjunction = ListConjunction.And, string separator = ", ", CultureInfo? 
 2747            => source.Humanize(x => x?.ToString(), new() { Conjunction = conjunction, Separator = separator }, culture);
 48
 49        /// <summary>
 50        /// Formats a collection of items into a human-readable list using a custom formatter for each item.
 51        /// </summary>
 52        /// <param name="formatter">The formatter function to apply to each item. Cannot be null.</param>
 53        /// <param name="conjunction">The conjunction to use (And, Or, None, or Ampersand). Defaults to And.</param>
 54        /// <param name="separator">The separator between items. Defaults to ", ".</param>
 55        /// <param name="culture">The culture to use when formatting the list.</param>
 56        /// <returns>A formatted string representation of the list.</returns>
 57        /// <exception cref="ArgumentNullException">Thrown when <paramref name="formatter"/> is null.</exception>
 58        public string Humanize(Func<T, string?> formatter, ListConjunction conjunction = ListConjunction.And, string sep
 59        {
 2760            ArgumentNullException.ThrowIfNull(formatter);
 2761            return source.Humanize(formatter, new() { Conjunction = conjunction, Separator = separator }, culture);
 62        }
 63
 64        /// <summary>
 65        /// Formats a collection of items into a human-readable list using the provided formatting options.
 66        /// Returns an empty string if the collection is empty or no formatter is available.
 67        /// </summary>
 68        /// <param name="options">The formatting options to use. If null, default options are applied.</param>
 69        /// <param name="culture">The culture to use when formatting the list.</param>
 70        /// <returns>A formatted string representation of the list.</returns>
 71        public string Humanize(ListFormattingOptions options, CultureInfo? culture = null)
 1572            => source.Humanize(x => x?.ToString(), options, culture);
 73
 74        /// <summary>
 75        /// Formats a collection of items into a human-readable list using a custom formatter and formatting options.
 76        /// Returns an empty string if the collection is empty or no formatter is available.
 77        /// </summary>
 78        /// <param name="formatter">The formatter function to apply to each item. Cannot be null.</param>
 79        /// <param name="options">The formatting options to use. If null, default options are applied.</param>
 80        /// <param name="culture">The culture to use when formatting the list.</param>
 81        /// <returns>A formatted string representation of the list.</returns>
 82        /// <exception cref="ArgumentNullException">Thrown when <paramref name="formatter"/> is null.</exception>
 83        public string Humanize(Func<T, string?> formatter, ListFormattingOptions options, CultureInfo? culture = null)
 84        {
 7285            ArgumentNullException.ThrowIfNull(formatter);
 86
 7287            var items = source.Select(formatter).ToList();
 7288            return Localizer.ForCulture(culture).GetRequired<IListFormatter>().Format(items, options);
 89        }
 90
 91        /// <summary>
 92        /// Formats a collection of items with an optional prefix and suffix.
 93        /// </summary>
 94        /// <param name="prefix">An optional prefix to prepend to the formatted list (e.g., "Items: ").</param>
 95        /// <param name="suffix">An optional suffix to append to the formatted list (e.g., ".").</param>
 96        /// <param name="options">The formatting options to use. If null, default options are applied.</param>
 97        /// <param name="culture">The culture to use when formatting the list.</param>
 98        /// <returns>The formatted list with optional prefix and suffix, or an empty string if the list is empty.</retur
 99        /// <example>
 100        /// <code>
 101        /// var items = new[] { "Apple", "Banana" };
 102        /// var result = items.HumanizeWith("Items: ", "."); // "Items: Apple and Banana."
 103        /// </code>
 104        /// </example>
 105        public string HumanizeWith(string? prefix = null, string? suffix = null, ListFormattingOptions? options = null, 
 106        {
 12107            var formatted = source.Humanize(options ?? ListFormattingOptions.Default, culture);
 12108            return string.IsNullOrEmpty(formatted) ? formatted : $"{prefix}{formatted}{suffix}";
 109        }
 110
 111        /// <summary>
 112        /// Formats a collection of items with a custom formatter, optional prefix and suffix.
 113        /// </summary>
 114        /// <param name="formatter">The formatter function to apply to each item. Cannot be null.</param>
 115        /// <param name="prefix">An optional prefix to prepend to the formatted list.</param>
 116        /// <param name="suffix">An optional suffix to append to the formatted list.</param>
 117        /// <param name="options">The formatting options to use. If null, default options are applied.</param>
 118        /// <param name="culture">The culture to use when formatting the list.</param>
 119        /// <returns>The formatted list with optional prefix and suffix, or an empty string if the list is empty.</retur
 120        /// <exception cref="ArgumentNullException">Thrown when <paramref name="formatter"/> is null.</exception>
 121        public string HumanizeWith(Func<T, string?> formatter, string? prefix = null, string? suffix = null, ListFormatt
 122        {
 3123            ArgumentNullException.ThrowIfNull(formatter);
 3124            var formatted = source.Humanize(formatter, options ?? ListFormattingOptions.Default, culture);
 3125            return string.IsNullOrEmpty(formatted) ? formatted : $"{prefix}{formatted}{suffix}";
 126        }
 127
 128        /// <summary>
 129        /// Formats a collection of items with parentheses around the list.
 130        /// </summary>
 131        /// <param name="options">The formatting options to use. If null, default options are applied.</param>
 132        /// <param name="culture">The culture to use when formatting the list.</param>
 133        /// <returns>The formatted list in parentheses, or an empty string if the list is empty.</returns>
 134        /// <example>
 135        /// <code>
 136        /// var items = new[] { "Apple", "Banana" };
 137        /// var result = items.HumanizeInParentheses(); // "(Apple and Banana)"
 138        /// </code>
 139        /// </example>
 140        public string HumanizeInParentheses(ListFormattingOptions? options = null, CultureInfo? culture = null)
 3141            => source.HumanizeWith("(", ")", options, culture);
 142
 143        /// <summary>
 144        /// Formats a collection of items with brackets around the list.
 145        /// </summary>
 146        /// <param name="options">The formatting options to use. If null, default options are applied.</param>
 147        /// <param name="culture">The culture to use when formatting the list.</param>
 148        /// <returns>The formatted list in brackets, or an empty string if the list is empty.</returns>
 149        public string HumanizeInBrackets(ListFormattingOptions? options = null, CultureInfo? culture = null)
 3150            => source.HumanizeWith("[", "]", options, culture);
 151
 152        /// <summary>
 153        /// Formats a collection of items with braces around the list.
 154        /// </summary>
 155        /// <param name="options">The formatting options to use. If null, default options are applied.</param>
 156        /// <param name="culture">The culture to use when formatting the list.</param>
 157        /// <returns>The formatted list in braces, or an empty string if the list is empty.</returns>
 158        public string HumanizeInBraces(ListFormattingOptions? options = null, CultureInfo? culture = null)
 3159            => source.HumanizeWith("{", "}", options, culture);
 160
 161        /// <summary>
 162        /// Formats non-null items from a nullable collection.
 163        /// </summary>
 164        /// <param name="options">The formatting options to use. If null, default options are applied.</param>
 165        /// <param name="culture">The culture to use when formatting the list.</param>
 166        /// <returns>A formatted string of non-null items.</returns>
 167        public string HumanizeNonNull(ListFormattingOptions? options = null, CultureInfo? culture = null)
 168        {
 3169            options ??= ListFormattingOptions.Default;
 170
 171            // Create a new options with IgnoreNullOrWhiteSpace set to true
 3172            var mergedOptions = new ListFormattingOptions
 3173            {
 3174                Conjunction = options.Conjunction,
 3175                Separator = options.Separator,
 3176                UseOxfordComma = options.UseOxfordComma,
 3177                TrimItems = options.TrimItems,
 3178                IgnoreNullOrWhiteSpace = true
 3179            };
 180
 3181            return source.Humanize(mergedOptions, culture);
 182        }
 183    }
 184}
 185

Methods/Properties

BuildOptions()
Humanize(System.Collections.Generic.IEnumerable`1<T>,MyNet.Humanizer.Formatting.Collections.ListConjunction,System.String,System.Globalization.CultureInfo)
Humanize(System.Collections.Generic.IEnumerable`1<T>,System.Func`2<T,System.String>,MyNet.Humanizer.Formatting.Collections.ListConjunction,System.String,System.Globalization.CultureInfo)
Humanize(System.Collections.Generic.IEnumerable`1<T>,MyNet.Humanizer.Formatting.Collections.ListFormattingOptions,System.Globalization.CultureInfo)
Humanize(System.Collections.Generic.IEnumerable`1<T>,System.Func`2<T,System.String>,MyNet.Humanizer.Formatting.Collections.ListFormattingOptions,System.Globalization.CultureInfo)
HumanizeWith(System.Collections.Generic.IEnumerable`1<T>,System.String,System.String,MyNet.Humanizer.Formatting.Collections.ListFormattingOptions,System.Globalization.CultureInfo)
HumanizeWith(System.Collections.Generic.IEnumerable`1<T>,System.Func`2<T,System.String>,System.String,System.String,MyNet.Humanizer.Formatting.Collections.ListFormattingOptions,System.Globalization.CultureInfo)
HumanizeInParentheses(System.Collections.Generic.IEnumerable`1<T>,MyNet.Humanizer.Formatting.Collections.ListFormattingOptions,System.Globalization.CultureInfo)
HumanizeInBrackets(System.Collections.Generic.IEnumerable`1<T>,MyNet.Humanizer.Formatting.Collections.ListFormattingOptions,System.Globalization.CultureInfo)
HumanizeInBraces(System.Collections.Generic.IEnumerable`1<T>,MyNet.Humanizer.Formatting.Collections.ListFormattingOptions,System.Globalization.CultureInfo)
HumanizeNonNull(System.Collections.Generic.IEnumerable`1<T>,MyNet.Humanizer.Formatting.Collections.ListFormattingOptions,System.Globalization.CultureInfo)