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

Link to this function

as_string(value)

@spec as_string(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.as_string("this is a string")
"this is a string"

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

capitalize(string)

@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?"
@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
Link to this function

empty_to_nil(value)

@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"
Link to this macro

is_empty(string)

(macro)
Link to this function

present?(string)

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

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
Link to this function

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

@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 "
@spec uuid?(String.t()) :: boolean()

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