z_utils (zotonic_core v1.0.0-rc.10)

Misc utility functions for zotonic

Link to this section Summary

Functions

Check if two arguments are equal, optionally converting them
Check if an assertion is ok or failed
Ensure that the given string matches an existing module. Used to prevent a denial of service attack where we exhaust the atom space.
Safe erase of process dict, keeps some 'magical' proc_lib vars
Filter all filenames which start with a dot.
Flush all incoming messages, used when receiving timer ticks to prevent multiple ticks.
Return the current universal time in seconds
Get a value from a map or a proplist. Return 'undefined' if The value was not present.
Get a value from a map or a proplist. Return the default value if The value was not present.
Group by a property or m_rsc property, keeps the input list in the same order.
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}]]} ]
Make a property list based on the value of a property For example: [ [{a,b}], [{a,c}] ] gives [{a, [{a,b}]}, {c, [[{a,c}]]}]
Check if a value is 'empty'
Multinode is_process_alive check
Check if the parameter could represent the logical value of "true"
Javascript escape, see also: http://code.google.com/p/doctype/wiki/ArticleXSSInJavaScript
Create a javascript object from a proplist
Return an abspath to a directory relative to the application root.
Return a list of all files in a directory, recursive depth first search for files not starting with a '.'
Return the name used in the context of a hostname
Scan the props of a proplist, when the prop is a list with a $. characters in it then split the prop.
Return the current tick count
Simple escape function for command line arguments
Simple escape function for filenames as commandline arguments. foo/"bar.jpg -> "foo/\"bar.jpg"; on windows "foo\\\"bar.jpg" (both including quotes!)
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.
Replace a property in a proplist
Simple randomize of a list. Not good quality, but good enough for us
Convert a sorted list of integers to a list of range pairs {From,To}
Take max N elements from a list.
filename:wildcard version which filters dotfiles like unix does
Write a file that is readable by file:consult/1

Link to this section Functions

Link to this function

are_equal(Arg1, Arg2)

Check if two arguments are equal, optionally converting them
Link to this function

assert(_, Error)

Check if an assertion is ok or failed
Link to this function

checksum(Data, Context)

Link to this function

checksum_assert(Data, Checksum, Context)

Link to this function

decode_value(Data, Context)

Link to this function

decode_value_expire(Data, Context)

Link to this function

depickle(Data, Context)

Link to this function

encode_value(Value, Context)

Link to this function

encode_value_expire(Value, Date, Context)

Link to this function

ensure_existing_module(ModuleName)

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

erase_process_dict()

Safe erase of process dict, keeps some 'magical' proc_lib vars
Link to this function

filter_dot_files(Names)

Filter all filenames which start with a dot.
Link to this function

flush_message(Msg)

Flush all incoming messages, used when receiving timer ticks to prevent multiple ticks.
Return the current universal time in seconds
Link to this function

get_value(Key, Map)

-spec get_value(term(), map() | list()) -> term().
Get a value from a map or a proplist. Return 'undefined' if The value was not present.
Link to this function

get_value(Key, Map, Default)

-spec get_value(term(), map() | list(), term()) -> term().
Get a value from a map or a proplist. Return the default value if The value was not present.
Link to this function

group_by(L, Prop, Context)

Group by a property or m_rsc property, keeps the input list in the same order.
Link to this function

group_proplists(Prop, Rest)

-spec group_proplists(atom(), [{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}]]} ]
Link to this function

hex_decode(Value)

Link to this function

hex_encode(Value)

Link to this function

hmac(Type, Key, Data)

Link to this function

index_proplist(Prop, List)

-spec index_proplist(term(), [{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}]]}]
Link to this function

is_empty(Trans)

Check if a value is 'empty'
Link to this function

is_process_alive(Pid)

Multinode is_process_alive check
Check if the parameter could represent the logical value of "true"
Link to this function

join_defined(Sep, List)

Link to this function

js_escape(Trans, OptContext)

Javascript escape, see also: http://code.google.com/p/doctype/wiki/ArticleXSSInJavaScript
Create a javascript object from a proplist
Link to this function

js_object(L, OptContext)

Link to this function

js_object(L, T, Context)

Return an abspath to a directory relative to the application root.
Link to this function

list_dir_recursive(Dir)

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

name_for_site(Name, Context)

-spec name_for_site(Name :: atom(), atom() | #context{}) -> atom().
Return the name used in the context of a hostname
Link to this function

nested_proplist(Props)

Scan the props of a proplist, when the prop is a list with a $. characters in it then split the prop.
Link to this function

nested_proplist(T, Acc)

Return the current tick count
Link to this function

only_letters(T)

-spec os_escape(string() | binary() | undefined) -> string().
Simple escape function for command line arguments
-spec os_filename(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!)
Link to this function

pickle(Data, Context)

Link to this function

pipeline(Fs, As)

-spec pipeline([PipelineFun], list()) -> ok | {ok, term()} | {error, term()}
            when PipelineFun :: function() | mfa().
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.
Link to this function

prefix(Sep, List)

Link to this function

prop_delete(Prop, List)

Link to this function

prop_replace(Prop, Value, List)

Replace a property in a proplist
Link to this function

props_merge(Ps, Xs)

Link to this function

randomize(List)

-spec randomize(list()) -> list().
Simple randomize of a list. Not good quality, but good enough for us
-spec ranges([integer()]) -> [{integer(), integer()}].
Convert a sorted list of integers to a list of range pairs {From,To}
Link to this function

replace1(F, T, L)

Link to this function

set_nth(N, V, L)

-spec split(integer(), list()) -> {list(), list()}.
Take max N elements from a list.
Link to this function

vsplit_in(L, N)

Link to this function

wildcard(DirName)

-spec wildcard(DirName :: file:filename_all()) -> [file:filename()].
filename:wildcard version which filters dotfiles like unix does
Link to this function

wildcard(WildCard, DirName)

-spec wildcard(WildCard :: string(), DirName :: file:filename_all()) -> [file:filename()].
Link to this function

wildcard_recursive(WildCard, DirName)

-spec wildcard_recursive(WildCard :: string(), DirName :: file:filename_all()) -> [file:filename()].
Link to this function

write_terms(Filename, List)

-spec write_terms(file:filename_all(), [term()]) -> ok | {error, term()}.
Write a file that is readable by file:consult/1