Bundle of functions that are useful in many cases.
Got here, https://gist.github.com/tylerneylon/59f4bcf316be525b30ab
Used to convert a json string to a table or a table to string.
local json = require 'json'
json.parse(my_string) -- will return a table containing all objects
json.stringify(my_table) -- will return a printable stringUse this by adding local utilities = require 'utilities'.
Remove spaces from string (before first and after last word)
utilities.trim(str)Return the index of the first elem found in array.
utilities.get_elem_in_table(table, elem)Concatenate two arrays, add t2 elems after t1 ones, at same depth.
local t1 = {1, 2}
local t2 = {3, 4}
utilities.concatenate_arrays(t1, t2) -- return {1, 2, 3, 4}Split a string into a table of elements, in a same way as bash awk or php explode.
local str = 'Hello world'
local separator = ' '
utilities.split(str, separator) -- return {'Hello', 'world'} Execute a table of functions with parameters provided. Params must have the same index that actions, params can be nil.
Arguments:
actionstable (eg: actions[1] = print).argstable (eg: args[1] = "Hello world"), can be nil, and can contain nil elems.
local actions = {print, print}
local param = {'glastis > guigur', 'Hi'}
utilities.exec_function_table(actions, param) -- will print 'glastis > guigur' and 'Hi'
utilities.exec_function_table_revert(actions, param) -- will print 'Hi' and 'glastis > guigur' Write calltrace into a log file, with a custom text to see which debug_info function is called.
Arguments:
morestring (eg: "in buggy_function main loop"), can be nil.calltraceboolean, if true will write the calltrace to the file.
local more = 'We are inside buggy_function, with parametter test, before the infinite loop'
local calltrace = true
utilities.debug_info(more, calltrace)Like the php var_dump, it returns a variable (table, number, string...) in string.
Arguments:
varvariable to be dumped.printvalboolean, if the dump should be printed, can be nil.max_depthinteger, if provided, limit the exploration of sub-tables (in case of recursive or huge tables).cur_depthused by var_dump itself, leave it nil.
local var = {1, 'DooM', {'asdasd'}}
utilities.var_dump(var, true)Args:
numbernumber to round.decimalsamount of digits after the decimal point.ext_absif true, 0.5 will be rounded to 1, otherwise, it will be rounded to 0.
local numer = 1.1234
local decimals = 2
local ext_abs = true
utilities.round(number, decimals) -- return 1.12
utilities.round(number, decimals, ext_abs) -- return 1.12Use this by adding local file = require 'file'.
Return true if file exists, false otherwise.
local filepath = '/home/glastis/foo.txt'
file.is_existing(filepath)Read and return all file content.
local filepath = '/home/glastis/foo.txt'
file.read(filepath)Write and string in file
Args:
filepathstring, path to file, eg:/home/glastis/foo.txtstrstring, content to writemodestring,"a"to append,"w"to overwrite file or"w+"to delete and write to file. Default is"a".
file.write(filepath, str, mode)Read and return one line from a file
Args:
filepathstring, path to file, eg: /home/glastis/foo.txtfilefile, if nil, will open filepath.
local file, line = file.read_line('/home/glastis/foo.txt')
while file do
print(line)
file, line = file.read_line('/home/glastis/foo.txt')
end