| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ListFormattingOptionsBuilder.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | |
| | | 9 | | namespace MyNet.Humanizer.Formatting.Collections; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Fluent builder for configuring <see cref="ListFormattingOptions"/>. |
| | | 13 | | /// Allows for a convenient and readable way to construct formatting options. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// This builder uses a fluent API pattern to make configuration more expressive. |
| | | 17 | | /// Each method returns the builder instance to allow method chaining. |
| | | 18 | | /// </remarks> |
| | | 19 | | public sealed class ListFormattingOptionsBuilder |
| | | 20 | | { |
| | 33 | 21 | | private string _separator = ", "; |
| | | 22 | | private bool _useOxfordComma; |
| | 33 | 23 | | private bool _trimItems = true; |
| | 33 | 24 | | private bool _ignoreNullOrWhiteSpace = true; |
| | 33 | 25 | | private ListConjunction _conjunction = ListConjunction.And; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Sets the separator to use between items in the list. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="separator">The separator string (e.g., ", " or " | ").</param> |
| | | 31 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 32 | | public ListFormattingOptionsBuilder WithSeparator(string separator) |
| | | 33 | | { |
| | 6 | 34 | | _separator = separator ?? throw new ArgumentNullException(nameof(separator)); |
| | 6 | 35 | | return this; |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Enables the use of the Oxford comma (comma before the final conjunction). |
| | | 40 | | /// </summary> |
| | | 41 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 42 | | /// <example> |
| | | 43 | | /// With Oxford comma: "Apple, Banana, and Cherry". |
| | | 44 | | /// Without: "Apple, Banana and Cherry". |
| | | 45 | | /// </example> |
| | | 46 | | public ListFormattingOptionsBuilder WithOxfordComma() |
| | | 47 | | { |
| | 6 | 48 | | _useOxfordComma = true; |
| | 6 | 49 | | return this; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Disables the use of the Oxford comma. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 56 | | public ListFormattingOptionsBuilder WithoutOxfordComma() |
| | | 57 | | { |
| | 3 | 58 | | _useOxfordComma = false; |
| | 3 | 59 | | return this; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Sets the conjunction to use when formatting the list. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="conjunction">The conjunction type (And, Or, None, or Ampersand).</param> |
| | | 66 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 67 | | public ListFormattingOptionsBuilder WithConjunction(ListConjunction conjunction) |
| | | 68 | | { |
| | 3 | 69 | | _conjunction = conjunction; |
| | 3 | 70 | | return this; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Uses the "and" conjunction. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 77 | | public ListFormattingOptionsBuilder WithAnd() |
| | | 78 | | { |
| | 0 | 79 | | _conjunction = ListConjunction.And; |
| | 0 | 80 | | return this; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Uses the "or" conjunction. |
| | | 85 | | /// </summary> |
| | | 86 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 87 | | public ListFormattingOptionsBuilder WithOr() |
| | | 88 | | { |
| | 3 | 89 | | _conjunction = ListConjunction.Or; |
| | 3 | 90 | | return this; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Uses no conjunction (items separated only by the separator). |
| | | 95 | | /// </summary> |
| | | 96 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 97 | | public ListFormattingOptionsBuilder WithNoConjunction() |
| | | 98 | | { |
| | 0 | 99 | | _conjunction = ListConjunction.None; |
| | 0 | 100 | | return this; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Uses the ampersand (&) as the conjunction. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 107 | | public ListFormattingOptionsBuilder WithAmpersand() |
| | | 108 | | { |
| | 0 | 109 | | _conjunction = ListConjunction.Ampersand; |
| | 0 | 110 | | return this; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Enables trimming of items (removes leading and trailing whitespace). |
| | | 115 | | /// </summary> |
| | | 116 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 117 | | public ListFormattingOptionsBuilder WithTrimming() |
| | | 118 | | { |
| | 3 | 119 | | _trimItems = true; |
| | 3 | 120 | | return this; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Disables trimming of items. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 127 | | public ListFormattingOptionsBuilder WithoutTrimming() |
| | | 128 | | { |
| | 6 | 129 | | _trimItems = false; |
| | 6 | 130 | | return this; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Enables filtering out null, empty, or whitespace-only items. |
| | | 135 | | /// </summary> |
| | | 136 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 137 | | public ListFormattingOptionsBuilder IgnoreNullOrWhiteSpace() |
| | | 138 | | { |
| | 3 | 139 | | _ignoreNullOrWhiteSpace = true; |
| | 3 | 140 | | return this; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Disables filtering of null, empty, or whitespace-only items. |
| | | 145 | | /// </summary> |
| | | 146 | | /// <returns>This builder instance for method chaining.</returns> |
| | | 147 | | public ListFormattingOptionsBuilder IncludeNullOrWhiteSpace() |
| | | 148 | | { |
| | 6 | 149 | | _ignoreNullOrWhiteSpace = false; |
| | 6 | 150 | | return this; |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// Builds and returns the configured <see cref="ListFormattingOptions"/> instance. |
| | | 155 | | /// </summary> |
| | | 156 | | /// <returns>A new <see cref="ListFormattingOptions"/> instance with the configured values.</returns> |
| | | 157 | | public ListFormattingOptions Build() |
| | 30 | 158 | | => new() |
| | 30 | 159 | | { |
| | 30 | 160 | | Separator = _separator, |
| | 30 | 161 | | UseOxfordComma = _useOxfordComma, |
| | 30 | 162 | | Conjunction = _conjunction, |
| | 30 | 163 | | TrimItems = _trimItems, |
| | 30 | 164 | | IgnoreNullOrWhiteSpace = _ignoreNullOrWhiteSpace |
| | 30 | 165 | | }; |
| | | 166 | | } |
| | | 167 | | |