View Source Moar.String (Moar v1.7.0)

String-related functions.

Link to this section Summary

Functions

Convert strings and atoms to dash-case (kebab-case) and trims leading and trailing non-alphanumeric characters.

Truncate s to max_length by replacing the middle of the string with replacement, which defaults to the single unicode character .

Compares the two binaries in constant-time to avoid timing attacks. See: http://codahale.com/a-lesson-in-timing-attacks/.

Trims a string and replaces consecutive whitespace characters with a single space.

Adds surrounder to the beginning and end of s.

Adds prefix to the beginning of s and suffix to the end.

Converts a string to an integer. Returns nil if the argument is nil or empty string.

Like to_integer/1 but with options

Like String.trim/1 but returns nil if the argument is nil.

Truncates s at the last instance of at, causing the string to be at most limit characters.

Link to this section Functions

@spec dasherize(atom() | binary()) :: binary()

Convert strings and atoms to dash-case (kebab-case) and trims leading and trailing non-alphanumeric characters.

iex> ["foo", "FOO", :foo] |> Enum.map(&Moar.String.dasherize/1)
["foo", "foo", "foo"]

iex> ["foo-bar", "foo_bar", :foo_bar, " fooBar ", "  ?foo ! bar  "] |> Enum.map(&Moar.String.dasherize/1)
["foo-bar", "foo-bar", "foo-bar", "foo-bar", "foo-bar"]
Link to this function

inner_truncate(s, max_length, replacement \\ "…")

View Source
@spec inner_truncate(binary(), integer(), binary()) :: binary()

Truncate s to max_length by replacing the middle of the string with replacement, which defaults to the single unicode character .

Note that the final length of the string will be max_length plus the length of replacement.

iex> Moar.String.inner_truncate("abcdefghijklmnopqrstuvwxyz", 10)
"abcde…vwxyz"

iex> Moar.String.inner_truncate("abcdefghijklmnopqrstuvwxyz", 10, "<==>")
"abcde<==>vwxyz"
Link to this function

secure_compare(left, right)

View Source
@spec secure_compare(binary(), binary()) :: boolean()

Compares the two binaries in constant-time to avoid timing attacks. See: http://codahale.com/a-lesson-in-timing-attacks/.

iex> Moar.String.secure_compare("foo", "bar")
false
@spec squish(binary()) :: binary()

Trims a string and replaces consecutive whitespace characters with a single space.

iex> Moar.String.squish("  foo   bar  	baz ")
"foo bar baz"
@spec surround(binary(), binary()) :: binary()

Adds surrounder to the beginning and end of s.

iex> Moar.String.surround("Hello", "**")
"**Hello**"
Link to this function

surround(s, prefix, suffix)

View Source
@spec surround(binary(), binary(), binary()) :: binary()

Adds prefix to the beginning of s and suffix to the end.

iex> Moar.String.surround("Hello", "“", "”")
"“Hello”"
@spec to_integer(nil | binary()) :: integer()

Converts a string to an integer. Returns nil if the argument is nil or empty string.

iex> Moar.String.to_integer("12,345")
12_345
@spec to_integer(binary(), :lenient | [{:default, binary()}]) :: integer()

Like to_integer/1 but with options:

  • :lenient option removes non-digit characters first
  • default: option specifies a default in case s is nil
iex> Moar.String.to_integer("USD$25", :lenient)
25

iex> Moar.String.to_integer(nil, default: 0)
0
@spec trim(nil | binary()) :: binary()

Like String.trim/1 but returns nil if the argument is nil.

Link to this function

truncate_at(s, at, limit)

View Source

Truncates s at the last instance of at, causing the string to be at most limit characters.

iex> Moar.String.truncate_at("I like apples. I like bananas. I like cherries.", ".", 35)
"I like apples. I like bananas."