slug

Type-safe slugs for Yesod ecosystem https://github.com/mrkkrp/slug

LTS Haskell 9.5:0.1.7
Stackage Nightly 2017-09-20:0.1.7
Latest on Hackage:0.1.7
BSD3 licensed and maintained by Mark Karpov

Module documentation for 0.1.7

Slug

License BSD3 Hackage Stackage Nightly Stackage LTS Build Status Coverage Status

This is a slug implementation that plays nicely with the Yesod 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 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 that there is a valid slug inside. Valid slug has the following qualities:

  • it's not empty;

  • it consists only of alpha-numeric groups of characters (words) separated by '-' dashes in such a way that entire slug cannot start or end in a dash and also two dashes in a row cannot be found;

  • every character with defined notion of case is lower-cased.

To use the package with persistent models, just import Web.Slug and add it to model file:

MyEntity
  slug Slug
  …

Use it in route file like this:

/post/#Slug PostR GET

In Haskell code, create slugs from Text with mkSlug and extract their textual representation with unSlug. The following property holds:

mkSlug = mkSlug >=> mkSlug . unSlug

License

Copyright © 2015–2017 Mark Karpov

Distributed under BSD 3 clause license.

Changes

Slug 0.1.7

  • Defined Semigroup instance for Slug.

  • Improved documentation and metadata.

Slug 0.1.6

  • Allowed Aeson 1.1.

  • Switched to Hspec for test suite.

  • Made public Arbitrary instance for Slug.

  • Derived Eq for SlugException.

  • Drop support for GHC 7.6.

  • Started to use the derived Show instance for SlugException. Human-friendly version goes to Exception's method displayException instead.

  • Added instances of ToHttpApiData and FromHttpApiData for Slug.

Slug 0.1.5

  • Allow Aeson 1.0.

Slug 0.1.4

  • Derive Ord and Data instances for Slug.

Slug 0.1.3

  • Export plain function unSlug instead of record selector unSlug.

Slug 0.1.2

  • Improved error messages in parseJSON.

Slug 0.1.1

  • Add Read instance of Slug.

  • Add parseSlug and truncateSlug functions.

  • Functions (including instance methods) that parse Text that must be formatted as valid slug are case-sensitive now.

Slug 0.1.0

  • Initial release.
comments powered byDisqus