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

Commit

Permalink
Some cosmetics whims and tweaks
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
mrkkrp committed May 23, 2017
1 parent 9b773b8 commit 75a6d73
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
[![Coverage Status](https://coveralls.io/repos/mrkkrp/slug/badge.svg?branch=master&service=github)](https://coveralls.io/github/mrkkrp/slug?branch=master)

This is a [slug](https://en.wikipedia.org/wiki/Semantic_URL#Slug)
implementation that plays nicely with [Yesod](http://www.yesodweb.com/)
implementation that plays nicely with the [Yesod](http://www.yesodweb.com/)
ecosystem. Although it's fairly easy to write this thing, slugs are useful
and general enough to be coded once and be used again and again. So this
little package eliminates some boilerplate you might find yourself writing.

## Quick start

The package provides the data type `Slug` that is an instance of various
type classes, so it can be used with Persistent or as part of a route. It
type classes, so it can be used with Persistent or as a part of a route. It
also works with the `aeson` package.

The slugs are completely type-safe. When you have a `Slug`, you can be sure
Expand Down
18 changes: 10 additions & 8 deletions Web/Slug.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Web.Slug
where

import Control.Exception (Exception (..))
import Control.Monad ((>=>), liftM)
import Control.Monad
import Control.Monad.Catch (MonadThrow (..))
import Data.Aeson.Types (ToJSON (..), FromJSON (..))
import Data.Char (isAlphaNum)
Expand All @@ -42,7 +42,7 @@ import qualified Data.Aeson as A
import qualified Data.Text as T

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
import Control.Applicative
#endif

-- | This exception is thrown by 'mkSlug' when its input cannot be converted
Expand Down Expand Up @@ -79,7 +79,7 @@ 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
-- applied. The argument of this function can be title of an article or
-- something like that.
--
-- Note that the result is inside 'MonadThrow', that means you can just get
Expand Down Expand Up @@ -108,7 +108,8 @@ unSlug (Slug x) = x

getSlugWords :: Text -> [Text]
getSlugWords = T.words . T.toLower . T.map f . T.replace "'" ""
where f x = if isAlphaNum x then x else ' '
where
f x = if isAlphaNum x then x else ' '

-- | Convert a 'Text' into a 'Slug' only when it is already valid slug.
--
Expand All @@ -117,10 +118,11 @@ getSlugWords = T.words . T.toLower . T.map f . T.replace "'" ""

parseSlug :: MonadThrow m => Text -> m Slug
parseSlug v = mkSlug v >>= check
where check s =
if unSlug s == v
then return s
else throwM (InvalidSlug v)
where
check s =
if unSlug s == v
then return s
else throwM (InvalidSlug v)

-- | Ensure that given 'Slug' is not longer than given maximum number of
-- characters. If truncated slug ends in a dash, remove that dash too. (Dash
Expand Down

0 comments on commit 75a6d73

Please sign in to comment.