From 2838568ea4447c4fe820a02f216a58bacfee0238 Mon Sep 17 00:00:00 2001 From: Johannes Hilden Date: Fri, 1 Sep 2023 01:31:55 +0300 Subject: [PATCH] Fix https://github.com/sqids/sqids-haskell/issues/4 --- src/Web/Sqids/Internal.hs | 5 +++-- test/Web/Sqids/BlocklistTests.hs | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Web/Sqids/Internal.hs b/src/Web/Sqids/Internal.hs index 2fa39ce..d59fb07 100644 --- a/src/Web/Sqids/Internal.hs +++ b/src/Web/Sqids/Internal.hs @@ -225,8 +225,9 @@ instance (MonadSqids m) => MonadSqids (SelectT r m) where -- 3. Remove words that contain characters that are not in the alphabet -- filteredBlocklist :: Text -> [Text] -> [Text] -filteredBlocklist alph ws = (Text.map toLower) <$> filter isValid ws where - isValid w = Text.length w >= 3 && Text.all (`Text.elem` alph) w +filteredBlocklist alph ws = filter isValid (Text.map toLower <$> ws) where + isValid w = Text.length w >= 3 && Text.all (`Text.elem` lowercaseAlphabet) w + lowercaseAlphabet = Text.map toLower alph decodeStep :: (Text, Text) -> Maybe (Int, (Text, Text)) decodeStep (sqid, alph) diff --git a/test/Web/Sqids/BlocklistTests.hs b/test/Web/Sqids/BlocklistTests.hs index fb9b3f6..60d1e39 100644 --- a/test/Web/Sqids/BlocklistTests.hs +++ b/test/Web/Sqids/BlocklistTests.hs @@ -61,3 +61,11 @@ testBlocklist = do it "match against a short blocklist word" $ withCustomBlocklist [ "pPQ" ] ((encode >=> decode) [1000]) `shouldBe` Right [1000] + + it "blocklist filtering in constructor" $ do + let options = defaultSqidsOptions { alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", blocklist = ["sqnmpn"] } + testFn = do + p <- encode [1, 2, 3] + q <- decode p + pure (p, q) + runSqids options testFn `shouldBe` Right ("ULPBZGBM", [1, 2, 3])