Skip to content
This repository has been archived by the owner on Jun 22, 2018. It is now read-only.

Commit

Permalink
Define ‘Semigroup’ instance for ‘Slug’
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkkrp committed Apr 30, 2017
1 parent 9f0b3b9 commit 7694dc8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## Slug 0.1.7

* Define `Semigroup` instance for `Slug`.

## Slug 0.1.6

* Allowed Aeson 1.1.
Expand Down
4 changes: 4 additions & 0 deletions Web/Slug.hs
Expand Up @@ -29,6 +29,7 @@ import Data.Aeson.Types (ToJSON (..), FromJSON (..))
import Data.Char (isAlphaNum)
import Data.Data (Data)
import Data.Maybe (isJust, fromJust)
import Data.Semigroup
import Data.Text (Text)
import Data.Typeable (Typeable)
import Database.Persist.Class (PersistField (..))
Expand Down Expand Up @@ -74,6 +75,9 @@ instance Exception SlugException where

newtype Slug = Slug Text deriving (Eq, Ord, Data, Typeable)

instance Semigroup Slug where
x <> y = Slug (unSlug x <> "-" <> unSlug y)

-- | Create a 'Slug' from a 'Text' value, all necessary transformations are
-- applied. Argument of this function can be title of an article or
-- something like that.
Expand Down
4 changes: 4 additions & 0 deletions slug.cabal
Expand Up @@ -61,6 +61,8 @@ library
, path-pieces >= 0.1.5 && < 0.3
, persistent >= 2.0 && < 3.0
, text >= 1.0 && < 1.3
if !impl(ghc >= 8.0)
build-depends: semigroups == 0.18.*
default-extensions: OverloadedStrings
exposed-modules: Web.Slug
if flag(dev)
Expand All @@ -86,6 +88,8 @@ test-suite tests
, path-pieces >= 0.1.5 && < 0.3
, slug
, text >= 1.0 && < 1.3
if !impl(ghc >= 8.0)
build-depends: semigroups == 0.18.*
default-language: Haskell2010

source-repository head
Expand Down
7 changes: 7 additions & 0 deletions tests/Main.hs
Expand Up @@ -39,6 +39,7 @@ import Control.Monad ((>=>))
import Data.Char (isAlphaNum, isUpper)
import Data.Function (on)
import Data.Maybe (isJust, isNothing)
import Data.Semigroup
import Data.Text (Text)
import Test.Hspec
import Test.QuickCheck
Expand Down Expand Up @@ -91,6 +92,12 @@ spec = do
it "valid Slug text is a valid HTTP API data" $
property $ \slug ->
parseUrlPiece (toUrlPiece slug) === Right (slug :: Slug)
describe "Semigroup instance of Slug" $
it "the (<>) operation produces valid slugs in all cases" $
property $ \x y -> do
let slug = unSlug (x <> y)
slug' <- unSlug <$> parseSlug slug
slug' `shouldBe` slug
describe "mkSlug" $ do
it "Slug transformation in idempotent" $
property $ \x ->
Expand Down

0 comments on commit 7694dc8

Please sign in to comment.