Module hb_util.erl¶
A collection of utility functions for building with HyperBEAM.
Function Index¶
add_commas/1* | |
addresses_to_binary/1* | Serialize the given list of addresses to a binary, using the structured fields format. |
all_hb_modules/0 | Get all loaded modules that are loaded and are part of HyperBEAM. |
atom/1 | Coerce a string to an atom. |
bin/1 | Coerce a value to a binary. |
binary_to_addresses/1 | Parse a list from a binary. |
count/2 | |
debug_fmt/1 | Convert a term to a string for debugging print purposes. |
debug_fmt/2 | |
debug_print/4 | Print a message to the standard error stream, prefixed by the amount of time that has elapsed since the last call to this function. |
decode/1 | Try to decode a URL safe base64 into a binary or throw an error when invalid. |
deep_merge/3 | Deep merge two maps, recursively merging nested maps. |
do_debug_fmt/2* | |
do_to_lines/1* | |
encode/1 | Encode a binary to URL safe base64 binary string. |
eunit_print/2 | Format and print an indented string to standard error. |
find_value/2 | Find the value associated with a key in parsed a JSON structure list. |
find_value/3 | |
find_value/4* | |
float/1 | Coerce a string to a float. |
format_address/2* | If the user attempts to print a wallet, format it as an address. |
format_binary/1 | Format a binary as a short string suitable for printing. |
format_debug_trace/3* | Generate the appropriate level of trace for a given call. |
format_indented/2 | Format a string with an indentation level. |
format_indented/3 | |
format_maybe_multiline/2 | Format a map as either a single line or a multi-line string depending
on the value of the debug_print_map_line_threshold runtime option. |
format_trace/1 | Format a stack trace as a list of strings, one for each stack frame. |
format_trace/2* | |
format_trace_short/1 | Format a trace to a short string. |
format_trace_short/4* | |
format_tuple/2* | Helper function to format tuples with arity greater than 2. |
get_trace/0* | Get the trace of the current process. |
hd/1 | Get the first element (the lowest integer key >= 1) of a numbered map. |
hd/2 | |
hd/3 | |
hd/5* | |
human_id/1 | Convert a native binary ID to a human readable ID. |
human_int/1 | Add , characters to a number every 3 digits to make it human readable. |
id/1 | Return the human-readable form of an ID of a message when given either
a message explicitly, raw encoded ID, or an Erlang Arweave tx record. |
id/2 | |
int/1 | Coerce a string to an integer. |
is_hb_module/1 | Is the given module part of HyperBEAM?. |
is_hb_module/2 | |
is_human_binary/1* | Determine whether a binary is human-readable. |
is_ordered_list/2 | Determine if the message given is an ordered list, starting from 1. |
is_ordered_list/3* | |
is_string_list/1 | Is the given term a string list?. |
key_to_atom/2 | Convert keys in a map to atoms, lowering - to _ . |
list/1 | Coerce a value to a list. |
list_intersection/2* | Returns the intersection of two lists, with stable ordering. |
list_replace/3 | Replace a key in a list with a new value. |
list_to_numbered_message/1 | Convert a list of elements to a map with numbered keys. |
maybe_throw/2 | Throw an exception if the Opts map has an error_strategy key with the
value throw . |
mean/1 | |
message_to_ordered_list/1 | Take a message with numbered keys and convert it to a list of tuples with the associated key as an integer. |
message_to_ordered_list/2 | |
message_to_ordered_list/4* | |
native_id/1 | Convert a human readable ID to a native binary ID. |
normalize_trace/1* | Remove all calls from this module from the top of a trace. |
number/1 | Label a list of elements with a number. |
ok/1 | Unwrap a tuple of the form {ok, Value} , or throw/return, depending on
the value of the error_strategy option. |
ok/2 | |
pick_weighted/2* | Pick a random element from a list, weighted by the values in the list. |
print_trace/3* | |
print_trace/4 | Print the trace of the current stack, up to the first non-hyperbeam module. |
print_trace_short/4 | Print a trace to the standard error stream. |
remove_common/2 | Remove the common prefix from two strings, returning the remainder of the first string. |
remove_trailing_noise/1* | |
remove_trailing_noise/2 | |
safe_decode/1 | Safely decode a URL safe base64 into a binary returning an ok or error tuple. |
safe_encode/1 | Safely encode a binary to URL safe base64. |
server_id/0* | Retreive the server ID of the calling process, if known. |
server_id/1* | |
short_id/1 | Return a short ID for the different types of IDs used in AO-Core. |
shuffle/1* | Shuffle a list. |
stddev/1 | |
to_hex/1 | Convert a binary to a hex string. |
to_lines/1* | |
to_lower/1 | Convert a binary to a lowercase. |
to_sorted_keys/1 | Given a map or KVList, return a deterministically ordered list of its keys. |
to_sorted_keys/2 | |
to_sorted_list/1 | Given a map or KVList, return a deterministically sorted list of its key-value pairs. |
to_sorted_list/2 | |
trace_macro_helper/5 | Utility function to help macro ?trace/0 remove the first frame of the
stack trace. |
unique/1 | Take a list and return a list of unique elements. |
until/1 | Utility function to wait for a condition to be true. |
until/2 | |
until/3 | |
variance/1 | |
weighted_random/1 | Return a random element from a list, weighted by the values in the list. |
Function Details¶
add_commas/1 *¶
add_commas(Rest) -> any()
addresses_to_binary/1 *¶
addresses_to_binary(List) -> any()
Serialize the given list of addresses to a binary, using the structured fields format.
all_hb_modules/0¶
all_hb_modules() -> any()
Get all loaded modules that are loaded and are part of HyperBEAM.
atom/1¶
atom(Str) -> any()
Coerce a string to an atom.
bin/1¶
bin(Value) -> any()
Coerce a value to a binary.
binary_to_addresses/1¶
binary_to_addresses(List) -> any()
Parse a list from a binary. First attempts to parse the binary as a structured-fields list, and if that fails, it attempts to parse the list as a comma-separated value, stripping quotes and whitespace.
count/2¶
count(Item, List) -> any()
debug_fmt/1¶
debug_fmt(X) -> any()
Convert a term to a string for debugging print purposes.
debug_fmt/2¶
debug_fmt(X, Indent) -> any()
debug_print/4¶
debug_print(X, Mod, Func, LineNum) -> any()
Print a message to the standard error stream, prefixed by the amount of time that has elapsed since the last call to this function.
decode/1¶
decode(Input) -> any()
Try to decode a URL safe base64 into a binary or throw an error when invalid.
deep_merge/3¶
deep_merge(Map1, Map2, Opts) -> any()
Deep merge two maps, recursively merging nested maps.
do_debug_fmt/2 *¶
do_debug_fmt(Wallet, Indent) -> any()
do_to_lines/1 *¶
do_to_lines(In) -> any()
encode/1¶
encode(Bin) -> any()
Encode a binary to URL safe base64 binary string.
eunit_print/2¶
eunit_print(FmtStr, FmtArgs) -> any()
Format and print an indented string to standard error.
find_value/2¶
find_value(Key, List) -> any()
Find the value associated with a key in parsed a JSON structure list.
find_value/3¶
find_value(Key, Map, Default) -> any()
find_value/4 *¶
find_value(Key, Map, Default, Opts) -> any()
float/1¶
float(Str) -> any()
Coerce a string to a float.
format_address/2 *¶
format_address(Wallet, Indent) -> any()
If the user attempts to print a wallet, format it as an address.
format_binary/1¶
format_binary(Bin) -> any()
Format a binary as a short string suitable for printing.
format_debug_trace/3 *¶
format_debug_trace(Mod, Func, Line) -> any()
Generate the appropriate level of trace for a given call.
format_indented/2¶
format_indented(Str, Indent) -> any()
Format a string with an indentation level.
format_indented/3¶
format_indented(RawStr, Fmt, Ind) -> any()
format_maybe_multiline/2¶
format_maybe_multiline(X, Indent) -> any()
Format a map as either a single line or a multi-line string depending
on the value of the debug_print_map_line_threshold
runtime option.
format_trace/1¶
format_trace(Stack) -> any()
Format a stack trace as a list of strings, one for each stack frame.
Each stack frame is formatted if it matches the stack_print_prefixes
option. At the first frame that does not match a prefix in the
stack_print_prefixes
option, the rest of the stack is not formatted.
format_trace/2 *¶
format_trace(Rest, Prefixes) -> any()
format_trace_short/1¶
format_trace_short(Trace) -> any()
Format a trace to a short string.
format_trace_short/4 *¶
format_trace_short(Max, Latch, Trace, Prefixes) -> any()
format_tuple/2 *¶
format_tuple(Tuple, Indent) -> any()
Helper function to format tuples with arity greater than 2.
get_trace/0 *¶
get_trace() -> any()
Get the trace of the current process.
hd/1¶
hd(Message) -> any()
Get the first element (the lowest integer key >= 1) of a numbered map. Optionally, it takes a specifier of whether to return the key or the value, as well as a standard map of HyperBEAM runtime options.
hd/2¶
hd(Message, ReturnType) -> any()
hd/3¶
hd(Message, ReturnType, Opts) -> any()
hd/5 *¶
hd(Map, Rest, Index, ReturnType, Opts) -> any()
human_id/1¶
human_id(Bin) -> any()
Convert a native binary ID to a human readable ID. If the ID is already a human readable ID, it is returned as is. If it is an ethereum address, it is returned as is.
human_int/1¶
human_int(Float) -> any()
Add ,
characters to a number every 3 digits to make it human readable.
id/1¶
id(Item) -> any()
Return the human-readable form of an ID of a message when given either
a message explicitly, raw encoded ID, or an Erlang Arweave tx
record.
id/2¶
id(TX, Type) -> any()
int/1¶
int(Str) -> any()
Coerce a string to an integer.
is_hb_module/1¶
is_hb_module(Atom) -> any()
Is the given module part of HyperBEAM?
is_hb_module/2¶
is_hb_module(Atom, Prefixes) -> any()
is_human_binary/1 *¶
is_human_binary(Bin) -> any()
Determine whether a binary is human-readable.
is_ordered_list/2¶
is_ordered_list(Msg, Opts) -> any()
Determine if the message given is an ordered list, starting from 1.
is_ordered_list/3 *¶
is_ordered_list(N, Msg, Opts) -> any()
is_string_list/1¶
is_string_list(MaybeString) -> any()
Is the given term a string list?
key_to_atom/2¶
key_to_atom(Key, Mode) -> any()
Convert keys in a map to atoms, lowering -
to _
.
list/1¶
list(Value) -> any()
Coerce a value to a list.
list_intersection/2 *¶
list_intersection(List1, List2) -> any()
Returns the intersection of two lists, with stable ordering.
list_replace/3¶
list_replace(List, Key, Value) -> any()
Replace a key in a list with a new value.
list_to_numbered_message/1¶
list_to_numbered_message(Msg) -> any()
Convert a list of elements to a map with numbered keys.
maybe_throw/2¶
maybe_throw(Val, Opts) -> any()
Throw an exception if the Opts map has an error_strategy
key with the
value throw
. Otherwise, return the value.
mean/1¶
mean(List) -> any()
message_to_ordered_list/1¶
message_to_ordered_list(Message) -> any()
Take a message with numbered keys and convert it to a list of tuples with the associated key as an integer. Optionally, it takes a standard message of HyperBEAM runtime options.
message_to_ordered_list/2¶
message_to_ordered_list(Message, Opts) -> any()
message_to_ordered_list/4 *¶
message_to_ordered_list(Message, Keys, Key, Opts) -> any()
native_id/1¶
native_id(Bin) -> any()
Convert a human readable ID to a native binary ID. If the ID is already a native binary ID, it is returned as is.
normalize_trace/1 *¶
normalize_trace(Rest) -> any()
Remove all calls from this module from the top of a trace.
number/1¶
number(List) -> any()
Label a list of elements with a number.
ok/1¶
ok(Value) -> any()
Unwrap a tuple of the form {ok, Value}
, or throw/return, depending on
the value of the error_strategy
option.
ok/2¶
ok(Other, Opts) -> any()
pick_weighted/2 *¶
pick_weighted(Rest, Remaining) -> any()
Pick a random element from a list, weighted by the values in the list.
print_trace/3 *¶
print_trace(Stack, Label, CallerInfo) -> any()
print_trace/4¶
print_trace(Stack, CallMod, CallFunc, CallLine) -> any()
Print the trace of the current stack, up to the first non-hyperbeam
module. Prints each stack frame on a new line, until it finds a frame that
does not start with a prefix in the stack_print_prefixes
hb_opts.
Optionally, you may call this function with a custom label and caller info,
which will be used instead of the default.
print_trace_short/4¶
print_trace_short(Trace, Mod, Func, Line) -> any()
Print a trace to the standard error stream.
remove_common/2¶
remove_common(MainStr, SubStr) -> any()
Remove the common prefix from two strings, returning the remainder of the first string. This function also coerces lists to binaries where appropriate, returning the type of the first argument.
remove_trailing_noise/1 *¶
remove_trailing_noise(Str) -> any()
remove_trailing_noise/2¶
remove_trailing_noise(Str, Noise) -> any()
safe_decode/1¶
safe_decode(E) -> any()
Safely decode a URL safe base64 into a binary returning an ok or error tuple.
safe_encode/1¶
safe_encode(Bin) -> any()
Safely encode a binary to URL safe base64.
server_id/0 *¶
server_id() -> any()
Retreive the server ID of the calling process, if known.
server_id/1 *¶
server_id(Opts) -> any()
short_id/1¶
short_id(Bin) -> any()
Return a short ID for the different types of IDs used in AO-Core.
shuffle/1 *¶
shuffle(List) -> any()
Shuffle a list.
stddev/1¶
stddev(List) -> any()
to_hex/1¶
to_hex(Bin) -> any()
Convert a binary to a hex string. Do not use this for anything other than generating a lower-case, non-special character id. It should not become part of the core protocol. We use b64u for efficient encoding.
to_lines/1 *¶
to_lines(Elems) -> any()
to_lower/1¶
to_lower(Str) -> any()
Convert a binary to a lowercase.
to_sorted_keys/1¶
to_sorted_keys(Msg) -> any()
Given a map or KVList, return a deterministically ordered list of its keys.
to_sorted_keys/2¶
to_sorted_keys(Msg, Opts) -> any()
to_sorted_list/1¶
to_sorted_list(Msg) -> any()
Given a map or KVList, return a deterministically sorted list of its key-value pairs.
to_sorted_list/2¶
to_sorted_list(Msg, Opts) -> any()
trace_macro_helper/5¶
trace_macro_helper(Fun, X2, Mod, Func, Line) -> any()
Utility function to help macro ?trace/0
remove the first frame of the
stack trace.
unique/1¶
unique(List) -> any()
Take a list and return a list of unique elements. The function is order-preserving.
until/1¶
until(Condition) -> any()
Utility function to wait for a condition to be true. Optionally, you can pass a function that will be called with the current count of iterations, returning an integer that will be added to the count. Once the condition is true, the function will return the count.
until/2¶
until(Condition, Count) -> any()
until/3¶
until(Condition, Fun, Count) -> any()
variance/1¶
variance(List) -> any()
weighted_random/1¶
weighted_random(List) -> any()
Return a random element from a list, weighted by the values in the list.