| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TextCasingExtensions.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 MyNet.Primitives; |
| | | 10 | | using MyNet.Text.TextCasing; |
| | | 11 | | |
| | | 12 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 13 | | namespace MyNet.Text; |
| | | 14 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 15 | | |
| | | 16 | | public static class TextCasingExtensions |
| | | 17 | | { |
| | | 18 | | extension(TextPipeline pipeline) |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Transforms a string using the provided casing transform. Transformations are applied in the provided order. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="transform">The casing transform to apply.</param> |
| | | 24 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | 0 | 25 | | public TextPipeline Case(ITextCasingTransform transform) => pipeline.Apply(transform); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Transforms the string to lower case. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | 0 | 31 | | public TextPipeline LowerCase() => pipeline.Apply(Casing.LowerCase); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Transforms the string to upper case. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | 0 | 37 | | public TextPipeline UpperCase() => pipeline.Apply(Casing.UpperCase); |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Transforms the string to title case. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | 0 | 43 | | public TextPipeline TitleCase() => pipeline.Apply(Casing.TitleCase); |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Transforms the string to sentence case. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | 0 | 49 | | public TextPipeline SentenceCase() => pipeline.Apply(Casing.SentenceCase); |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Transforms the string to PascalCase. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | 3 | 55 | | public TextPipeline PascalCase() => pipeline.Apply(Casing.PascalCase); |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Transforms the string to camelCase. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | 3 | 61 | | public TextPipeline CamelCase() => pipeline.Apply(Casing.CamelCase); |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Transforms the string to snake_case. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | 3 | 67 | | public TextPipeline SnakeCase() => pipeline.Apply(Casing.SnakeCase); |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Transforms the string to kebab-case. |
| | | 71 | | /// </summary> |
| | | 72 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | 3 | 73 | | public TextPipeline KebabCase() => pipeline.Apply(Casing.KebabCase); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | extension(string input) |
| | | 77 | | { |
| | | 78 | | /// <summary> |
| | | 79 | | /// Transforms a string using the provided casing transform. Transformations are applied in the provided order. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="casing">The casing to apply.</param> |
| | | 82 | | /// <param name="culture">The culture to use for the transformation.</param> |
| | | 83 | | /// <returns>The transformed string.</returns> |
| | | 84 | | public string ApplyCase(LetterCasing casing, CultureInfo? culture = null) |
| | | 85 | | { |
| | 171 | 86 | | var effectiveCulture = culture.OrCurrent(); |
| | 171 | 87 | | return casing switch |
| | 171 | 88 | | { |
| | 6 | 89 | | LetterCasing.Normal => input, |
| | 33 | 90 | | LetterCasing.Title => input.Apply(effectiveCulture, Casing.TitleCase), |
| | 12 | 91 | | LetterCasing.Lower => input.Apply(effectiveCulture, Casing.LowerCase), |
| | 9 | 92 | | LetterCasing.Upper => input.Apply(effectiveCulture, Casing.UpperCase), |
| | 9 | 93 | | LetterCasing.Sentence => input.Apply(effectiveCulture, Casing.SentenceCase), |
| | 33 | 94 | | LetterCasing.Pascal => input.Apply(effectiveCulture, Casing.PascalCase), |
| | 36 | 95 | | LetterCasing.Camel => input.Apply(effectiveCulture, Casing.CamelCase), |
| | 9 | 96 | | LetterCasing.Snake => input.Apply(effectiveCulture, Casing.SnakeCase), |
| | 24 | 97 | | LetterCasing.Kebab => input.Apply(effectiveCulture, Casing.KebabCase), |
| | 0 | 98 | | _ => throw new ArgumentOutOfRangeException(nameof(casing)) |
| | 171 | 99 | | }; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Transforms the string to upper case. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <returns>The transformed string.</returns> |
| | 0 | 106 | | public string ToUpperCase() => input.ApplyCase(LetterCasing.Upper); |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Transforms the string to lower case. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <returns>The transformed string.</returns> |
| | 0 | 112 | | public string ToLowerCase() => input.ApplyCase(LetterCasing.Lower); |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Transforms the string to title case. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <returns>The transformed string.</returns> |
| | 18 | 118 | | public string ToTitleCase() => input.ApplyCase(LetterCasing.Title); |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Transforms the string to sentence case. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <returns>The transformed string.</returns> |
| | 0 | 124 | | public string ToSentenceCase() => input.ApplyCase(LetterCasing.Sentence); |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Transforms the string to PascalCase. |
| | | 128 | | /// </summary> |
| | 33 | 129 | | public string ToPascalCase() => input.ApplyCase(LetterCasing.Pascal); |
| | | 130 | | |
| | | 131 | | /// <summary> |
| | | 132 | | /// Transforms the string to camelCase. |
| | | 133 | | /// </summary> |
| | 36 | 134 | | public string ToCamelCase() => input.ApplyCase(LetterCasing.Camel); |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Converts the string to snake_case. |
| | | 138 | | /// </summary> |
| | 9 | 139 | | public string ToSnakeCase() => input.ApplyCase(LetterCasing.Snake); |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Converts the string to kebab-case. |
| | | 143 | | /// </summary> |
| | 24 | 144 | | public string ToKebabCase() => input.ApplyCase(LetterCasing.Kebab); |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | |