Swiss.String (swiss v3.12.0) View Source

A few extra functions to deal with Strings. Heavily inspired by lodash.

Link to this section Summary

Link to this section Functions

Specs

deburr(String.t()) :: String.t()

Deburrs a string from unicode to its ascii equivalent.

Examples

iex> Swiss.String.deburr "hola señor!"
"hola senor!"
Link to this function

insert_at(string, pos, substr)

View Source

Specs

insert_at(String.t(), integer(), String.t()) :: String.t()

Inserts a substring into another string at the given position.

Examples

iex> Swiss.String.insert_at "Banas", 2, "na"
"Bananas"

iex> Swiss.String.insert_at "800", -2, "."
"8.00"

Specs

kebab_case(String.t()) :: String.t()

Converts a string into kebab-case.

Examples

iex> Swiss.String.kebab_case "Foo Bar"
"foo-bar"

iex> Swiss.String.kebab_case "--foo-bar--"
"foo-bar"

iex> Swiss.String.kebab_case "__FOO_BAR__"
"foo-bar"

iex> Swiss.String.kebab_case "FooBar"
"foo-bar"

Specs

snake_case(String.t()) :: String.t()

Converts a string into snake_case.

Examples

iex> Swiss.String.snake_case "Foo Bar"
"foo_bar"

iex> Swiss.String.snake_case "--foo-bar--"
"foo_bar"

iex> Swiss.String.snake_case "__FOO_BAR__"
"foo_bar"

iex> Swiss.String.snake_case "FooBar"
"foo_bar"
Link to this function

start_case(string, opts \\ [])

View Source

Specs

start_case(String.t(), keyword()) :: String.t()

Converts a string to Capital Case.

Options

  • :deburr: whether to deburr (remove accents, etc.) the given string. true by default, for consistency with the other functions in this module.

Examples

iex> Swiss.String.start_case "Foo Bar"
"Foo Bar"

iex> Swiss.String.start_case "--foo-bar--"
"Foo Bar"

iex> Swiss.String.start_case "__FOO_BAR__"
"Foo Bar"

iex> Swiss.String.start_case "FooBar"
"Foo Bar"

iex> Swiss.String.start_case "hola señor"
"Hola Senor"

iex> Swiss.String.start_case "hola señor", deburr: false
"Hola Señor"
Link to this function

words(string, pattern \\ %{__struct__: Regex, opts: "", re_pattern: {:re_pattern, 0, 0, 0, <<69, 82, 67, 80, 105, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 131, 0, 37, 111, 0, 0, 0, 0, 0, 0, 255, 3, 254, 255, 255, 7, 254, 255, 255, 7, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 107, 120, 0, 37, 0>>}, re_version: {"8.44 2020-02-12", :little}, source: "[^\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\x7f]+"})

View Source

Specs

words(String.t(), Regex.t()) :: [String.t()]

Decomposes a string into an array of its words.

Examples

iex> Swiss.String.words "FredBarney"
["Fred", "Barney"]

iex> Swiss.String.words "fred, barney, & pebbles"
["fred", "barney", "pebbles"]

iex> Swiss.String.words "fred, barney, & pebbles", ~r/[^, ]+/
["fred", "barney", "&", "pebbles"]