From e9ecdf582e6babb484ab247cd18f30dde0e13692 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Wed, 14 Sep 2022 15:53:27 -0700 Subject: [PATCH] Fix casing in route matching. --- codegen/Generate.elm | 42 +++++++++++++++++++++++++++-- codegen/elm.json | 1 + src/Pages/Internal/RoutePattern.elm | 22 ++++++++++++--- 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/codegen/Generate.elm b/codegen/Generate.elm index ea198348..7f34977a 100644 --- a/codegen/Generate.elm +++ b/codegen/Generate.elm @@ -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 diff --git a/codegen/elm.json b/codegen/elm.json index 9126cbde..e560557c 100644 --- a/codegen/elm.json +++ b/codegen/elm.json @@ -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" diff --git a/src/Pages/Internal/RoutePattern.elm b/src/Pages/Internal/RoutePattern.elm index 0df84010..e591739e 100644 --- a/src/Pages/Internal/RoutePattern.elm +++ b/src/Pages/Internal/RoutePattern.elm @@ -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