Created
April 29, 2019 07:31
-
-
Save mikebrind/736680167c9458ad2eaccd2d1b9a52f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
public static class Extensions | |
{ | |
public static string ToSlug(this string value, bool toLower = true) | |
{ | |
if (value == null) | |
return ""; | |
var normalised = value.Normalize(NormalizationForm.FormKD); | |
const int maxlen = 80; | |
int len = normalised.Length; | |
bool prevDash = false; | |
var sb = new StringBuilder(len); | |
char c; | |
for (int i = 0; i < len; i++) | |
{ | |
c = normalised[i]; | |
if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) | |
{ | |
if (prevDash) | |
{ | |
sb.Append('-'); | |
prevDash = false; | |
} | |
sb.Append(c); | |
} | |
else if (c >= 'A' && c <= 'Z') | |
{ | |
if (prevDash) | |
{ | |
sb.Append('-'); | |
prevDash = false; | |
} | |
// Tricky way to convert to lowercase | |
if (toLower) | |
sb.Append((char)(c | 32)); | |
else | |
sb.Append(c); | |
} | |
else if (c == ' ' || c == ',' || c == '.' || c == '/' || c == '\\' || c == '-' || c == '_' || c == '=') | |
{ | |
if (!prevDash && sb.Length > 0) | |
{ | |
prevDash = true; | |
} | |
} | |
else | |
{ | |
string swap = ConvertEdgeCases(c, toLower); | |
if (swap != null) | |
{ | |
if (prevDash) | |
{ | |
sb.Append('-'); | |
prevDash = false; | |
} | |
sb.Append(swap); | |
} | |
} | |
if (sb.Length == maxlen) | |
break; | |
} | |
return sb.ToString(); | |
} | |
static string ConvertEdgeCases(char c, bool toLower) | |
{ | |
string swap = null; | |
switch (c) | |
{ | |
case 'ı': | |
swap = "i"; | |
break; | |
case 'ł': | |
swap = "l"; | |
break; | |
case 'Ł': | |
swap = toLower ? "l" : "L"; | |
break; | |
case 'đ': | |
swap = "d"; | |
break; | |
case 'ß': | |
swap = "ss"; | |
break; | |
case 'ø': | |
swap = "o"; | |
break; | |
case 'Þ': | |
swap = "th"; | |
break; | |
} | |
return swap; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment