namespace DocFx.Plugin.LastModified.Helpers;
using System.Linq;
///
/// Extensions for .
///
public static class StringHelper
{
///
/// Truncates the string to match the specified .
///
///
/// This method is retrieved and modified from Humanizr/Humanizer.
/// MIT License (c)
/// Copyright (c) .NET Foundation and Contributors
///
/// The string to truncate.
/// The target maximum length of the string.
///
/// A truncated string based on the specified.
///
public static string Truncate(this string value, int length)
{
const string truncationString = "...";
if (value.Length == 0)
{
return value;
}
var alphaNumericalCharactersProcessed = 0;
if (value.ToCharArray().Count(char.IsLetterOrDigit) <= length)
{
return value;
}
for (var i = 0; i < value.Length - truncationString.Length; i++)
{
if (char.IsLetterOrDigit(value[i]))
{
alphaNumericalCharactersProcessed++;
}
if (alphaNumericalCharactersProcessed + truncationString.Length == length)
{
return value[.. (i + 1)] + truncationString;
}
}
return value;
}
}