| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TextTruncationExtensions.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.Truncation; |
| | | 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 TextTruncationExtensions |
| | | 17 | | { |
| | | 18 | | extension(TextPipeline pipeline) |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Transforms a string using the provided truncation transform. Transformations are applied in the provided ord |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="transform">The truncation transform to apply.</param> |
| | | 24 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | | 25 | | public TextPipeline Truncate(ITextTruncator transform) |
| | | 26 | | { |
| | 3 | 27 | | ArgumentNullException.ThrowIfNull(transform); |
| | 0 | 28 | | return pipeline.Apply(transform); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Truncate the string. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="length">The maximum length of the string.</param> |
| | | 35 | | /// <param name="truncationString">The string to append when truncating.</param> |
| | | 36 | | /// <param name="direction">The direction from which to truncate.</param> |
| | | 37 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | | 38 | | public TextPipeline Truncate(int length, string truncationString = "…", TruncateFrom direction = TruncateFrom.Ri |
| | 0 | 39 | | => pipeline.Truncate(new FixedLengthTextTruncator( |
| | 0 | 40 | | new() |
| | 0 | 41 | | { |
| | 0 | 42 | | Length = length, |
| | 0 | 43 | | TruncationString = truncationString, |
| | 0 | 44 | | Direction = direction |
| | 0 | 45 | | })); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Truncate the string to a fixed number of words. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="count">The number of words to keep.</param> |
| | | 51 | | /// <param name="truncationString">The string to append when truncating.</param> |
| | | 52 | | /// <param name="direction">The direction from which to truncate.</param> |
| | | 53 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | | 54 | | public TextPipeline TruncateWords(int count, string truncationString = "…", TruncateFrom direction = TruncateFro |
| | 0 | 55 | | => pipeline.Truncate(new FixedNumberOfWordsTextTruncator( |
| | 0 | 56 | | new() |
| | 0 | 57 | | { |
| | 0 | 58 | | Length = count, |
| | 0 | 59 | | TruncationString = truncationString, |
| | 0 | 60 | | Direction = direction |
| | 0 | 61 | | })); |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Truncate the string to a fixed number of characters. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="count">The number of characters to keep.</param> |
| | | 67 | | /// <param name="truncationString">The string to append when truncating.</param> |
| | | 68 | | /// <param name="direction">The direction from which to truncate.</param> |
| | | 69 | | /// <returns>The transformed TextPipeline instance.</returns> |
| | | 70 | | public TextPipeline TruncateCharacters(int count, string truncationString = "…", TruncateFrom direction = Trunca |
| | 0 | 71 | | => pipeline.Truncate(new FixedNumberOfCharactersTextTruncator( |
| | 0 | 72 | | new() |
| | 0 | 73 | | { |
| | 0 | 74 | | Length = count, |
| | 0 | 75 | | TruncationString = truncationString, |
| | 0 | 76 | | Direction = direction |
| | 0 | 77 | | })); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | extension(string input) |
| | | 81 | | { |
| | | 82 | | /// <summary> |
| | | 83 | | /// Transforms a string using the provided truncation transform. Transformations are applied in the provided ord |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="transform">The truncation transform to apply.</param> |
| | | 86 | | /// <param name="culture">The culture to use for the truncation.</param> |
| | | 87 | | /// <returns>The transformed string.</returns> |
| | | 88 | | public string Truncate(ITextTruncator transform, CultureInfo? culture = null) |
| | | 89 | | { |
| | 294 | 90 | | ArgumentNullException.ThrowIfNull(transform); |
| | 291 | 91 | | return transform.Apply(input, culture.OrCurrent()); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Truncate the string. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name="length">The maximum length of the string.</param> |
| | | 98 | | /// <param name="truncationString">The string to append when truncating.</param> |
| | | 99 | | /// <param name="direction">The direction from which to truncate.</param> |
| | | 100 | | /// <returns>The transformed string.</returns> |
| | | 101 | | public string Truncate(int length, string truncationString = "…", TruncateFrom direction = TruncateFrom.Right) |
| | 117 | 102 | | => input.Truncate(new FixedLengthTextTruncator( |
| | 117 | 103 | | new() |
| | 117 | 104 | | { |
| | 117 | 105 | | Length = length, |
| | 117 | 106 | | TruncationString = truncationString, |
| | 117 | 107 | | Direction = direction |
| | 117 | 108 | | })); |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Truncate the string to a fixed number of words. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="count">The number of words to keep.</param> |
| | | 114 | | /// <param name="truncationString">The string to append when truncating.</param> |
| | | 115 | | /// <param name="direction">The direction from which to truncate.</param> |
| | | 116 | | /// <returns>The transformed string.</returns> |
| | | 117 | | public string TruncateWords(int count, string truncationString = "…", TruncateFrom direction = TruncateFrom.Righ |
| | 90 | 118 | | => input.Truncate(new FixedNumberOfWordsTextTruncator( |
| | 90 | 119 | | new() |
| | 90 | 120 | | { |
| | 90 | 121 | | Length = count, |
| | 90 | 122 | | TruncationString = truncationString, |
| | 90 | 123 | | Direction = direction |
| | 90 | 124 | | })); |
| | | 125 | | |
| | | 126 | | /// <summary> |
| | | 127 | | /// Truncate the string to a fixed number of characters. |
| | | 128 | | /// </summary> |
| | | 129 | | /// <param name="count">The number of characters to keep.</param> |
| | | 130 | | /// <param name="truncationString">The string to append when truncating.</param> |
| | | 131 | | /// <param name="direction">The direction from which to truncate.</param> |
| | | 132 | | /// <returns>The transformed string.</returns> |
| | | 133 | | public string TruncateCharacters(int count, string truncationString = "…", TruncateFrom direction = TruncateFrom |
| | 84 | 134 | | => input.Truncate(new FixedNumberOfCharactersTextTruncator( |
| | 84 | 135 | | new() |
| | 84 | 136 | | { |
| | 84 | 137 | | Length = count, |
| | 84 | 138 | | TruncationString = truncationString, |
| | 84 | 139 | | Direction = direction |
| | 84 | 140 | | })); |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | |