| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="HumanizeKeyFormatterTransform.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using System.Text.RegularExpressions; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Text.Formatting; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Converts key-like identifiers (PascalCase, camelCase, snake_case, kebab-case) into human-readable text. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed partial class HumanizeKeyFormatterTransform : ITextFormatterTransform |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="HumanizeKeyFormatterTransform"/> class. |
| | | 20 | | /// </summary> |
| | 3 | 21 | | internal HumanizeKeyFormatterTransform() { } |
| | | 22 | | |
| | | 23 | | /// <inheritdoc/> |
| | | 24 | | public string Apply(string input, CultureInfo culture) |
| | | 25 | | { |
| | 21 | 26 | | ArgumentNullException.ThrowIfNull(input); |
| | 21 | 27 | | ArgumentNullException.ThrowIfNull(culture); |
| | | 28 | | |
| | 21 | 29 | | if (input.Length == 0) |
| | 0 | 30 | | return input; |
| | | 31 | | |
| | 21 | 32 | | var normalized = SeparatorRegex().Replace(input, " "); |
| | 21 | 33 | | normalized = AcronymBoundaryRegex().Replace(normalized, " "); |
| | 21 | 34 | | normalized = CasingBoundaryRegex().Replace(normalized, " "); |
| | 21 | 35 | | normalized = LetterToDigitBoundaryRegex().Replace(normalized, " "); |
| | 21 | 36 | | normalized = DigitToLetterBoundaryRegex().Replace(normalized, " "); |
| | 21 | 37 | | normalized = MultiWhitespaceRegex().Replace(normalized, " ").Trim(); |
| | | 38 | | |
| | 21 | 39 | | if (normalized.Length == 0) |
| | 0 | 40 | | return normalized; |
| | | 41 | | |
| | 21 | 42 | | var sentence = normalized.ToLower(culture); |
| | 21 | 43 | | return sentence.Length == 1 |
| | 21 | 44 | | ? sentence.ToUpper(culture) |
| | 21 | 45 | | : 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 | | |