Module Verificate

Performance optimized multi- and complex-type detection and comparison.

See isType for details.

Module Status: Polishing.

Usage:

    local Verificate = require('__eradicators-library__/erlib/lua/Verificate')()
    

isType

isType This table contains all type checking functions offered by this module.

isType → Factorio

isType.LuaObject() Is this any kind of factorio lua object?
isType.LuaFactorioClassName() This module procedurally generates a checker function for every factorio class.

isType → Number

isType.NaturalNumber() n > 0 and n % 1 == 0.
isType.Integer() n % 1 == 0.

isType → Table

isType.NonEmptyTable()
isType.EmptyTable()
isType.PlainTable() A lua table, not a factorio LuaObject.

isType → Array

isType.Array() DenseArray or SparseArray, not MixedTable.
isType.DenseArray()
isType.NonEmptyArray() DenseArray or SparseArray, not MixedTable.
isType.EmptyArray()

isType → Boolean

isType.true()
isType.false()

isType → String

isType.EmptyString(obj) The empty string of length 0.
isType.NonEmptyString(obj) A string, but not the empty one.
isType.WhiteSpaceString(obj) Space of any length.

isType → Collections

isType.NonEmptyArrayOfNonEmptyString()
isType.NonEmptyTableOfFunction()
isType.NonEmptyDenseArrayOfNaturalNumber()

isType → Custom

isType.NotNil()
isType.Vector() A size-4 array of numbers.
isType.Position() A factorio {[1]=,[2]=} or {x=,y=} table.
isType.Probability() Float 0 ≤ n ≤ 1.
isType.UnitInterval() Float 0 ≤ n ≤ 1.
isType.TablePath() TablePath
isType.InputName() InputName

Verify

verify(obj, typ[, ...]) assert() like checker with support for all isType checks.
verify_or(obj, types[, ...]) Performes multiple type checks in order.
verify_and(obj, types[, ...]) Performes multiple type checks in order.
assert(obj, ...) Shorthand for Verificate.verify(obj,'true',...).
wrap(functions, checkers) Provides function input-checking wrappers.


isType

isType
This table contains all type checking functions offered by this module.

For the 8 primary lua types it offers functions to check any combination of up to three types at once by concatenateing the names with a | pipe. For these combinations you can also use the short names: nil, num, str, bool, tbl, func, udat instead of the full type name.

Note: This module also automatically generates a "nil|" variant for non-primary type methods. I.e. isType['nil|LuaPlayer'], etc..

Note: To keep the documentation concise paramters for type functions are not documented per function. Every function takes exactly one argument - the object to type-check - and returns a boolean.

Performance Tip: Combinations starting with nil, i.e. "nil", "nil|string", "nil|number|string" are optimized for situations where the object to be checked is expected to be nil most of the time (~90% faster if obj is nil, but 10% slower if obj is not nil). If you expect the object to be NotNil most of the time then you should put nil at the end of the combination, i.e. "str|nil", "num|str|nil".

Experimental: You can now use the "|" pipe syntax for any combination of types. A combined function will be transparently generated the first time you use a combination. You should always use the exact same string for each combination if you do not want multiple functions to be generated.

Usage:

    local isNumStr = Verificate.isType['number|string']
    
    if isNumStr(42) then print('Yes!') end
    > Yes!
    
    if isNumStr('word') then print('Yes!') end
    > Yes!
    
    if not isNumStr(nil) then print('Not!') end
    > Not!
    
    if Verificate.isType['nil|str|tbl']( {'empty table'} ) then print('Yes!') end
    > Yes!
    

isType → Factorio

isType.LuaObject()
Is this any kind of factorio lua object? A LuaPlayer, LuaEntity, etc...?
isType.LuaFactorioClassName()
This module procedurally generates a checker function for every factorio class.

Usage:

    if Verificate.isType.LuaGameScript(game) then print('Can do this!') end
    > Can do this!
    
    if Verificate.isType.LuaPlayer(game.player) then print('Can also do this!') end
    > Can also do this!
    
    print(Verificate.isType.LuaPlayer(game))
    > false
    

isType → Number

isType.NaturalNumber()
n > 0 and n % 1 == 0.
isType.Integer()
n % 1 == 0.

isType → Table

isType.NonEmptyTable()
isType.EmptyTable()
isType.PlainTable()
A lua table, not a factorio LuaObject.

isType → Array

isType.Array()
DenseArray or SparseArray, not MixedTable.
isType.DenseArray()
isType.NonEmptyArray()
DenseArray or SparseArray, not MixedTable.
isType.EmptyArray()

isType → Boolean

isType.true()
isType.false()

isType → String

isType.EmptyString(obj)
The empty string of length 0.

Parameters:

  • obj
isType.NonEmptyString(obj)
A string, but not the empty one.

Parameters:

  • obj
isType.WhiteSpaceString(obj)
Space of any length. Doesn't seem to work for non-Ascii space even though the lua manual says it should.

Parameters:

  • obj

isType → Collections

isType.NonEmptyArrayOfNonEmptyString()
isType.NonEmptyTableOfFunction()
isType.NonEmptyDenseArrayOfNaturalNumber()

isType → Custom

isType.NotNil()
isType.Vector()
A size-4 array of numbers. Ignores extra content.
isType.Position()
A factorio {[1]=,[2]=} or {x=,y=} table. Ignores extra content. Also accepts mixed definitions like {[1]=,y=}.
isType.Probability()
Float 0 ≤ n ≤ 1.
isType.UnitInterval()
Float 0 ≤ n ≤ 1. Unit interval
isType.TablePath()
TablePath
isType.InputName()
InputName

Verify

verify(obj, typ[, ...])
assert() like checker with support for all isType checks. Raises an error if the input object is not of the expected type.

Parameters:

Returns:

    AnyValue or error If the check succeeds this returns obj, if not a hard error is raised.

Raises:

VerificationError with a standard error message plus anything you specified in addition.

Usage:

    local function my_adder(x)
      Verificate.verify(x,'number|nil',"Your","Error","Message.")
      x = x or 0
      return x + 1
      end
    
    print(my_adder(5))
    > 6
    
    print(my_adder('five'))
    > Verification failed!
    > expected: number|nil
    > received: five.
    > Your Error Message.
    
verify_or(obj, types[, ...])
Performes multiple type checks in order.

Parameters:

  • obj AnyValue
  • types DenseArray The types in the order they should be verified.
  • ... AnyValue Anything you want to show up in the error message. (optional)

Returns:

    AnyValue or error If at least one check succeeds this returns obj, if not a hard error is raised.
verify_and(obj, types[, ...])
Performes multiple type checks in order.

Parameters:

  • obj AnyValue
  • types DenseArray The types in the order they should be verified.
  • ... AnyValue Anything you want to show up in the error message. (optional)

Returns:

    AnyValue or error If all checks succeed this returns obj, if not a hard error is raised.
assert(obj, ...)
Shorthand for Verificate.verify(obj,'true',...). Allows fully serialized multi-part error messages with the same syntax as assert.

Parameters:

  • obj
  • ...
wrap(functions, checkers)
Provides function input-checking wrappers. When a wrapped function is called then first the checker function is called with all arguments. Only if the checker function returns truthy is the real function called.

Note: The checker is expected to raise an error if the check failed. If it does not then Verificate.Wrap will raise a generic error. It is not possible to continue execution after a failed check.

Parameters:

  • functions table or function All functions that you want wrapped. If this is a single function then checkers must also be a single function.
  • checkers table or function All checking functions, indexed by the same keys as the to-be-wrapped functions.

Returns:

    table A table of wrapped functions. Functions for which checkers did not contain a corresponding function will be returned unwrapped.
generated by LDoc 1.4.6 Last updated 2021-09-10 19:51:19