View Source Moar
An assortment of useful functions.
The docs can be found at https://hexdocs.pm/moar
installation
Installation
The package can be installed by adding moar to your list of dependencies in mix.exs:
def deps do
[
{:moar, "~> 1.13"}
]
end
similar-libraries
Similar libraries
a-quick-tour
A quick tour
assert_eqis 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_recentasserts that a datetime is pretty close to now.assert_thatasserts that a pre-condition and post-condition are true after performing an action.refute_thatasserts that a condition didn't change after performing an action.
from_stringandto_stringconvert 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
addcan add aMoar.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_roundedconverts to an ISO 8601 string, truncated to the second.
- a protocol that defines
diff(a, b)along with implementations for datetimes.
- is a
{time, unit}tuple (like{27, :minute}) and supports the regularSystem.time_unit/0values and also:minute,:hour, and:day. agoreturns the duration between a given datetime and now.approxshifts the duration to a simple approximate value.betweenreturns the duration between two datetimes.convertconverts a duration to a new time unit, returning only the value.formatformats a duration in long ("3 seconds") or short ("3s") format with optional transformers and suffix.humanizeconverts the duration to the highest possible time unit.shiftconverts a duration to a new time unit.to_stringrenders a duration into a string like"27 minutes".
atis likeEnum.atbut raises if the index is out of bounds.compactrejects nil values.first!returns the first item or raises if there isn't a first item.isortandisort_bysort case-insensitively.tidsextractstidfields. (tidis short for "test id" and the authors of Moar use tids extensively for testing.)
new_tempfile_pathreturns a new path for a tempfile, without creating it.write_tempfilewrites some data to a new tempfile.
atomize_key,atomize_keys, anddeep_atomize_keysconvert keys in a map from strings to atoms, andstringify_keysdoes the opposite.mergeanddeep_mergemerge maps, and can also convert enumerables into maps before merging.rename_keyandrename_keysrename keys in a map.transformtransforms a key or multiple keys with a transformer function.
- is meant to be used with function options.
getextracts a value from opts, falling back to a default if the value is blank (viaMoar.Term.blank?/1)takeextracts multiple values from opts, falling back to defaults if the value is blank (viaMoar.Term.blank?/1)
implements!raises if a struct does not implement the given protocol.implements?returns true if a struct implements the given protocol.
integerreturns a random integer.stringreturns random base64- or base32-encoded string.
rescue_untilandrescue_forrun the given function repeatedly until it does not raise.retry_untilandretry_forrun the given function repeatedly until it returns a truthy value.
inner_truncateremoves the middle of a string to make it the desired length.secure_comparecompares two strings in constant time.slugconverts a string into a slug with a custom joiner character;dasherizeandunderscoreare shortcuts for common slug formats.squishcollapses consecutive whitespace characters.surroundwraps a string with the given characters.to_caseconverts text to:camel_case,:lower_camel_case, or:snake_case.to_integerconverts a string to an integer with a few handy options.trimis likeString.trim/1but handlesnilvalues.truncate_attruncates a string at the last instance of a substring that results in the truncated string being shorter than a given length.
error,noreply, andokcreate tuples ("foo" |> error()->{:error, "foo"}).error!andok!unwrap tuples ({:error, "foo"} |> error!()->"foo").returningtakes two arguments and returns the second one.
blank?returns true if a term isnil, an empty string, a whitespace-only string, an empty list, or an empty map.present?is the opposite ofblank?.presencereturns a default if the argument is blank.
from_list!converts a list of tuples like[{:ok, 1}, {:ok, 2}, {:ok, 3}]to a tuple like{:ok, [1, 2, 3]}