Module

Slug

#Slug

newtype Slug

A Slug represents a string value which is guaranteed to have the following qualities:

  • it is not empty
  • it consists of alphanumeric groups of characters separated by - dashes, where the slug cannot begin or end with a dash, and there can never be two dashes in a row.
  • every character with a defined notion of case is lower-cased

Example: Slug "this-is-an-article-slug"

Instances

#generate

generate :: String -> Maybe Slug

Create a Slug from a string. This will transform the input string to be a valid slug (if it is possible to do so) by separating words with - dashes, ensuring the string does not begin or end with a dash, and ensuring there are never two dashes in a row.

Slugs are usually created for article titles and other resources which need a human-readable resource name in a URL.

> Slug.generate "My article title!"
> Just (Slug "my-article-title")

> Slug.generate "¬¬¬{}¬¬¬"
> Nothing

#parse

parse :: String -> Maybe Slug

Parse a valid slug (as a string) into a Slug. This will fail if the string is not a valid slug and does not provide the same behavior as generate.

> Slug.parse "my-article-title"
> Just (Slug "my-article-title")

> Slug.parse "My article"
> Nothing

#toString

toString :: Slug -> String

Unwrap a Slug into the string contained within, without performing any transformations.

> Slug.toString (mySlug :: Slug)
> "my-slug-i-generated"

#truncate

truncate :: Int -> Slug -> Maybe Slug

Ensure a Slug is no longer than a given number of characters. If the last character is a dash, it will also be removed. Providing a non-positive number as the length will return Nothing.

> Slug.create "My article title is long!" >>= Slug.truncate 3
> Just (Slug "my")

Modules