Feature/slug generator, first 3 tasks - #6
Conversation
MostafaShraief
commented
Apr 13, 2026
- Extend to custom separator slug generation.
- Implement a C# Extension Method for slug generation.
- Ensure collision prevention for identical titles.
There was a problem hiding this comment.
Pull request overview
This PR expands the slug generator library to support custom separators, adds a string extension method for hyphen-based slugs, and introduces a “unique slug” generator intended to reduce collisions for identical titles.
Changes:
- Added
CustomGenerate(text, separator)plusGenerateHyphens/GenerateUnderscoresconvenience methods. - Added
StringExtensions.ToSlug()for hyphen-separated slugs. - Added
GenerateUnique(text)and updated/expanded unit tests to cover the new APIs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| SlugGeneratorLibrary/SlugGenerator.cs | Adds custom-separator generation, convenience methods, a “unique” slug generator, and a ToSlug() extension method. |
| SlugGeneratorUnitTesting/SlugAlgorithmUnitTest.cs | Updates existing tests to use GenerateHyphens and adds coverage for ToSlug, custom separators, underscores, and uniqueness. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| text = Regex.Replace(text, @"[+()^*%#@!/\\.,|`~]+", string.Empty); | ||
| text = Regex.Replace(text, @"[\s_-]+", "-"); | ||
| text = Regex.Replace(text, @"[\s_-]+", separator.ToString()); |
There was a problem hiding this comment.
Introducing CustomGenerate changes behavior for custom separators that are currently treated as removable characters (e.g., . and * are stripped by the first regex). With separator='.', an input like "hello.world" would lose the dot instead of treating it as a separator. Consider excluding the chosen separator from the removal regex and/or treating it as a valid separator in the normalization regex.
| public static string Generate(string text) | ||
| public static string CustomGenerate(string text, char separator) | ||
| { | ||
| if (text is null) |
There was a problem hiding this comment.
instead you can use ArgumentNullException.ThrowIfNull(text); which is the modern standard way
| throw new ArgumentNullException(nameof(text)); | ||
| // append text with a GUID-based suffix to greatly reduce collision risk | ||
| string uniqueText = text + '-' + Guid.NewGuid().ToString("N"); | ||
| return GenerateHyphens(uniqueText); |
There was a problem hiding this comment.
Slugify the base text first, and then append the GUID to the already-cleaned string.
| Assert.Equal("hello", SlugGenerator.GenerateHyphens("Hello*")); | ||
| Assert.Equal("hello", SlugGenerator.GenerateHyphens("Hello()")); | ||
| Assert.Equal("helloworld", SlugGenerator.GenerateHyphens("Hello()!@#%^*+/\\.|`~,world")); | ||
| Assert.Equal("hello-world", SlugGenerator.GenerateHyphens("Hello_- ()!@#%^*+/\\.|`~,- world")); |
There was a problem hiding this comment.
try this tc:
!@# Hello World
| { | ||
| ArgumentNullException.ThrowIfNull(text); | ||
| // append text with a GUID-based suffix to greatly reduce collision risk | ||
| string uniqueText = text; |