Module Filter

Some simple filter functions.

Module Status: Polishing. Compatibility: Pure Lua.

Usage:

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

Simple

SKIP No-Op, does nothing at all.
TRUE() Always returns boolean true.
FALSE() Always returns boolean false.
VALID(obj) Returns obj.valid, the validity of factorio LuaObjects.
INVALID(obj) Returns true for factorio LuaObjects that are not valid.
PASS(obj) Returns the object given.

Factory

string_prefix(prefix)
string_postfix(postfix)
string_infix(infix)
string_pattern(pattern[, init=1])
true_object_array(sarr) Whitelist / Allowlist.
false_object_array(sarr) Blacklist / Blocklist.
table_value(TableValueFilterSpecification) Generates generalized table filter functions.
chain(RecursiveFilterChainSpecification) Composes multiple filters into one.


Simple

SKIP
No-Op, does nothing at all.
TRUE()
Always returns boolean true.
FALSE()
Always returns boolean false.
VALID(obj)
Returns obj.valid, the validity of factorio LuaObjects.

Parameters:

Usage:

    for k,entity in Iter.filtered_pairs(entities,Filter.VALID) do
      print(entity.name,'is valid!')
      end
    
INVALID(obj)
Returns true for factorio LuaObjects that are not valid.

Parameters:

Usage:

    for k,entity in Iter.filtered_pairs(entities,Filter.INVALID) do
      print(k,'is not valid anymore!')
      end
    
PASS(obj)
Returns the object given. For when the syntax requires a function but you don't want to actually change the object.

Parameters:

Returns:

    AnyValue obj

Factory

string_prefix(prefix)

Parameters:

  • prefix string The exact plain prefix to look for. Not a lua pattern.

Returns:

    function The filter function f(str) returns true if str starts with the prefix.
string_postfix(postfix)

Parameters:

  • postfix string The exact plain postfix to look for. Not a lua pattern.

Returns:

    function The filter function f(str) returns true if str ends with the postfix.
string_infix(infix)

Parameters:

  • infix string The exact plain infix to look for. Not a lua pattern.

Returns:

    function The filter function f(str) returns true if str contains the infix anywhere, including as a pre- or post-fix.
string_pattern(pattern[, init=1])

Parameters:

Returns:

    function The filter function f(str) returns true if the pattern matches the string.
true_object_array(sarr)
Whitelist / Allowlist.

Parameters:

  • sarr SparseArray A list of objects that should return true when passed to the filter. All other objects will return false.

Returns:

    function The filter function f(obj) returns a boolean.
false_object_array(sarr)
Blacklist / Blocklist.

Parameters:

  • sarr SparseArray A list of objects that should return false when passed to the filter. All other objects will return true.

Returns:

    function The filter function f(obj) returns a boolean.
table_value(TableValueFilterSpecification)
Generates generalized table filter functions.

The TableValueFilterSpecification must contain either "is" or "has" but not both.

Parameters:

  • TableValueFilterSpecification
    • ... string or number The DenseArray part of a TableValueFilterSpecification is a TablePath.
    • is NotNil or DenseArray If this is a DenseArray then the filter will return true if the value at the above given path in the table is equal to any of the values in the array. For convenience if you're only looking for a single target value you can give it directly without putting it in a table. (optional)
    • has DenseArray Configures the filter to look for values in the subtable found in the filtered object at the given path. The [1] key in this array specifies the comparision mode and must be a literal string 'or','nor','and' or 'nand'. All elements [2] to [n] represent the accepted values in the subtable. When no accepted values are given 'and' is always true and 'or' is always false. (optional)

Returns:

    function The TableValueFilter function f(tbl). Calling f with anything that is not a table will always return false.

Usage:

    local tblA = {keyA = {keyB = {1,2,3,4}, keyC = 1}}
    local tblB = {keyA = {keyB = {3,4,5,6}}}
    
    -- "is" is an exact comparision
    print(Filter.table_value {'keyA','keyB',is  =  1         } (tblA) )
    > false
    print(Filter.table_value {'keyA','keyC',is  =  1         } (tblA) )
    > true
    
    -- "and" must have all elements given
    print(Filter.table_value {'keyA','keyB',has = {'and',1,2}} (tblA) )
    > true
    print(Filter.table_value {'keyA','keyB',has = {'and',2,3}} (tblB) )
    > false
    
    -- "or" needs only one element
    print(Filter.table_value {'keyA','keyB',has = {'or' ,1,6}} (tblA) )
    > true
    print(Filter.table_value {'keyA','keyB',has = {'or' ,1,6}} (tblB) )
    > true
    
chain(RecursiveFilterChainSpecification)
Composes multiple filters into one.

Parameters:

  • RecursiveFilterChainSpecification
    • 1 string How the filters should be combined. Must be one of the literal combination mode strings "and", "nand", "or" or "nor".
    • ... table or function The filters to be combined. Each value can be either a custom filter function f(obj), a TableValueFilterSpecification or another RecursiveFilterChainSpecification.

      Note: The first path key in an in-line TableValueFilterSpecification can not be a filter chain combination mode string ("and", "or", etc.). Cou have to pre-compile the TableValueFilter in that case.

Usage:

    local f = Filter.chain { -- RecursiveFilterChainSpecification
      'or',
      function(x) return (type(x) == 'number') and (x > 5) end, -- custom function
      { -- another RecursiveFilterChainSpecification
        'and',
        {'number', is ={5,42} },                   -- TableValueFilterSpecification
        {'names' , has={'or','Michiko','Tarou'} }, -- TableValueFilterSpecification
        }
      }
    print( f(5) ) -- not x > 5
    > false
    print( f(6) ) -- x > 5
    > true
    print( f{number = 5 , names = {'Mamoru' } } ) -- wrong name
    > false
    print( f{number = 42, names = {'Michiko'} } ) -- number and name ok
    > true
    
generated by LDoc 1.4.6 Last updated 2021-09-10 19:51:19