Skip to content

Instantly share code, notes, and snippets.

@oal
Created February 28, 2014 13:58
Show Gist options
  • Save oal/9271533 to your computer and use it in GitHub Desktop.
Save oal/9271533 to your computer and use it in GitHub Desktop.
Simple slugify function for D
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