Module Tool
A bunch of small experimental utilities that don't clearly belong into any of the other modules or do not have a good name yet.
Factorio already has "util" so this is Tool. Basically a collection of functions that don't fit into any other module. When a new module is added that is a good fit for one of these they will be moved there on short notice so watch the changelog carefully.
Module Status: Work in progress forever.
Usage:
local Tool = require('__eradicators-library__/erlib/factorio/Tool')()
Draft
try_require(path) | Does not raise an error if the file doesn't exist. |
Section
KeepArgsCall(f, ...) | In-line call a function but return the arguments first. |
IfNilCall(value, f, ...) | Calls a function when a value is nil. |
SelectCall(condition, f1, f2, ...) | Calls one of two functions depending on a condition. |
CastType(value, typ, caster, typer) | Converts a value to a type if it is not yet of that type. |
First(...) | Get the first non-nil value. |
Last(...) | Get the last non-nil value. |
Select(condition, true_value, false_value) | In-line ternary decision that allows nil and false. |
Select_second(...) | Returns the second argument given. |
Select_third(...) | Returns the third argument given. |
tick_to_time(tick) | Converts a factorio tick to hours, minutes and seconds. |
Import(relative_path) | Require()'s lua files relative to the calling file. |
cross_require(path) | Loads other mods files. |
Draft
- try_require(path)
-
Does not raise an error if the file doesn't exist.
Parameters:
- path string
Returns:
Section
- KeepArgsCall(f, ...)
-
In-line call a function but return the arguments first.
Parameters:
Returns:
Usage:
print(Tool.KeepArgsCall(function(a,b,c) return a+b+c end, 1, 2, 3)) > 1 2 3 6
local arg1, prototype = Tool.KeepArgsCall(SimpleHotkey,{'some','values'})
- IfNilCall(value, f, ...)
-
Calls a function when a value is nil.
Parameters:
Returns:
-
If value was not nil it's value will be returned. Otherwise
the result of calling f(...) will be returned.
- SelectCall(condition, f1, f2, ...)
-
Calls one of two functions depending on a condition.
Parameters:
- condition AnyValue If this is truthy calls f1, else calls f2.
- f1 function
- f2 function
- ... AnyValue Extra arguments for the functions.
Returns:
-
AnyValue
The return value of calling f1(...) or f2(...).
- CastType(value, typ, caster, typer)
-
Converts a value to a type if it is not yet of that type.
Parameters:
- value AnyValue The input value.
- typ string The desired type of the input value.
- caster function The function that converts the input value if it's not of the desired type.
- typer function The function that determines the type of the input value. (default type)
Returns:
-
AnyValue
The input value or the result of caster(value).
- First(...)
-
Get the first non-nil value.
For when you can't use
return a or b
because false is a valid return value.Parameters:
Returns:
-
NotNil
The first value that was not nil. Can return boolean false
if that was the first applicable value.
- Last(...)
-
Get the last non-nil value.
Parameters:
Returns:
-
NotNil
The last value that was not nil. Can return boolean false
if that was the last applicable value.
- Select(condition, true_value, false_value)
-
In-line ternary decision that allows nil and false.
The lua idiom
(c and a or b)
doesn't work if false or nil are possible values for a or b.Not to be confused with select.
Parameters:
- condition AnyValue If this is a truthy value then the true_value will be returned, else the false_value will be returned.
- true_value AnyValue
- false_value AnyValue
Returns:
-
AnyValue
The true_value or false_value.
- Select_second(...)
-
Returns the second argument given.
About ~5% faster than select(2,...)
Parameters:
- ... AnyValue
Returns:
- Select_third(...)
-
Returns the third argument given.
About ~5% faster than select(3,...)
Parameters:
- ... AnyValue
Returns:
- tick_to_time(tick)
-
Converts a factorio tick to hours, minutes and seconds.
Parameters:
- tick NaturalNumber
Returns:
-
table
A table
{h=,m=,s=}
of NaturalNumbers. - Import(relative_path)
-
Require()'s lua files relative to the calling file.
Parameters:
- relative_path string Directories are seperated by "/" forward slash, ".." two full stops go up one directory.
Returns:
- cross_require(path)
-
Loads other mods files.
Contrary to Factorio's built-in require this works correctly
even if the target file itself uses relative-path requires to load more files.
This is a workaround for: Minor Issue: Requiring lua scripts from other mods
Parameters:
- path string An absolute path to a file in another mod.
Returns:
-
NotNil
Whatever the file returned.