Module restia.utils
Utility functions for Restia
Info:
- License: Unlicense
- Author: DarkWiiPlayer
Functions
escape (str) | Escapes special HTML characters in a string |
mixin (first, second, ...) | Mixes several tables into another and returns it. |
normalizeindent (block) | Removes excessive indentation from a block of text |
unpipe (block) | Removes leading whitespace up to and including a pipe character. |
deepindex (tab, path) | Indexes tables recursively with a chain of string keys |
deepinsert (tab, path, value) | Inserts a table into a nested table following a path. |
deepen (tab) | Turns a flat table and turns it into a nested table. |
tree.insert (head, route, value) | Inserts a value into a tree. |
tree.get (head, route) | Gets a value from a tree. |
deepconcat (tab, separator) | Recursively concatenates a table |
stack (level) | Returns a list containing the result of debug.getinfo for every level in the current call stack. |
randomhex (n) | Returns a random hexadecimal string with N bytes |
files (dir) | Returns an iterator over all the files in a directory and subdirectories |
delete (path) | Deletes a file or directory recursively |
builddir (prefix, tab) | Builds a directory structure recursively from a table template. |
Functions
- escape (str)
-
Escapes special HTML characters in a string
Parameters:
- str
- mixin (first, second, ...)
-
Mixes several tables into another and returns it.
Parameters:
- first
- second
- ...
- normalizeindent (block)
-
Removes excessive indentation from a block of text
Parameters:
- block
- unpipe (block)
-
Removes leading whitespace up to and including a pipe character.
This is used to trim off unwanted whitespace at the beginning of a line.
This is hopefully a bit faster and more versatile than the normalizeindent function.
Parameters:
- block
- deepindex (tab, path)
-
Indexes tables recursively with a chain of string keys
Parameters:
- tab
- path
- deepinsert (tab, path, value)
-
Inserts a table into a nested table following a path.
The path string mimics normal chained indexing in normal Lua.
Nil-elements along the path will be created as tables.
Non-nil elements will be indexed and error accordingly if this fails.
Parameters:
- tab table A table or indexable object to recursively insert into
- path table A string describing the path to iterate
- value The value that will be inserted
Usage:
utils.deepinsert(some_table, 'foo.bar.baz', value)
- deepen (tab)
-
Turns a flat table and turns it into a nested table.
Parameters:
- tab
Usage:
local deep = restia.utils.deep { ['foo.bar.baz'] = "hello"; ['foo[1]'] = "first"; ['foo[2]'] = "second"; } -- Is equal to local deep = { foo = { "first", "second"; bar = { baz = "hello" }; } }
- tree.insert (head, route, value)
-
Inserts a value into a tree.
Every node in the tree, not only leaves, can hold a value.
The special index __value is used for this and should not appear in the route.
Parameters:
- head table The tree to insert the value into.
- route table A list of values to recursively index the tree with.
- value Any Lua value to be inserted into the tree.
Returns:
-
table
The head node of the tree.
See also:
Usage:
local insert = restia.utils.tree.insert local tree = {} insert(tree, {"foo"}, "value 1") -- Nodes can have values and children at once insert(tree, {"foo", "bar"}, "value 2") -- Keys can be anything insert(tree, {function() end, {}}, "value 2")
- tree.get (head, route)
-
Gets a value from a tree.
Parameters:
- head table The tree to retreive the value from.
- route table A list of values to recursively index the tree with.
Returns:
-
The value at the described node in the tree.
See also:
Usage:
local tree = { foo = { bar = { __value = "Some value" }, __value = "Unused value" } } restia.utils.tree.get(tree, {"foo", "bar"})
- deepconcat (tab, separator)
-
Recursively concatenates a table
Parameters:
- tab
- separator
- stack (level)
-
Returns a list containing the result of debug.getinfo for every level in
the current call stack. The table also contains its length at index
n
.Parameters:
- level
- randomhex (n)
-
Returns a random hexadecimal string with N bytes
Parameters:
- n
- files (dir)
-
Returns an iterator over all the files in a directory and subdirectories
Parameters:
- dir string The directory to look in
Returns:
-
function
Iterator over the file names
Usage:
for file in utils.files 'views' do print('found view: ', file) end
- delete (path)
-
Deletes a file or directory recursively
Parameters:
- path string The path to the file or directory to delete
- builddir (prefix, tab)
-
Builds a directory structure recursively from a table template.
Parameters:
- prefix string A prefix to the path, aka. where to initialize the directory structure.
- tab table A table representing the directory structure. Table entries are subdirectories, strings are files, false means delete, true means touch file, everything else is an error.
Usage:
builddir { sub_dir = { empty_file = '' } file = 'Hello World!'; }