Module z_utils

Misc utility functions for zotonic Parts are from wf_utils.erl which is Copyright (c) 2008-2009 Rusty Klophaus.

Copyright © 2009-2023 Marc Worrell

Authors: Marc Worrell.

Description

Misc utility functions for zotonic Parts are from wf_utils.erl which is Copyright (c) 2008-2009 Rusty Klophaus

Function Index

are_equal/2Check if two arguments are equal, optionally converting them.
assert/2Check if an assertion is ok or failed, raise an erlang error exception if the condition failed.
checksum/2(Deprecated.) Calculate a checksum for the given data using the sign_key_simple of the site.
checksum_assert/3(Deprecated.) Assert that the checksum is correct.
coalesce/1COALESCE, select the first value non-null-ish value in a list.
decode_value/2(Deprecated.) Decode a value.
decode_value_expire/2(Deprecated.) Decode a value using a checksum, check date to check for expiration.
depickle/2(Deprecated.) Decode pickled base64url data.
encode_value/2(Deprecated.) Encode value to a binary with a checksum, for use in cookies.
encode_value_expire/3(Deprecated.) Encode a value using a checksum, add a date to check for expiration.
ensure_existing_module/1Ensure that the given string matches an existing module.
erase_process_dict/0Safe erase of process dict, keeps some 'magical' proc_lib vars.
f/1
f/2
filter_dot_files/1Filter all filenames which start with a dot.
flush_message/1Flush all incoming messages, used when receiving timer ticks to prevent multiple ticks.
get_nth/2Get the Nth value of a list, if the list is too short then return 'undefined'.
get_seconds/0Return the current universal time in seconds.
get_value/2Get a value from a map or a proplist.
get_value/3Get a value from a map or a proplist.
group_by/3Group by a property or m_rsc property, keeps the input list in the same order.
group_proplists/2Given a list of proplists, make it a nested list with respect to a property, combining elements with the same property.
hex_decode/1
hex_encode/1
hex_sha/1Hash data and encode into a hex string safe for filenames and texts.
hex_sha2/1Hash256 data and encode into a hex string safe for filenames and texts.
hmac/3(Deprecated.)
index_proplist/2Make a property list based on the value of a property For example: [ [{a,b}], [{a,c}] ] gives [{a, [{a,b}]}, {c, [[{a,c}]]}].
is_empty/1Check if a value is 'empty'.
is_iolist/1
is_process_alive/1Multinode is_process_alive check.
is_proplist/1
is_true/1Check if the parameter could represent the logical value of "true".
join_defined/2
js_array/1
js_escape/1
js_escape/2Javascript escape, see also: http://code.google.com/p/doctype/wiki/ArticleXSSInJavaScript.
js_object/1Create a javascript object from a proplist.
js_object/2
js_object/3
lib_dir/0Return an abspath to a directory relative to the application root.
lib_dir/1
list_dir_recursive/1Return a list of all files in a directory, recursive depth first search for files not starting with a '.'.
name_for_site/2Return the name used in the context of a hostname.
nested_proplist/1Scan the props of a proplist, when the prop is a string with "." characters in it then split the prop.
nested_proplist/2
now/0Return the current tick count.
now_msec/0
only_digits/1
only_letters/1
os_escape/1Simple escape function for command line arguments.
os_filename/1Simple escape function for filenames as commandline arguments.
otp_release/0Return the major OTP version as an integer.
pickle/2(Deprecated.) Encode an arbitrary to a binary.
pipeline/2Apply a list of functions to a startlist of arguments.
prefix/2
prop_delete/2
prop_replace/3Replace a property in a proplist with a new value.
props_merge/2Overlay property list List1 over List2, keys in List1 overrule keys in List2.
randomize/1Simple randomize of a list.
ranges/1Convert a sorted list of integers to a list of range pairs {From,To}.
replace1/3
set_nth/3Update the nth value of a list.
split/2Take max N elements from a list.
split_in/2
vsplit_in/2
wildcard/1filename:wildcard version which filters dotfiles like unix does.
wildcard/2
wildcard_recursive/2
write_terms/2Write a file that is readable by file:consult/1.

Function Details

are_equal/2

are_equal(Arg1, Arg2) -> any()

Check if two arguments are equal, optionally converting them

assert/2

assert(Condition, Error) -> ok

Check if an assertion is ok or failed, raise an erlang error exception if the condition failed.

checksum/2

checksum(Data, Context) -> Checksum

This function is deprecated: Use z_crypto:checksum/2 instead.

Calculate a checksum for the given data using the sign_key_simple of the site.

checksum_assert/3

checksum_assert(Data, Checksum, Context) -> ok | no_return()

This function is deprecated: Use z_crypto:checksum_assert/3 instead.

Assert that the checksum is correct. Throws an exception of class error with reason checksum_invalid if the checksum is not valid. The sign_key_simple if used for the checksum calculation.

coalesce/1

coalesce(List) -> Value

COALESCE, select the first value non-null-ish value in a list. Return 'undefined' if there are no non-null-ish values. A value is is considered null-ish if it is undefined, null or the empty list.

decode_value/2

decode_value(Data, ContextOrSecret) -> any()

This function is deprecated: Use z_crypto:decode_value/2 instead.

Decode a value. Crash if the checksum is invalid.

decode_value_expire/2

decode_value_expire(Encoded, Context) -> {ok, Value} | {error, expired}

This function is deprecated: Use z_crypto:decode_value_expire/3 instead.

Decode a value using a checksum, check date to check for expiration. Crashes if the checksum is invalid.

depickle/2

depickle(Data, Context) -> Term | no_return()

This function is deprecated: Use z_crypto:depickle/2 instead.

Decode pickled base64url data. If the data checksum is invalid then an exception of class error with reason {checksum_invalid, Data} is thrown. The site's sign_key is used as the secret.

encode_value/2

encode_value(Value, ContextOrSecret) -> any()

This function is deprecated: Use z_crypto:encode_value/2 instead.

Encode value to a binary with a checksum, for use in cookies.

encode_value_expire/3

encode_value_expire(Value, Date, Context) -> Encoded

This function is deprecated: Use z_crypto:encode_value_expire/3 instead.

Encode a value using a checksum, add a date to check for expiration.

ensure_existing_module/1

ensure_existing_module(ModuleName) -> any()

Ensure that the given string matches an existing module. Used to prevent a denial of service attack where we exhaust the atom space.

erase_process_dict/0

erase_process_dict() -> any()

Safe erase of process dict, keeps some 'magical' proc_lib vars

f/1

f(S) -> any()

f/2

f(S, Args) -> any()

filter_dot_files/1

filter_dot_files(Names) -> any()

Filter all filenames which start with a dot.

flush_message/1

flush_message(Msg) -> any()

Flush all incoming messages, used when receiving timer ticks to prevent multiple ticks.

get_nth/2

get_nth(N, L) -> Value | undefined

Get the Nth value of a list, if the list is too short then return 'undefined'. The first value is 1.

get_seconds/0

get_seconds() -> any()

Return the current universal time in seconds

get_value/2

get_value(Key::term(), Map::map() | list()) -> term()

Get a value from a map or a proplist. Return 'undefined' if The value was not present.

get_value/3

get_value(Key::term(), Map::map() | list(), Default::term()) -> term()

Get a value from a map or a proplist. Return the default value if The value was not present.

group_by/3

group_by(L, Prop, Context) -> any()

Group by a property or m_rsc property, keeps the input list in the same order.

group_proplists/2

group_proplists(Prop::atom(), Rest::[{atom(), term()}]) -> [{term(), list()}]

Given a list of proplists, make it a nested list with respect to a property, combining elements with the same property. Assumes the list is sorted on the property you are splitting on For example: [[{a,b}{x}], [{a,b}{z}], [{a,c}{y}]] gives: [ {b, [[{a,b}{x}], [{a,b}{z}]]}, {c, [[{a,c}{y}]]} ]

hex_decode/1

hex_decode(Value::iodata()) -> binary()

hex_encode/1

hex_encode(Value::iodata()) -> binary()

hex_sha/1

hex_sha(Value) -> Hash

Hash data and encode into a hex string safe for filenames and texts.

hex_sha2/1

hex_sha2(Value) -> Hash

Hash256 data and encode into a hex string safe for filenames and texts.

hmac/3

hmac(Type, Key, Data) -> any()

This function is deprecated: Use crypto:mac/4 instead.

index_proplist/2

index_proplist(Prop::term(), List::[{term(), term()}]) -> [{term(), term()}]

Make a property list based on the value of a property For example: [ [{a,b}], [{a,c}] ] gives [{a, [{a,b}]}, {c, [[{a,c}]]}]

is_empty/1

is_empty(Value) -> boolean()

Check if a value is 'empty'. Special empty values are empty strings, dates in the year 9999 and trans records with no or only empty values.

is_iolist/1

is_iolist(C) -> any()

is_process_alive/1

is_process_alive(Pid) -> any()

Multinode is_process_alive check

is_proplist/1

is_proplist(R) -> any()

is_true/1

is_true(Value) -> boolean()

Check if the parameter could represent the logical value of "true"

join_defined/2

join_defined(Sep, List) -> any()

js_array/1

js_array(L) -> any()

js_escape/1

js_escape(V) -> any()

js_escape/2

js_escape(Trans, OptContext) -> any()

Javascript escape, see also: http://code.google.com/p/doctype/wiki/ArticleXSSInJavaScript

js_object/1

js_object(L) -> any()

Create a javascript object from a proplist

js_object/2

js_object(L, OptContext) -> any()

js_object/3

js_object(L, T, Context) -> any()

lib_dir/0

lib_dir() -> any()

Return an abspath to a directory relative to the application root.

lib_dir/1

lib_dir(Dir) -> any()

list_dir_recursive/1

list_dir_recursive(Dir) -> any()

Return a list of all files in a directory, recursive depth first search for files not starting with a '.'

name_for_site/2

name_for_site(Name::atom(), Context::atom() | #context{cowreq = cowboy_req:req() | undefined, cowenv = cowboy_middleware:env() | undefined, site = atom(), controller_module = atom() | undefined, client_id = binary() | undefined, client_topic = mqtt_sessions:topic() | undefined, routing_id = binary() | undefined, acl = term() | admin | undefined, acl_is_read_only = boolean(), user_id = integer() | authenticated | undefined, render_state = undefined | z_render:render_state(), db = {atom(), atom()} | undefined, dbc = pid() | undefined, language = [atom()], tz = binary(), props = map(), depcache = pid() | atom(), dispatcher = pid() | atom(), template_server = pid() | atom(), scomp_server = pid() | atom(), dropbox_server = pid() | atom(), pivot_server = pid() | atom(), module_indexer = pid() | atom(), translation_table = atom()}) -> atom()

Return the name used in the context of a hostname

nested_proplist/1

nested_proplist(Props) -> any()

Scan the props of a proplist, when the prop is a string with "." characters in it then split the prop.

nested_proplist/2

nested_proplist(T, Acc) -> any()

now/0

now() -> any()

Return the current tick count

now_msec/0

now_msec() -> any()

only_digits/1

only_digits(L) -> any()

only_letters/1

only_letters(T) -> any()

os_escape/1

os_escape(S::string() | binary() | undefined) -> string()

Simple escape function for command line arguments

os_filename/1

os_filename(F::string() | binary()) -> string()

Simple escape function for filenames as commandline arguments. foo/"bar.jpg -> "foo/\"bar.jpg"; on windows "foo\\\"bar.jpg" (both including quotes!)

otp_release/0

otp_release() -> integer()

Return the major OTP version as an integer.

pickle/2

pickle(Term, Context) -> Data

This function is deprecated: Use z_crypto:pickle/2 instead.

Encode an arbitrary to a binary. A checksum is added to prevent decoding erlang terms not originating from this server. An Nonce is added so that identical terms vary in their checksum. The encoded value is safe to use in URLs (base64url). The site's sign_key is used as the secret.

pipeline/2

pipeline(Fs::[PipelineFun], As::list()) -> ok | {ok, term()} | {error, term()}

Apply a list of functions to a startlist of arguments. All functions must return: ok | {ok, term()} | {error, term()}. Execution stops if a function returns an error tuple. The return value of the last executed function is returned.

prefix/2

prefix(Sep, List) -> any()

prop_delete/2

prop_delete(Prop, List) -> any()

prop_replace/3

prop_replace(Prop, Value, List) -> List1

Replace a property in a proplist with a new value.

props_merge/2

props_merge(List1, List2) -> List3

Overlay property list List1 over List2, keys in List1 overrule keys in List2.

randomize/1

randomize(List::list()) -> list()

Simple randomize of a list. Not good quality, but good enough for us

ranges/1

ranges(Ns::[integer()]) -> [{integer(), integer()}]

Convert a sorted list of integers to a list of range pairs {From,To}

replace1/3

replace1(F, T, L) -> any()

set_nth/3

set_nth(N, Value, List) -> List2

Update the nth value of a list. The first value is 1. If the list is too short then it is appended with 'undefined' values till the correct length.

split/2

split(N::integer(), L::list()) -> {list(), list()}

Take max N elements from a list.

split_in/2

split_in(L, N) -> any()

vsplit_in/2

vsplit_in(L, N) -> any()

wildcard/1

wildcard(DirName::file:filename_all()) -> [file:filename()]

filename:wildcard version which filters dotfiles like unix does

wildcard/2

wildcard(WildCard::string(), DirName::file:filename_all()) -> [file:filename()]

wildcard_recursive/2

wildcard_recursive(WildCard::string(), DirName::file:filename_all()) -> [file:filename()]

write_terms/2

write_terms(Filename::file:filename_all(), List::[term()]) -> ok | {error, term()}

Write a file that is readable by file:consult/1


Generated by EDoc