Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sluggify | Elm
import Html exposing (..)
import Char
main = text <| sluggify "Sluggify's Example String!"
sluggify : String -> String
sluggify string =
string
|> removeSpecialCharacters
|> convertSpacesToDashes
|> String.toLower
removeSpecialCharacters : String -> String
removeSpecialCharacters string =
let
charList : List Char
charList = String.toList string
isNormalChar : Char -> Bool
isNormalChar char =
Char.isUpper char
|| Char.isLower char
|| Char.isDigit char
|| char == ' '
|| char == '-'
newList : List Char
newList =
List.filter isNormalChar charList
in
String.fromList newList
convertSpacesToDashes : String -> String
convertSpacesToDashes string =
let
charList : List Char
charList = String.toList string
ifSpaceThenDash : Char -> Char
ifSpaceThenDash char =
if char == ' ' then
'-'
else
char
newList : List Char
newList =
List.map ifSpaceThenDash charList
in
String.fromList newList
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment