Skip to content

Instantly share code, notes, and snippets.

@ngerakines
Created February 11, 2010 21:19
Show Gist options
  • Select an option

  • Save ngerakines/301967 to your computer and use it in GitHub Desktop.

Select an option

Save ngerakines/301967 to your computer and use it in GitHub Desktop.
A small example in creating string slugs.
-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