Module l2df.helper

Last updated 2021-05-28 15:24:22

Utility functions collection.

Info:

Functions

dump(var) Serialize variable to string.
hook(obj, key, callback[, caller]) Creates a hook for table's event / function.
requireFolder(folderpath[, keys=false[, pattern]]) Require all scripts from specified directory.
requireFile(filepath) Require a script from file.
newTable() Allocate new table from pool.
freeTable(table) Dispose table and return it back to pool.
copyTable(table[, result]) Deep-copy of the table.
plural(str) Get the plural form of the word.
singular(str) Get the singular form of the word.
isClass(object, class) Determine if object is an instance of a specified class.
isArray(obj) Determine if object is array or not.
array(...) Convert iterator to array
split(str[, sep]) Splitting string to array by separator.
trim(str) Trim spaces at start and end of string.
sign(x) Get sign of value.
clamp(val, min, max) Clamp variable between two edges.
nearest(val, min, max) Get the nearest boundary.
bound(val, min, max) Find out if the object is within the specified values.
round(value, precision) Get rounded value with precision.
notZero(var[, default=1]) Coalesce function for 'non-empty' value.
notNil(value[, default=nil]) Coalesce function for 'non-nil' value.
maximum(arr) Get maximum of array.
max(x, y) Get maximum of two values.
min(x, y) Get minimum of two values.
lerp(a, b, ratio) Linear interpolation beetween two values.
distance(x1, y1, x2, y2) Get distance between two points.
mulMatrix(m1, m2) Matrix multiplication.


Functions

Methods
dump(var)
Serialize variable to string.

Parameters:

  • var mixed Variable to dump.

Returns:

    string
hook(obj, key, callback[, caller])
Creates a hook for table's event / function.

Parameters:

  • obj table Table to hook.
  • key string Table's event / function to hook.
  • callback function Hook's callback function.
  • caller table First parameter to callback function. (optional)
requireFolder(folderpath[, keys=false[, pattern]])
Require all scripts from specified directory. Returns table with them.

Parameters:

  • folderpath string Scripts folderpath.
  • keys boolean Use filenames instead of autoincremental indexes as table's keys. (default false)
  • pattern string If specified only scripts that match pattern would be loaded. (optional)

Returns:

    table
requireFile(filepath)
Require a script from file. Returns loaded module and its filename.

Parameters:

  • filepath string Path to the script-file.

Returns:

  1. table
  2. string
newTable()
Allocate new table from pool.

Returns:

    table
freeTable(table)
Dispose table and return it back to pool.

Parameters:

copyTable(table[, result])
Deep-copy of the table.

Parameters:

  • table table Given table.
  • result table Resulting table. Can be used for the in-place updating. (optional)

Returns:

    table
plural(str)
Get the plural form of the word.

Parameters:

  • str string Word in the singular form.

Returns:

    string
singular(str)
Get the singular form of the word.

Parameters:

  • str string Word in the plural form.

Returns:

    string
isClass(object, class)
Determine if object is an instance of a specified class.

Parameters:

  • object table Intance to check.
  • class table Class to check.

Returns:

    boolean
isArray(obj)
Determine if object is array or not.

Parameters:

  • obj mixed Object to check.

Returns:

    boolean
array(...)
Convert iterator to array

Parameters:

  • ...

Returns:

    table
split(str[, sep])
Splitting string to array by separator.

Parameters:

  • str string Source string.
  • sep string Separator string. (optional)

Returns:

    table
trim(str)
Trim spaces at start and end of string.

Parameters:

Returns:

    string
sign(x)
Get sign of value.

Parameters:

  • x number Specified value.

Returns:

    number
clamp(val, min, max)
Clamp variable between two edges.

Parameters:

  • val number Source value.
  • min number Left (minimum) bound of the value.
  • max number Right (maximum) bound of the value.

Returns:

    number
nearest(val, min, max)
Get the nearest boundary.

Parameters:

  • val number Source value.
  • min number Left (minimum) bound of the value.
  • max number Right (maximum) bound of the value.

Returns:

    number
bound(val, min, max)
Find out if the object is within the specified values.

Parameters:

  • val number Source value.
  • min number Left (minimum) bound of the value.
  • max number Right (maximum) bound of the value.

Returns:

    boolean
round(value, precision)
Get rounded value with precision.

Parameters:

  • value number Source value.
  • precision number Required precision.

Returns:

    number
notZero(var[, default=1])
Coalesce function for 'non-empty' value.

Parameters:

  • var mixed Value to check.
  • default mixed Default value. (default 1)

Returns:

    mixed
notNil(value[, default=nil])
Coalesce function for 'non-nil' value.

Parameters:

  • value mixed Value to check.
  • default mixed Default value. (default nil)

Returns:

    mixed
maximum(arr)
Get maximum of array.

Parameters:

  • arr table Array to process.

Returns:

    int
max(x, y)
Get maximum of two values.

Parameters:

  • x mixed First value.
  • y mixed Second value.

Returns:

    mixed
min(x, y)
Get minimum of two values.

Parameters:

  • x mixed First value.
  • y mixed Second value.

Returns:

    mixed
lerp(a, b, ratio)
Linear interpolation beetween two values.

Parameters:

  • a number Start value.
  • b number End value.
  • ratio number Value used to interpolate between a and b.

Returns:

    number
distance(x1, y1, x2, y2)
Get distance between two points.

Parameters:

  • x1 number First point x.
  • y1 number First point y.
  • x2 number Second point x.
  • y2 number Second point y.

Returns:

    number
mulMatrix(m1, m2)
Matrix multiplication.

Parameters:

  • m1 table Left hand side matrix.
  • m2 table Right hand side matrix.