From 3efc71c977f20f27c5bccfefa57af96076ddca99 Mon Sep 17 00:00:00 2001 From: Alex Perkins Date: Wed, 2 Nov 2022 18:43:32 -0600 Subject: [PATCH] remove trailing dash --- src/Nri/Ui/Util.elm | 36 +++++++++++++++++++++--------------- tests/Spec/Nri/Ui/Util.elm | 12 ++++++++---- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/Nri/Ui/Util.elm b/src/Nri/Ui/Util.elm index d7b523d4..ac5f2749 100644 --- a/src/Nri/Ui/Util.elm +++ b/src/Nri/Ui/Util.elm @@ -34,15 +34,15 @@ Ensures that nonletter characters are cut from the front and replaces bad charac safeIdString : String -> String safeIdString = let - nonAlphaAtStartOfString = + nonAlphaAtStart = -- From the start of the string, match the contiguous block of -- not ASCII letters at the start of the String. - -- Note majiscule letters are before miniscule in the range, - -- so [A-z] is the same as [A-Za-z] as above in removePunctuation - "^[^A-z]+" + -- Why [a-zA-Z] and not [A-z]? + -- There are punctuation characters in that range: [\]^_` all come after Z and before a + "^[^a-zA-Z]+" nonAlphaNumUnderscoreHyphenAnywhere = - -- match any contiguous block of letters that aren't any of + -- any contiguous block of characters that aren't any of -- + ASCII letters -- + numbers -- + underscore @@ -50,30 +50,36 @@ safeIdString = -- This does not need the + at the end; Regex.replace is global by default -- but we pay a penalty for calling the replacement function, so -- calling it once per contiguous group is an easy way to cut down on that. - "[^A-z0-9_-]+" + "[^a-zA-Z0-9_-]+" anyOfThese strs = "(" ++ String.join "|" strs ++ ")" unsafeChar = - [ nonAlphaAtStartOfString + [ nonAlphaAtStart , nonAlphaNumUnderscoreHyphenAnywhere ] |> anyOfThese |> regexFromString + nonAlphaNumAtEnd = + -- any contiguous block of letters that aren't any of + -- + ASCII letters + -- + numbers + regexFromString "[^a-zA-Z0-9]+$" + collapsePunctuationToOne = - -- match any contiguous group of either underscore or hyphen-minus characters regexFromString "[_-]+" in - Regex.replace unsafeChar - (\{ index } -> - if index == 0 then - "" + Regex.replace nonAlphaNumAtEnd (always "") + >> Regex.replace unsafeChar + (\{ index } -> + if index == 0 then + "" - else - "-" - ) + else + "-" + ) >> Regex.replace collapsePunctuationToOne (always "-") >> String.toLower diff --git a/tests/Spec/Nri/Ui/Util.elm b/tests/Spec/Nri/Ui/Util.elm index 0a17713d..c1f339d8 100644 --- a/tests/Spec/Nri/Ui/Util.elm +++ b/tests/Spec/Nri/Ui/Util.elm @@ -15,19 +15,23 @@ spec = |> Expect.equal "enable-text-to-speech-for-angela-s-account" , test "removes leading non alpha characters" <| \() -> - Util.safeIdString "#@!!232now we are in business" + Util.safeIdString "#@!!232now we are in business__--__" |> Expect.equal "now-we-are-in-business" + , test "removes trailing non alphanum characters" <| + \() -> + Util.safeIdString "#@!!232now we are in business__--__123!!@&*%^ @" + |> Expect.equal "now-we-are-in-business-123" , test "hard mode" <| \() -> Util.safeIdString "!@#21something else interesting321$ ... hi" |> Expect.equal "something-else-interesting321-hi" - , test "with capitol letters" <| + , test "with capital letters" <| \() -> Util.safeIdString "1232!@#%#@JFEKLfds-----SFJK3@#@jj23FDS........''''\"\"***" - |> Expect.equal "jfeklfds-sfjk3-jj23fds-" + |> Expect.equal "jfeklfds-sfjk3-jj23fds" , test "lotsa hyphens and dashes" <| \() -> - Util.safeIdString "hellO----_______---HOw----___---____--ArE------___You___--__--__Today" + Util.safeIdString "--__--hellO----_______---HOw----___---____--ArE------___You___--__--__Today" |> Expect.equal "hello-how-are-you-today" ] ]