< Summary

Information
Class: MyNet.Humanizer.Formatting.Collections.ListFormattingOptionsBuilder
Assembly: MyNet.Humanizer
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Formatting/Collections/ListFormattingOptionsBuilder.cs
Tag: 323_28699572109
Line coverage
83%
Covered lines: 30
Uncovered lines: 6
Coverable lines: 36
Total lines: 167
Line coverage: 83.3%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
WithSeparator(...)50%22100%
WithOxfordComma()100%11100%
WithoutOxfordComma()100%11100%
WithConjunction(...)100%11100%
WithAnd()100%210%
WithOr()100%11100%
WithNoConjunction()100%210%
WithAmpersand()100%210%
WithTrimming()100%11100%
WithoutTrimming()100%11100%
IgnoreNullOrWhiteSpace()100%11100%
IncludeNullOrWhiteSpace()100%11100%
Build()100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Formatting/Collections/ListFormattingOptionsBuilder.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ListFormattingOptionsBuilder.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8
 9namespace 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>
 19public sealed class ListFormattingOptionsBuilder
 20{
 3321    private string _separator = ", ";
 22    private bool _useOxfordComma;
 3323    private bool _trimItems = true;
 3324    private bool _ignoreNullOrWhiteSpace = true;
 3325    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    {
 634        _separator = separator ?? throw new ArgumentNullException(nameof(separator));
 635        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    {
 648        _useOxfordComma = true;
 649        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    {
 358        _useOxfordComma = false;
 359        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    {
 369        _conjunction = conjunction;
 370        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    {
 079        _conjunction = ListConjunction.And;
 080        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    {
 389        _conjunction = ListConjunction.Or;
 390        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    {
 099        _conjunction = ListConjunction.None;
 0100        return this;
 101    }
 102
 103    /// <summary>
 104    /// Uses the ampersand (&amp;) as the conjunction.
 105    /// </summary>
 106    /// <returns>This builder instance for method chaining.</returns>
 107    public ListFormattingOptionsBuilder WithAmpersand()
 108    {
 0109        _conjunction = ListConjunction.Ampersand;
 0110        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    {
 3119        _trimItems = true;
 3120        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    {
 6129        _trimItems = false;
 6130        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    {
 3139        _ignoreNullOrWhiteSpace = true;
 3140        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    {
 6149        _ignoreNullOrWhiteSpace = false;
 6150        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()
 30158        => new()
 30159        {
 30160            Separator = _separator,
 30161            UseOxfordComma = _useOxfordComma,
 30162            Conjunction = _conjunction,
 30163            TrimItems = _trimItems,
 30164            IgnoreNullOrWhiteSpace = _ignoreNullOrWhiteSpace
 30165        };
 166}
 167