A.String (Aja v0.5.1) View Source

Some extra helper functions for working with strings, that are not in the core String module.

Link to this section Summary

Functions

Transforms the text as a slug. Removes whitespace, special characters and converts the rest to lowercase.

Link to this section Types

Specs

mode() :: :default | :ascii | :greek

Link to this section Functions

Link to this function

slugify(string, mode \\ :default)

View Source

Specs

slugify(String.t(), mode()) :: String.t()

Transforms the text as a slug. Removes whitespace, special characters and converts the rest to lowercase.

This is typically useful to generate URLs based on content, e.g. the title of an article.

Like String.downcase/2, slugify/2 also can handle the following modes: :default (keeps unicode), :ascii or :greek.

Examples

iex> A.String.slugify("> \"It Was Me, Dio!!!\"\n")
"it-was-me-dio"
iex> A.String.slugify("Joseph Joestar a.k.a ジョジョ")
"joseph-joestar-aka-ジョジョ"
iex> A.String.slugify(<<220>>)
** (ArgumentError) Invalid string <<220>>

:ascii converts to ascii when possible or strips characters:

iex> A.String.slugify("OLÁ!\n", :ascii)
"ola"
iex> A.String.slugify("DIOの世界 -さらば友よ- ", :ascii)
"dio"

:greek handles the context sensitive sigma in Greek:

iex> A.String.slugify("\tΣΣ?")
"σσ"
iex> A.String.slugify("\tΣΣ?", :greek)
"σς"