| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TextFormattingExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Globalization; |
| | | 9 | | using MyNet.Primitives; |
| | | 10 | | using MyNet.Text.Formatting; |
| | | 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 | | /// <summary> |
| | | 17 | | /// Provides text formatting extension methods. |
| | | 18 | | /// </summary> |
| | | 19 | | public static class TextFormattingExtensions |
| | | 20 | | { |
| | | 21 | | extension(TextPipeline pipeline) |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Converts the current text to initials. |
| | | 25 | | /// </summary> |
| | 3 | 26 | | public TextPipeline Initials() => pipeline.Apply(Formatter.Initials); |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Converts key-like identifiers (PascalCase, camelCase, snake_case, kebab-case) to human-readable text. |
| | | 30 | | /// </summary> |
| | 3 | 31 | | public TextPipeline HumanizeKey() => pipeline.Apply(Formatter.HumanizeKey); |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Applies an increment transformation to the current text using explicit options. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="options">Options used to configure increment behavior.</param> |
| | | 37 | | /// <returns>The TextPipeline instance with the applied transformation.</returns> |
| | | 38 | | public TextPipeline Increment(IncrementTransformOptions options) => |
| | 0 | 39 | | pipeline.Apply(new IncrementTransform(options)); |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Applies an increment transformation to the current text by appending an incrementing number until a unique v |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="existStrings">The collection of existing strings to check against.</param> |
| | | 45 | | /// <param name="minIncrement">The minimum increment value. Current is 1.</param> |
| | | 46 | | /// <param name="step">The step value for incrementing. Current is 1.</param> |
| | | 47 | | /// <param name="format">Optional format string for the incrementing number.</param> |
| | | 48 | | /// <returns>The TextPipeline instance with the applied transformation.</returns> |
| | | 49 | | public TextPipeline Increment(IEnumerable<string> existStrings, int minIncrement = 1, int step = 1, string? form |
| | 0 | 50 | | pipeline.Increment(new() |
| | 0 | 51 | | { |
| | 0 | 52 | | ExistingStrings = existStrings, |
| | 0 | 53 | | Kind = IncrementKind.Numeric, |
| | 0 | 54 | | MinIncrement = minIncrement, |
| | 0 | 55 | | Step = step, |
| | 0 | 56 | | NumericFormat = format |
| | 0 | 57 | | }); |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Applies an alphabetic increment transformation to the current text by appending alphabetic suffixes until a |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="existStrings">The collection of existing strings to check against.</param> |
| | | 63 | | /// <param name="minIncrement">The minimum increment value (1 for 'A', 2 for 'B', etc.). Current is 1.</param> |
| | | 64 | | /// <param name="step">The step value for incrementing. Current is 1.</param> |
| | | 65 | | /// <returns>The TextPipeline instance with the applied transformation.</returns> |
| | | 66 | | public TextPipeline IncrementAlpha(IEnumerable<string> existStrings, int minIncrement = 1, int step = 1) => |
| | 0 | 67 | | pipeline.Increment(new() |
| | 0 | 68 | | { |
| | 0 | 69 | | ExistingStrings = existStrings, |
| | 0 | 70 | | Kind = IncrementKind.Alpha, |
| | 0 | 71 | | MinIncrement = minIncrement, |
| | 0 | 72 | | Step = step |
| | 0 | 73 | | }); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | extension(string input) |
| | | 77 | | { |
| | | 78 | | /// <summary> |
| | | 79 | | /// Converts the string to initials. |
| | | 80 | | /// </summary> |
| | 3 | 81 | | public string Initials(CultureInfo? culture = null) => input.Apply(culture.OrCurrent(), Formatter.Initials); |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Converts key-like identifiers (PascalCase, camelCase, snake_case, kebab-case) to human-readable text. |
| | | 85 | | /// </summary> |
| | 15 | 86 | | public string HumanizeKey(CultureInfo? culture = null) => input.Apply(culture.OrCurrent(), Formatter.HumanizeKey |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Applies an increment transformation using explicit options. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <param name="options">Options used to configure increment behavior.</param> |
| | | 92 | | /// <param name="culture">Optional culture used for formatting numeric increments.</param> |
| | | 93 | | /// <returns>A unique incremented version of the input string.</returns> |
| | | 94 | | public string Increment(IncrementTransformOptions options, CultureInfo? culture = null) => |
| | 12 | 95 | | input.Apply(culture.OrCurrent(), new IncrementTransform(options)); |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Applies an increment transformation by appending an incrementing number until a unique value is found. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="existStrings">The collection of existing strings to check against.</param> |
| | | 101 | | /// <param name="minIncrement">The minimum increment value. Current is 1.</param> |
| | | 102 | | /// <param name="step">The step value for incrementing. Current is 1.</param> |
| | | 103 | | /// <param name="format">Optional format string for the incrementing number.</param> |
| | | 104 | | /// <returns>A new string that is an incremented version of the input string.</returns> |
| | | 105 | | public string Increment(IEnumerable<string> existStrings, int minIncrement = 1, int step = 1, string? format = n |
| | 0 | 106 | | input.Increment(new IncrementTransformOptions |
| | 0 | 107 | | { |
| | 0 | 108 | | ExistingStrings = existStrings, |
| | 0 | 109 | | Kind = IncrementKind.Numeric, |
| | 0 | 110 | | MinIncrement = minIncrement, |
| | 0 | 111 | | Step = step, |
| | 0 | 112 | | NumericFormat = format |
| | 0 | 113 | | }); |
| | | 114 | | |
| | | 115 | | /// <summary> |
| | | 116 | | /// Applies an alphabetic increment transformation by appending alphabetic suffixes until a unique value is foun |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="existStrings">The collection of existing strings to check against.</param> |
| | | 119 | | /// <param name="minIncrement">The minimum increment value (1 for 'A', 2 for 'B', etc.). Current is 1.</param> |
| | | 120 | | /// <param name="step">The step value for incrementing. Current is 1.</param> |
| | | 121 | | /// <returns>A unique incremented string with alphabetic suffix.</returns> |
| | | 122 | | public string IncrementAlpha(IEnumerable<string> existStrings, int minIncrement = 1, int step = 1) => |
| | 6 | 123 | | input.Increment(new IncrementTransformOptions |
| | 6 | 124 | | { |
| | 6 | 125 | | ExistingStrings = existStrings, |
| | 6 | 126 | | Kind = IncrementKind.Alpha, |
| | 6 | 127 | | MinIncrement = minIncrement, |
| | 6 | 128 | | Step = step |
| | 6 | 129 | | }); |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | |