< Summary

Information
Class: MyNet.Humanizer.Formatting.Addresses.AddressFormatterBase
Assembly: MyNet.Humanizer
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Formatting/Addresses/AddressFormatter.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 89
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Culture()100%11100%
Format(...)100%22100%
ReplaceTokens(...)100%11100%
CleanupWhitespace(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Humanizer/Formatting/Addresses/AddressFormatter.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="AddressFormatter.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 System.Text.RegularExpressions;
 12using MyNet.Geography;
 13
 14namespace MyNet.Humanizer.Formatting.Addresses;
 15
 16/// <summary>
 17/// Current implementation of <see cref="IAddressFormatter"/> that provides a standard address format using invariant cu
 18/// </summary>
 19public class AddressFormatter() : AddressFormatterBase(CultureInfo.InvariantCulture)
 20{
 21    /// <inheritdoc/>
 22    protected override string Template { get; } = "{Street}\n{PostalCode} {City}\n{Country}";
 23}
 24
 25/// <summary>
 26/// Base class for address formatters, providing common functionality and properties for derived classes.
 27/// </summary>
 28/// <param name="culture">The culture information for the address formatter.</param>
 29public abstract partial class AddressFormatterBase(CultureInfo culture) : IAddressFormatter
 30{
 31    /// <inheritdoc/>
 932    public CultureInfo Culture => culture;
 33
 34    /// <summary>
 35    /// Gets the address template used for formatting addresses. The template should contain tokens in the format of {To
 36    /// </summary>
 37    protected abstract string Template { get; }
 38
 39    /// <inheritdoc/>
 40    public string Format(Address address)
 41    {
 1842        ArgumentNullException.ThrowIfNull(address);
 43
 1544        var values = new Dictionary<string, string?>
 1545        {
 1546            ["Street"] = address.Street,
 1547            ["City"] = address.City,
 1548            ["PostalCode"] = address.PostalCode,
 1549            ["Country"] = address.Country?.Name
 1550        };
 51
 1552        var lines = Template
 1553            .Split('\n', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
 1554            .Select(line => ReplaceTokens(line, values))
 1555            .Select(CleanupWhitespace)
 1556            .Where(static line => !string.IsNullOrWhiteSpace(line));
 57
 1558        return string.Join(Environment.NewLine, lines);
 59    }
 60
 61    /// <summary>
 62    /// Replaces the tokens in the template with the corresponding values from the dictionary. Tokens are defined in the
 63    /// </summary>
 64    /// <param name="template">The template string containing tokens to be replaced.</param>
 65    /// <param name="values">A dictionary containing the values for the tokens.</param>
 66    /// <returns>The template string with tokens replaced by their corresponding values.</returns>
 4867    private static string ReplaceTokens(string template, Dictionary<string, string?> values) => TokenRegex().Replace(tem
 4868    {
 4869        var token = match.Groups[1].Value;
 4870
 4871        return values.TryGetValue(token, out var value)
 4872            ? value ?? string.Empty
 4873            : string.Empty;
 4874    });
 75
 76    /// <summary>
 77    /// Cleans up the whitespace in the provided string by replacing multiple consecutive whitespace characters with a s
 78    /// </summary>
 79    /// <param name="value">The string value to clean up.</param>
 80    /// <returns>The cleaned-up string with normalized whitespace.</returns>
 4881    private static string CleanupWhitespace(string value) => MultiWhitespaceRegex().Replace(value, " ").Trim();
 82
 83    [GeneratedRegex(@"\{(.*?)\}")]
 84    private static partial Regex TokenRegex();
 85
 86    [GeneratedRegex(@"\s+")]
 87    private static partial Regex MultiWhitespaceRegex();
 88}
 89