View Source Moar.Term (Moar v1.62.0)

Blank/present functions for terms.

A term is considered present when it is not blank.

A term is considered blank when:

  • it is nil
  • it is false
  • it is a string, and its length after being trimmed is 0
  • it is an empty list
  • it is an empty map

Summary

Functions

Returns true if the term is blank, nil, or empty.

Returns the value if it is present (via present?), or else returns the default value.

Returns true if the term is not blank, nil, or empty.

Returns present_value when term is present (via present?), and blank_value when term is blank.

Functions

@spec blank?(any()) :: boolean()

Returns true if the term is blank, nil, or empty.

iex> Moar.Term.blank?(nil)
true

iex> Moar.Term.blank?("   ")
true

iex> Moar.Term.blank?([])
true

iex> Moar.Term.blank?(%{})
true
Link to this function

presence(term, default \\ nil)

View Source
@spec presence(any(), any()) :: any()

Returns the value if it is present (via present?), or else returns the default value.

iex> Moar.Term.presence(20, 100)
20

iex> Moar.Term.presence(nil, 100)
100
@spec present?(any()) :: boolean()

Returns true if the term is not blank, nil, or empty.

iex> Moar.Term.present?(1)
true

iex> Moar.Term.present?([1])
true

iex> Moar.Term.present?(%{a: 1})
true

iex> Moar.Term.present?("1")
true
Link to this function

when_present(term, present_value, blank_value)

View Source
@spec when_present(any(), any(), any()) :: any()

Returns present_value when term is present (via present?), and blank_value when term is blank.

iex> Moar.Term.when_present(20, "continue", "value missing")
"continue"

iex> Moar.Term.when_present(nil, "continue", "value missing")
"value missing"