| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ListFormatterExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Linq; |
| | | 11 | | using MyNet.Globalization.Facade; |
| | | 12 | | using MyNet.Humanizer.Formatting.Collections; |
| | | 13 | | |
| | | 14 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 15 | | namespace 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> |
| | | 21 | | public 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> |
| | 3 | 27 | | 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? |
| | 27 | 47 | | => 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 | | { |
| | 27 | 60 | | ArgumentNullException.ThrowIfNull(formatter); |
| | 27 | 61 | | 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) |
| | 15 | 72 | | => 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 | | { |
| | 72 | 85 | | ArgumentNullException.ThrowIfNull(formatter); |
| | | 86 | | |
| | 72 | 87 | | var items = source.Select(formatter).ToList(); |
| | 72 | 88 | | 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 | | { |
| | 12 | 107 | | var formatted = source.Humanize(options ?? ListFormattingOptions.Default, culture); |
| | 12 | 108 | | 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 | | { |
| | 3 | 123 | | ArgumentNullException.ThrowIfNull(formatter); |
| | 3 | 124 | | var formatted = source.Humanize(formatter, options ?? ListFormattingOptions.Default, culture); |
| | 3 | 125 | | 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) |
| | 3 | 141 | | => 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) |
| | 3 | 150 | | => 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) |
| | 3 | 159 | | => 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 | | { |
| | 3 | 169 | | options ??= ListFormattingOptions.Default; |
| | | 170 | | |
| | | 171 | | // Create a new options with IgnoreNullOrWhiteSpace set to true |
| | 3 | 172 | | var mergedOptions = new ListFormattingOptions |
| | 3 | 173 | | { |
| | 3 | 174 | | Conjunction = options.Conjunction, |
| | 3 | 175 | | Separator = options.Separator, |
| | 3 | 176 | | UseOxfordComma = options.UseOxfordComma, |
| | 3 | 177 | | TrimItems = options.TrimItems, |
| | 3 | 178 | | IgnoreNullOrWhiteSpace = true |
| | 3 | 179 | | }; |
| | | 180 | | |
| | 3 | 181 | | return source.Humanize(mergedOptions, culture); |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | |