Packages
moar
1.11.0
5.0.0
4.3.0
4.2.0
4.1.0
4.0.0
3.2.0
3.1.0
3.0.0
2.7.0
2.6.0
2.5.0
2.4.2
2.4.1
2.4.0
2.3.0
2.2.0
2.1.0
2.0.1
2.0.0
1.64.0
1.63.1
1.63.0
1.62.0
1.61.2
1.61.1
1.61.0
1.60.0
1.59.2
1.59.1
1.59.0
1.58.0
1.57.0
1.56.2
1.56.1
1.56.0
1.55.0
1.54.0
1.53.0
1.52.1
1.52.0
1.51.0
1.50.0
1.49.0
1.48.0
1.47.0
1.46.0
1.45.1
1.45.0
1.44.0
1.43.0
1.42.0
1.41.0
1.40.0
1.39.0
1.38.0
1.37.0
1.36.0
1.35.0
1.34.0
1.33.0
1.32.0
1.31.0
1.30.0
1.29.0
1.28.0
1.27.0
1.26.0
1.25.0
1.24.1
1.24.0
1.23.0
1.22.0
1.21.0
1.20.0
1.19.3
1.19.2
1.19.1
1.19.0
1.18.1
1.18.0
1.17.0
1.16.0
1.15.0
1.14.0
1.13.1
1.13.0
1.12.0
1.11.0
1.10.0
1.9.0
1.8.0
1.7.0
1.6.0
1.5.0
1.4.0
1.3.0
1.2.0
1.1.0
1.0.0
0.1.0
A dependency-free utility library containing 100+ useful functions.
Current section
Files
Jump to
Current section
Files
README.md
# Moar
[](https://github.com/synchronal/moar/actions)
[](https://hex.pm/packages/moar)
[](https://github.com/synchronal/moar/blob/main/LICENSE.md)
An assortment of useful functions.
The docs can be found at <https://hexdocs.pm/moar>
## Installation
The package can be installed by adding `moar` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:moar, "~> 1.11"}
]
end
```
## Similar libraries
* [Swiss](https://hexdocs.pm/swiss/readme.html)
## A quick tour
`Moar.Assertions`
* `assert_eq` is a pipeable equality assertion, with options such as the ability to ignore order when comparing
lists, returning a different value than what was passed in, and asserting that a value is within some delta
(which can be a number or time duration).
* `assert_recent` asserts that a datetime is pretty close to now.
* `assert_that` asserts that a pre-condition and post-condition are true after performing an action.
`Moar.Atom`
* `from_string` and `to_string` convert between strings and atoms, and don't fail if you try to convert an
atom to an atom or a string to a string.
`Moar.DateTime` and `Moar.NaiveDateTime`
* `add` can add a `Moar.Duration`, which is a tuple with a time unit, like `{27, :minute}`.
* `from_iso8601!` raises if the string is not in ISO 8601 format.
* `to_iso8601_rounded` converts to an ISO 8601 string, truncated to the second.
`Moar.Difference`
* a protocol that defines `diff(a, b)` along with implementations for datetimes.
`Moar.Duration`
* is a `{time, unit}` tuple (like `{27, :minute}`) and supports the regular `t:System.time_unit/0` values and also
`:minute`, `:hour`, and `:day`.
* `convert` converts between various time units.
* `to_string` renders a duration into a string like `"27 minutes"`.
`Moar.Enum`
* `at` is like `Enum.at` but raises if the index is out of bounds.
* `compact` rejects nil values.
* `first!` returns the first item or raises if there isn't a first item.
* `isort` and `isort_by` sort case-insensitively.
* `tids` extracts `tid` fields. (`tid` is short for "test id" and the authors of Moar use tids extensively for testing.)
`Moar.File`
* `new_tempfile_path` returns a new path for a tempfile, without creating it.
* `write_tempfile` writes some data to a new tempfile.
`Moar.Map`
* `atomize_key`, `atomize_keys`, and `deep_atomize_keys` convert keys in a map from strings to atoms, and
`stringify_keys` does the opposite.
* `merge` and `deep_merge` merge maps, and can also convert enumerables into maps before merging.
* `rename_key` and `rename_keys` rename keys in a map.
* `transform` transforms a key or multiple keys with a transformer function.
`Moar.Opts`
* is meant to be used with function options.
* `get` extracts a value from opts, falling back to a default if the value is blank (via `Moar.Term.blank?/1`)
* `take` extracts multiple values from opts, falling back to defaults if the value is blank (via `Moar.Term.blank?/1`)
`Moar.Protocol`
* `implements!` raises if a struct does not implement the given protocol.
* `implements?` returns true if a struct implements the given protocol.
`Moar.Random`
* `integer` returns a random integer.
* `string` returns random base64- or base32-encoded string.
`Moar.Retry`
* `rescue_until` and `rescue_for` run the given function repeatedly until it does not raise.
* `retry_until` and `retry_for` run the given function repeatedly until it returns a truthy value.
`Moar.String`
* `inner_truncate` removes the middle of a string to make it the desired length.
* `secure_compare` compares two strings in constant time.
* `slug` converts a string into a slug with a custom joiner character; `dasherize` and `underscore` are shortcuts for
common slug formats.
* `squish` collapses consecutive whitespace characters.
* `surround` wraps a string with the given characters.
* `to_integer` converts a string to an integer with a few handy options.
* `trim` is like `String.trim/1` but handles `nil` values.
* `truncate_at` truncates a string at the last instance of a substring that results in the truncated string
being shorter than a given length.
`Moar.Sugar`
* `error`, `noreply`, and `ok` create tuples (`"foo" |> error()` -> `{:error, "foo"}`).
* `error!` and `ok!` unwrap tuples (`{:error, "foo"} |> error!()` -> `"foo"`).
* `returning` takes two arguments and returns the second one.
`Moar.Term`
* `blank?` returns true if a term is `nil`, an empty string, a whitespace-only string, an empty list, or an empty map.
* `present?` is the opposite of `blank?`.
* `presence` returns a default if the argument is blank.
`Moar.Tuple`
* `from_list!` converts a list of tuples like `[{:ok, 1}, {:ok, 2}, {:ok, 3}]` to a tuple like `{:ok, [1, 2, 3]}`