Created
December 2, 2016 16:28
-
-
Save ryan-haskell/1e9d87cf23bbe664a98f29c379a9db25 to your computer and use it in GitHub Desktop.
Sluggify | Elm
This file contains 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
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