< Summary

Information
Class: MyNet.Text.TextTruncationExtensions
Assembly: MyNet.Text
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Extensions/TextTruncationExtensions.cs
Tag: 323_28699572109
Line coverage
52%
Covered lines: 24
Uncovered lines: 22
Coverable lines: 46
Total lines: 143
Line coverage: 52.1%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Truncate(...)100%1150%
Truncate(...)100%210%
TruncateWords(...)100%210%
TruncateCharacters(...)100%210%
Truncate(...)100%11100%
Truncate(...)100%11100%
TruncateWords(...)100%11100%
TruncateCharacters(...)100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Extensions/TextTruncationExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TextTruncationExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Globalization;
 9using MyNet.Primitives;
 10using MyNet.Text.Truncation;
 11
 12#pragma warning disable IDE0130 // Namespace does not match folder structure
 13namespace MyNet.Text;
 14#pragma warning restore IDE0130 // Namespace does not match folder structure
 15
 16public 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        {
 327            ArgumentNullException.ThrowIfNull(transform);
 028            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
 039            => pipeline.Truncate(new FixedLengthTextTruncator(
 040                new()
 041                {
 042                    Length = length,
 043                    TruncationString = truncationString,
 044                    Direction = direction
 045                }));
 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
 055            => pipeline.Truncate(new FixedNumberOfWordsTextTruncator(
 056                new()
 057                {
 058                    Length = count,
 059                    TruncationString = truncationString,
 060                    Direction = direction
 061                }));
 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
 071            => pipeline.Truncate(new FixedNumberOfCharactersTextTruncator(
 072                new()
 073                {
 074                    Length = count,
 075                    TruncationString = truncationString,
 076                    Direction = direction
 077                }));
 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        {
 29490            ArgumentNullException.ThrowIfNull(transform);
 29191            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)
 117102            => input.Truncate(new FixedLengthTextTruncator(
 117103                new()
 117104                {
 117105                    Length = length,
 117106                    TruncationString = truncationString,
 117107                    Direction = direction
 117108                }));
 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
 90118            => input.Truncate(new FixedNumberOfWordsTextTruncator(
 90119                new()
 90120                {
 90121                    Length = count,
 90122                    TruncationString = truncationString,
 90123                    Direction = direction
 90124                }));
 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
 84134            => input.Truncate(new FixedNumberOfCharactersTextTruncator(
 84135                new()
 84136                {
 84137                    Length = count,
 84138                    TruncationString = truncationString,
 84139                    Direction = direction
 84140                }));
 141    }
 142}
 143