View Source Moar.String (Moar v0.1.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 removing the middle of the string

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

Replace consecutive whitespace characters with a single space

Add surrounder to the beginning and end of s

Add prefix to the beginning of s and suffix to the end

Convert 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.

The following all get converted to "foo": "foo", "FOO", :foo.

The following all get converted to "foo-bar": "foo-bar", :foo_bar, " fooBar ", " ?foo ! bar "

Link to this function

inner_truncate(s, max_length)

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

Truncate s to max_length by removing the middle of the string

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/

@spec squish(binary()) :: binary()

Replace consecutive whitespace characters with a single space

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

Add surrounder to the beginning and end of s

Link to this function

surround(s, prefix, suffix)

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

Add prefix to the beginning of s and suffix to the end

@spec to_integer(nil | binary()) :: integer()

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

@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
@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."