Created
February 28, 2014 13:58
-
-
Save oal/9271533 to your computer and use it in GitHub Desktop.
Simple slugify function for D
This file contains hidden or 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
import std.stdio; | |
import std.uni; | |
import std.ascii; | |
import std.string; | |
import std.algorithm; | |
import std.range; | |
string slugify(string text) { | |
text = normalize!NFKD(text); | |
auto slug = ""; | |
auto lettersAndDigits = letters ~ digits; | |
auto whitespace = ['-', ' ', '_']; | |
foreach(v; text) { | |
if(lettersAndDigits.canFind(v)) { | |
slug ~= v; | |
} else if (whitespace.canFind(v) && slug.back != '-') { | |
slug ~= '-'; | |
} | |
} | |
return slug.toLower(); | |
} | |
void main() { | |
} | |
unittest { | |
assert(slugify("Du må ikke sove.") == "du-ma-ikke-sove"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment