|
| 1 | +using System; |
| 2 | +using System.Globalization; |
| 3 | +using System.Text; |
| 4 | + |
| 5 | +namespace PowerUtils.AspNetCore.ErrorHandler.Handlers |
| 6 | +{ |
| 7 | + internal static class PropertyHandler |
| 8 | + { |
| 9 | + public static Func<string, string> Create(PropertyNamingPolicy propertyNamingPolicy) |
| 10 | + => propertyNamingPolicy switch |
| 11 | + { |
| 12 | + PropertyNamingPolicy.Original => Original, |
| 13 | + PropertyNamingPolicy.SnakeCase => SnakeCase, |
| 14 | + _ => CamelCase, |
| 15 | + }; |
| 16 | + |
| 17 | + public static string Original(string property) => property; |
| 18 | + |
| 19 | + public static string CamelCase(string property) |
| 20 | + { |
| 21 | + if(string.IsNullOrWhiteSpace(property)) |
| 22 | + { |
| 23 | + return ""; |
| 24 | + } |
| 25 | + |
| 26 | + var propertyParts = property.Split('.'); |
| 27 | + for(var count = 0; count < propertyParts.Length; count++) |
| 28 | + { |
| 29 | + // Prevent System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' for cases "Hello." |
| 30 | + if(propertyParts[count].Length == 0) |
| 31 | + { |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + propertyParts[count] = char.ToLowerInvariant(propertyParts[count][0]) + propertyParts[count][1..]; |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + return string.Join(".", propertyParts); |
| 40 | + } |
| 41 | + |
| 42 | + public static string SnakeCase(string property) |
| 43 | + { |
| 44 | + if(string.IsNullOrWhiteSpace(property)) |
| 45 | + { |
| 46 | + return ""; |
| 47 | + } |
| 48 | + |
| 49 | + var propertyParts = property.Split('.'); |
| 50 | + |
| 51 | + for(var count = 0; count < propertyParts.Length; count++) |
| 52 | + { |
| 53 | + propertyParts[count] = propertyParts[count].ToSnakeCase(); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + return string.Join(".", propertyParts); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Convert a text to snake case format |
| 62 | + /// </summary> |
| 63 | + /// <param name="input">Text to be transformed</param> |
| 64 | + /// <returns>Text transformed</returns> |
| 65 | + public static string ToSnakeCase(this string input) |
| 66 | + { // Reference1: https://stackoverflow.com/questions/63055621/how-to-convert-camel-case-to-snake-case-with-two-capitals-next-to-each-other |
| 67 | + // Reference1: https://github.com/efcore/EFCore.NamingConventions/blob/main/EFCore.NamingConventions/Internal/SnakeCaseNameRewriter.cs |
| 68 | + if(string.IsNullOrEmpty(input)) |
| 69 | + { |
| 70 | + return input; |
| 71 | + } |
| 72 | + |
| 73 | + var builder = new StringBuilder(input.Length + Math.Min(2, input.Length / 5)); |
| 74 | + var previousCategory = default(UnicodeCategory?); |
| 75 | + |
| 76 | + for(var currentIndex = 0; currentIndex < input.Length; currentIndex++) |
| 77 | + { |
| 78 | + var currentChar = input[currentIndex]; |
| 79 | + if(currentChar == '_') |
| 80 | + { |
| 81 | + builder.Append('_'); |
| 82 | + previousCategory = null; |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + var currentCategory = char.GetUnicodeCategory(currentChar); |
| 87 | + switch(currentCategory) |
| 88 | + { |
| 89 | + case UnicodeCategory.UppercaseLetter: |
| 90 | + case UnicodeCategory.TitlecaseLetter: |
| 91 | + _snakeCaseHandleUppercaseLetterAndTitlecaseLetter( |
| 92 | + input, |
| 93 | + currentIndex, |
| 94 | + previousCategory, |
| 95 | + builder |
| 96 | + ); |
| 97 | + |
| 98 | + currentChar = char.ToLower(currentChar, CultureInfo.InvariantCulture); |
| 99 | + break; |
| 100 | + |
| 101 | + case UnicodeCategory.LowercaseLetter: |
| 102 | + case UnicodeCategory.DecimalDigitNumber: |
| 103 | + if(previousCategory == UnicodeCategory.SpaceSeparator) |
| 104 | + { |
| 105 | + builder.Append('_'); |
| 106 | + } |
| 107 | + break; |
| 108 | + |
| 109 | + default: |
| 110 | + if(previousCategory != null) |
| 111 | + { |
| 112 | + previousCategory = UnicodeCategory.SpaceSeparator; |
| 113 | + } |
| 114 | + continue; |
| 115 | + } |
| 116 | + |
| 117 | + builder.Append(currentChar); |
| 118 | + previousCategory = currentCategory; |
| 119 | + } |
| 120 | + |
| 121 | + return builder.ToString(); |
| 122 | + } |
| 123 | + |
| 124 | + private static void _snakeCaseHandleUppercaseLetterAndTitlecaseLetter(string input, int currentIndex, UnicodeCategory? previousCategory, StringBuilder builder) |
| 125 | + { |
| 126 | + if(previousCategory == UnicodeCategory.SpaceSeparator || |
| 127 | + previousCategory == UnicodeCategory.LowercaseLetter || |
| 128 | + (previousCategory != UnicodeCategory.DecimalDigitNumber && |
| 129 | + previousCategory != null && |
| 130 | + currentIndex > 0 && |
| 131 | + currentIndex + 1 < input.Length && |
| 132 | + char.IsLower(input[currentIndex + 1]))) |
| 133 | + { |
| 134 | + builder.Append('_'); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments