< Summary

Information
Class: MyNet.Text.Slugification.SlugifyTransform
Assembly: MyNet.Text
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Slugification/SlugifyTransform.cs
Tag: 323_28699572109
Line coverage
94%
Covered lines: 18
Uncovered lines: 1
Coverable lines: 19
Total lines: 57
Line coverage: 94.7%
Branch coverage
70%
Covered branches: 17
Total branches: 24
Branch coverage: 70.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Apply(...)70.83%242494.44%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Slugification/SlugifyTransform.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="SlugifyTransform.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Globalization;
 9using System.Text;
 10using MyNet.Text.Normalization;
 11
 12namespace MyNet.Text.Slugification;
 13
 14/// <summary>
 15/// Converts text to a URL-friendly slug.
 16/// </summary>
 17public sealed class SlugifyTransform(TextSlugifierOptions options) : ITextSlugifierTransform
 18{
 319    private static readonly DiacriticsRemovalTransform DiacriticsRemoval = new();
 20
 21    /// <inheritdoc/>
 22    public string Apply(string input, CultureInfo culture)
 23    {
 1524        ArgumentNullException.ThrowIfNull(input);
 1525        ArgumentNullException.ThrowIfNull(culture);
 26
 1527        if (string.IsNullOrWhiteSpace(input))
 028            return string.Empty;
 29
 1530        var value = options.RemoveDiacritics ? DiacriticsRemoval.Apply(input, culture) : input;
 1531        var sb = new StringBuilder(value.Length);
 1532        var wroteSeparator = false;
 33
 48634        foreach (var ch in value)
 35        {
 22836            if (char.IsLetterOrDigit(ch))
 37            {
 19538                sb.Append(ch);
 19539                wroteSeparator = false;
 19540                continue;
 41            }
 42
 3343            if (char.IsWhiteSpace(ch) || ch == '_' || ch == '-' || ch == '.' || ch == '/')
 44            {
 3345                if (sb.Length > 0 && !wroteSeparator)
 46                {
 2747                    sb.Append(options.Separator);
 2748                    wroteSeparator = true;
 49                }
 50            }
 51        }
 52
 1553        var slug = sb.ToString().Trim(options.Separator);
 1554        return options.Lowercase ? slug.ToLower(culture) : slug;
 55    }
 56}
 57