Created
February 11, 2010 21:19
-
-
Save ngerakines/301967 to your computer and use it in GitHub Desktop.
A small example in creating string slugs.
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
| -module(slug). | |
| -compile(export_all). | |
| test() -> [ | |
| {slugify("Hello World"), "hello-world"}, | |
| {slugify(" HELLO *(^&)*(&* WORLD "), "hello-world"} | |
| ]. | |
| slugify(String) -> | |
| slugify(String, []). | |
| slugify([], Acc) -> Out = case Acc of [45 | T] -> T; _ -> Acc end, lists:reverse(Out); | |
| slugify([H | Tail], Acc) -> | |
| NewH = case H of | |
| H when H > -1, H < 10 -> H; %% Numbers stay the same | |
| H when H > 96, H < 123 -> H; %% lowercase stays the same | |
| H when H > 64, H < 91 -> H + 32; %% upercase is made lowercase | |
| _ -> 45 %% everything else gets replaced with an '-' | |
| end, | |
| NewAcc = case {NewH, Acc} of | |
| {45, []} -> []; %% Prevent leading '-' characters | |
| {45, [45 | _ ]} -> Acc; %% Prevent dupe '-' characters | |
| _ -> [NewH | Acc] | |
| end, | |
| slugify(Tail, NewAcc). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment