WuunderUtils.Strings (Wuunder Utils v0.2.5)
Contains a set of String helpers
Summary
Functions
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.
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
The inverse of is_nil_or_empty
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 ...
Tests is given string is an UUID
Functions
as_string(value)
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.as_string("this is a string")
"this is a string"
iex> WuunderUtils.Strings.as_string(%{a: 10})
"%{a: 10}"
capitalize(string)
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?"
clean(value)
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"
empty?(string)
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
empty_to_nil(value)
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"
present?(string)
The inverse of is_nil_or_empty
Examples
iex> WuunderUtils.Strings.present?(" ")
false
iex> WuunderUtils.Strings.present?(nil)
false
iex> WuunderUtils.Strings.present?("yes")
true
truncate(value, max_length, suffix \\ "...")
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 "
uuid?(uuid)
Tests is given string is an UUID
Examples
iex> WuunderUtils.Strings.uuid?("")
false
iex> WuunderUtils.Strings.uuid?("39169cb3-03ea")
false
iex> WuunderUtils.Strings.uuid?("39169CB3-03EA-47E5-9B3C-BD4C53E7FE3F")
true
iex> WuunderUtils.Strings.uuid?("39169cb3-03ea-47e5-9b3c-bd4c53e7fe3f")
true