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:

  1. yesOrNo true if it found it, false if it was nil
  2. 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:

  1. value value found at the path, else nil
  2. 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:

  1. value value found at the path, else your provided defaultValue
  2. 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
generated by LDoc 1.4.6 Last updated 2018-01-21 13:40:25