Module luafp.object
luafp object module.
Functions
has (path, o) | Checks to see if a table has a path name. |
get (path, o) | Gets the value of the object from the path, else nil. |
getOr (defaultValue, path, o) | Gets the value of the object from the path, else returns the default value. |
Functions
- has (path, o)
-
Checks to see if a table has a path name. This is for tables, not classes enabled via metatables.
Parameters:
- path string name of the property
- o table that might contain the property name
Returns:
- yesOrNo true if it found it, false if it was nil
- error String error message if parameter validation failed
Usage:
local has = require 'luafp.object'.has turtle = {firstName: 'Raphael', weapon: 'Sai'} print(has('weapon', turtle) -- true
- get (path, o)
-
Gets the value of the object from the path, else nil. Supports property names and array indexes separated by dots.
Parameters:
- path string name of the path
- o table that might contain the path name
Returns:
- value value found at the path, else nil
- error String error message if parameter validation failed
See also:
Usage:
local has = require 'luafp.object'.has turtle = { firstName = 'Raphael', type = { name = 'ninja', weapons = { { name = 'Sai', damage = 4 }, { name = 'Fist', damage = 2 } } } } print(get('type.weapons[1].damage')(turtle)) -- 4
- getOr (defaultValue, path, o)
-
Gets the value of the object from the path, else returns the default value. Supports property names and array indexes separated by dots. Like get, except it provides a default vs. nil
Parameters:
- defaultValue value you want returned if nil is found at the path
- path string name of the path
- o table that might contain the path name
Returns:
- value value found at the path, else your provided defaultValue
- error String error message if parameter validation failed
See also:
Usage:
local getOr = require 'luafp.object'.getOr turtle = { firstName = 'Raphael', type = { name = 'ninja', weapons = { { name = 'Sai', damage = 4 }, { name = 'Fist', damage = 2 } } } } print(getOr(0)('type.weapons[3].damage')(turtle)) -- 0