Fix casing in route matching.

This commit is contained in:
Dillon Kearns 2022-09-14 15:53:27 -07:00
parent 3b9da87723
commit e9ecdf582e
3 changed files with 60 additions and 5 deletions

View File

@ -20,6 +20,7 @@ import Gen.String
import Gen.Tuple
import Pages.Internal.RoutePattern as RoutePattern exposing (RoutePattern)
import Pretty
import Regex exposing (Regex)
type alias Flags =
@ -314,7 +315,7 @@ routeToPath routes =
(\param ->
case param of
RoutePattern.StaticParam name ->
[ Elm.string name ]
[ Elm.string (toKebab name) ]
|> Elm.list
RoutePattern.DynamicParam name ->
@ -342,7 +343,7 @@ routeToPath routes =
(\param ->
case param of
RoutePattern.StaticParam name ->
[ Elm.string name ]
[ Elm.string (toKebab name) ]
|> Elm.list
RoutePattern.DynamicParam name ->
@ -402,6 +403,43 @@ expose declaration =
}
{-| Decapitalize the first letter of a string.
decapitalize "This is a phrase" == "this is a phrase"
decapitalize "Hello, World" == "hello, World"
-}
decapitalize : String -> String
decapitalize word =
-- Source: https://github.com/elm-community/string-extra/blob/4.0.1/src/String/Extra.elm
changeCase Char.toLower word
{-| Change the case of the first letter of a string to either uppercase or
lowercase, depending of the value of `wantedCase`. This is an internal
function for use in `toSentenceCase` and `decapitalize`.
-}
changeCase : (Char -> Char) -> String -> String
changeCase mutator word =
-- Source: https://github.com/elm-community/string-extra/blob/4.0.1/src/String/Extra.elm
String.uncons word
|> Maybe.map (\( head, tail ) -> String.cons (mutator head) tail)
|> Maybe.withDefault ""
toKebab : String -> String
toKebab string =
string
|> decapitalize
|> String.trim
|> Regex.replace (regexFromString "([A-Z])") (.match >> String.append "-")
|> Regex.replace (regexFromString "[_-\\s]+") (always "-")
|> String.toLower
regexFromString : String -> Regex
regexFromString =
Regex.fromString >> Maybe.withDefault Regex.never
port onSuccessSend : List File -> Cmd msg

View File

@ -11,6 +11,7 @@
"elm/core": "1.0.5",
"elm/html": "1.0.0",
"elm/json": "1.1.3",
"elm/regex": "1.0.0",
"mdgriffith/elm-codegen": "2.0.0",
"the-sett/elm-pretty-printer": "3.0.0",
"the-sett/elm-syntax-dsl": "6.0.2"

View File

@ -13,6 +13,7 @@ import Elm
import Elm.Annotation exposing (Annotation)
import Elm.CodeGen
import Html exposing (Html)
import Regex exposing (Regex)
{-| -}
@ -145,7 +146,7 @@ routeToBranch route =
(\segment ->
case segment of
StaticSegment name ->
Elm.CodeGen.stringPattern (decapitalize name)
Elm.CodeGen.stringPattern (toKebab name)
DynamicSegment name ->
Elm.CodeGen.varPattern (decapitalize name)
@ -175,7 +176,7 @@ routeToBranch route =
(\segment ->
case segment of
StaticSegment name ->
Elm.CodeGen.stringPattern (decapitalize name)
Elm.CodeGen.stringPattern (toKebab name)
DynamicSegment name ->
Elm.CodeGen.varPattern (decapitalize name)
@ -196,7 +197,7 @@ routeToBranch route =
(\segment ->
case segment of
StaticSegment name ->
Elm.CodeGen.stringPattern (decapitalize name)
Elm.CodeGen.stringPattern (toKebab name)
DynamicSegment name ->
Elm.CodeGen.varPattern (decapitalize name)
@ -573,3 +574,18 @@ unconsPattern list =
)
listFirst
listRest
toKebab : String -> String
toKebab string =
string
|> decapitalize
|> String.trim
|> Regex.replace (regexFromString "([A-Z])") (.match >> String.append "-")
|> Regex.replace (regexFromString "[_-\\s]+") (always "-")
|> String.toLower
regexFromString : String -> Regex
regexFromString =
Regex.fromString >> Maybe.withDefault Regex.never