Skip to content

Instantly share code, notes, and snippets.

@ryan-haskell
Created December 2, 2016 16:28
Show Gist options
  • Save ryan-haskell/1e9d87cf23bbe664a98f29c379a9db25 to your computer and use it in GitHub Desktop.
Save ryan-haskell/1e9d87cf23bbe664a98f29c379a9db25 to your computer and use it in GitHub Desktop.
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