MavuUtils (MavuUtils v1.0.7) View Source

An opinionated set of utility functions used by other (upcoming) packages under mavuio/*

Link to this section Summary

Functions

applies a function to a pipe based on a condition

applies a function to a pipe based on a condition tested with present?/1

applies a function to a pipe based on a condition tested with true?/1

tests if value is "blank"

tests if value is one of several falsy values.

applies a value when piped value is_empty ( as defined by present?/1 )

applies a value when piped value is nil

simple pipeable logger.

similar to List.wrap, but for maps

macro to only apply a function within a pipe if a condition equals to true.

tests if value is "non-blank"

similar to List.wrap, but for strings

tries to convert any value to integer

tests if value is one of several truthy values and not false?/1

updates query (given as string) with new parameters (given as keyword-list) parameters which values are nil will be removed from the query

updates url (given as string) with new parameters (given as keyword-list) parameters which values are nil will be removed from the query

Link to this section Functions

applies a function to a pipe based on a condition

##Example:

get_name() |> do_if(opts[:uppercase], &String.upcase/1) |> Jason.encode!()

Link to this function

do_if_present(value, condition, fun)

View Source

applies a function to a pipe based on a condition tested with present?/1

##Example:

get_record() |> if_true(opts[:key], &Map.get(&1, opts[:key])) |> Jason.encode!()

Link to this function

do_if_true(value, condition, fun)

View Source

applies a function to a pipe based on a condition tested with true?/1

##Example:

get_record() |> if_true(opts[:uppercase], &String.upcase/1) |> Jason.encode!()

tests if value is "blank"

tests if value is one of several falsy values.

Link to this function

if_empty(val, default_val)

View Source

applies a value when piped value is_empty ( as defined by present?/1 )

##Example:

taxrate_item =

taxrates[taxkey]
|> if_empty(%{tax: "0", net: "0", gross: "0"})
|> taxrate_item_append(create_taxrate_stats_entry_for_item(item))
Link to this function

if_nil(val, default_val)

View Source

applies a value when piped value is nil

##Example:

get_max_order_id() |> if_nil(1000)

Link to this function

log(data, msg \\ "", level)

View Source

simple pipeable logger.

setting a loglevel is mandatory

order |> MavuUtils.log("trying to finish order #{order.id}", :info) |> finish_order()

similar to List.wrap, but for maps

Link to this function

on_error_nil(val, opts \\ [])

View Source
Link to this function

on_error_raise(val, opts \\ [])

View Source
Link to this macro

pipe_when(left, condition, fun)

View Source (macro)

macro to only apply a function within a pipe if a condition equals to true.

the conditional gets passed to true?/1 first

##Example:

|> pipe_when(
    item[:taxrate],
    update_in([:taxrates], &update_taxrate_stats(&1, item))
  )

tests if value is "non-blank"

similar to List.wrap, but for strings

tries to convert any value to integer

returns integer on success, nil on failure

tests if value is one of several truthy values and not false?/1

Link to this function

update_params_in_path(url, params)

View Source
Link to this function

update_params_in_query(query, params)

View Source

updates query (given as string) with new parameters (given as keyword-list) parameters which values are nil will be removed from the query

Link to this function

update_params_in_url(url, params)

View Source

updates url (given as string) with new parameters (given as keyword-list) parameters which values are nil will be removed from the query