View Source WuunderUtils.Strings (Wuunder Utils v0.9.0)

Contains a set of String helpers

Summary

Functions

The inverse of is_nil_or_empty

Capitalizes each word in a given string. Note that it will downcase the remainders of each word by default.

Trims and cleans up double spaces. Converts nil to empty string.

Checks if given value is nil, or a string that (after trimming) is empty

Converts an empty string to nil or returns the original string

Converts any given value to a string by using inspect. If the given value is already a string, the value is left as it is. Every value is truncated to max 255 chars.

Truncates a string with a given string. If string is longer than given length, it will add a suffix to that string. By default this is ...

Functions

@spec any?(String.t() | nil) :: boolean()

The inverse of is_nil_or_empty

Examples

iex> WuunderUtils.Strings.any?("   ")
false

iex> WuunderUtils.Strings.any?(nil)
false

iex> WuunderUtils.Strings.any?("yes")
true
@spec capitalize(String.t()) :: String.t()

Capitalizes each word in a given string. Note that it will downcase the remainders of each word by default.

Examples

iex> WuunderUtils.Strings.capitalize("this is sparta!")
"This Is Sparta!"

iex> WuunderUtils.Strings.capitalize("is this spArTA?")
"Is This Sparta?"

iex> WuunderUtils.Strings.capitalize("Michał")
"Michał"

iex> WuunderUtils.Strings.capitalize("łukasz")
"Łukasz"
@spec clean(String.t() | nil) :: String.t()

Trims and cleans up double spaces. Converts nil to empty string.

Examples

iex> WuunderUtils.Strings.clean(" well    this is a sentence")
"well this is a sentence"
@spec empty?(String.t() | nil) :: boolean()

Checks if given value is nil, or a string that (after trimming) is empty

Examples

iex> WuunderUtils.Strings.empty?(nil)
true

iex> WuunderUtils.Strings.empty?("   ")
true

iex> WuunderUtils.Strings.empty?("a string")
false
@spec empty_to_nil(String.t() | nil) :: nil | String.t()

Converts an empty string to nil or returns the original string

Examples

iex> WuunderUtils.Strings.empty_to_nil(nil)
nil

iex> WuunderUtils.Strings.empty_to_nil("")
nil

iex> WuunderUtils.Strings.empty_to_nil("    ")
nil

iex> WuunderUtils.Strings.empty_to_nil("this_is_a_string")
"this_is_a_string"
@spec inspect_value(any()) :: String.t()

Converts any given value to a string by using inspect. If the given value is already a string, the value is left as it is. Every value is truncated to max 255 chars.

Examples

iex> WuunderUtils.Strings.inspect_value("this is a string")
"this is a string"

iex> "This is " <> WuunderUtils.Strings.inspect_value("a string")
"This is a string"

iex> WuunderUtils.Strings.inspect_value(%{a: 10})
"%{a: 10}"
Link to this macro

is_empty(string)

View Source (macro)
Link to this function

truncate(value, max_length, suffix \\ "...")

View Source
@spec truncate(String.t(), integer(), String.t()) :: String.t()

Truncates a string with a given string. If string is longer than given length, it will add a suffix to that string. By default this is ...

Examples

iex> WuunderUtils.Strings.truncate("this is a long string", 30)
"this is a long string"

iex> WuunderUtils.Strings.truncate("this is a long string", 21)
"this is a long string"

iex> WuunderUtils.Strings.truncate("this is a long string", 20)
"this is a long st..."

iex> WuunderUtils.Strings.truncate("this is a long string", 10)
"this is..."

iex> WuunderUtils.Strings.truncate("this is a long string", 20, "... data truncated")
"th... data truncated"

iex> WuunderUtils.Strings.truncate("this is a long string", 21, "... data truncated")
"this is a long string"

iex> WuunderUtils.Strings.truncate("this is a long string", 10, "very long suffix")
"very long "