< Summary

Information
Class: MyNet.Text.Formatting.HumanizeKeyFormatterTransform
Assembly: MyNet.Text
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Formatting/HumanizeKeyFormatterTransform.cs
Tag: 323_28699572109
Line coverage
88%
Covered lines: 15
Uncovered lines: 2
Coverable lines: 17
Total lines: 66
Line coverage: 88.2%
Branch coverage
50%
Covered branches: 3
Total branches: 6
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%
Apply(...)50%6687.5%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Formatting/HumanizeKeyFormatterTransform.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="HumanizeKeyFormatterTransform.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Globalization;
 9using System.Text.RegularExpressions;
 10
 11namespace MyNet.Text.Formatting;
 12
 13/// <summary>
 14/// Converts key-like identifiers (PascalCase, camelCase, snake_case, kebab-case) into human-readable text.
 15/// </summary>
 16public sealed partial class HumanizeKeyFormatterTransform : ITextFormatterTransform
 17{
 18    /// <summary>
 19    /// Initializes a new instance of the <see cref="HumanizeKeyFormatterTransform"/> class.
 20    /// </summary>
 321    internal HumanizeKeyFormatterTransform() { }
 22
 23    /// <inheritdoc/>
 24    public string Apply(string input, CultureInfo culture)
 25    {
 2126        ArgumentNullException.ThrowIfNull(input);
 2127        ArgumentNullException.ThrowIfNull(culture);
 28
 2129        if (input.Length == 0)
 030            return input;
 31
 2132        var normalized = SeparatorRegex().Replace(input, " ");
 2133        normalized = AcronymBoundaryRegex().Replace(normalized, " ");
 2134        normalized = CasingBoundaryRegex().Replace(normalized, " ");
 2135        normalized = LetterToDigitBoundaryRegex().Replace(normalized, " ");
 2136        normalized = DigitToLetterBoundaryRegex().Replace(normalized, " ");
 2137        normalized = MultiWhitespaceRegex().Replace(normalized, " ").Trim();
 38
 2139        if (normalized.Length == 0)
 040            return normalized;
 41
 2142        var sentence = normalized.ToLower(culture);
 2143        return sentence.Length == 1
 2144            ? sentence.ToUpper(culture)
 2145            : char.ToUpper(sentence[0], culture) + sentence[1..];
 46    }
 47
 48    [GeneratedRegex(@"[\s_\-./:]+", RegexOptions.Compiled | RegexOptions.CultureInvariant)]
 49    private static partial Regex SeparatorRegex();
 50
 51    [GeneratedRegex(@"(?<=[\p{Lu}])(?=[\p{Lu}][\p{Ll}])", RegexOptions.Compiled | RegexOptions.CultureInvariant)]
 52    private static partial Regex AcronymBoundaryRegex();
 53
 54    [GeneratedRegex(@"(?<=[\p{Ll}\p{Nd}])(?=[\p{Lu}])", RegexOptions.Compiled | RegexOptions.CultureInvariant)]
 55    private static partial Regex CasingBoundaryRegex();
 56
 57    [GeneratedRegex(@"(?<=[\p{L}])(?=[\p{Nd}])", RegexOptions.Compiled | RegexOptions.CultureInvariant)]
 58    private static partial Regex LetterToDigitBoundaryRegex();
 59
 60    [GeneratedRegex(@"(?<=[\p{Nd}])(?=[\p{L}])", RegexOptions.Compiled | RegexOptions.CultureInvariant)]
 61    private static partial Regex DigitToLetterBoundaryRegex();
 62
 63    [GeneratedRegex(@"\s+", RegexOptions.Compiled | RegexOptions.CultureInvariant)]
 64    private static partial Regex MultiWhitespaceRegex();
 65}
 66