Skip to content

Commit

Permalink
Handle umlaute and non ascii chars in toSlug
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Apr 13, 2022
1 parent bc2b663 commit 62dfeee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 10 additions & 5 deletions IHP/NameSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,16 @@ haskellKeywords = [ "_"
-- "hallo-welt"
toSlug :: Text -> Text
toSlug text =
text
|> map (\char -> if Char.isAlphaNum char then char else ' ')
|> toLower
|> words
|> intercalate "-"
text
|> toLower
|> map replaceChar
|> words
|> intercalate "-"
where
replaceChar 'ä' = 'a'
replaceChar 'ö' = 'o'
replaceChar 'ü' = 'u'
replaceChar char = if Char.isAlphaNum char && Char.isAscii char then char else ' '


-- | Transform a data-field name like @userName@ to a friendly human-readable name like @User name@
Expand Down
4 changes: 4 additions & 0 deletions Test/NameSupportSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,7 @@ tests = do
it "should make a slug string from a string" do
toSlug "IHP Release: 21.08.2020 (v21082020)" `shouldBe` "ihp-release-21-08-2020-v21082020"
toSlug "Hallo! @ Welt" `shouldBe` "hallo-welt"

it "should deal with umlaute" do
toSlug "käuferpass" `shouldBe` "kauferpass"
toSlug "äöü" `shouldBe` "aou"

0 comments on commit 62dfeee

Please sign in to comment.