Brighter Shores:Lua/Helper modules
This is a table of modules and functions that were created specifically for facilitating the creation of other modules. Documentation for modules here is done in the source code with Template:Helper module. In all below descriptions, optional arguments have square brackets around them with default values after a pipe, if appropriate. i.e. foo( x, [y|1] )
means function foo has one required argument, and the second argument is optional and will default to 1.
Module | Function | Type | Use | Example |
---|---|---|---|---|
Array | all( arr, [fn] ) | arr: any[] fn?: any -> boolean | Behaviour depends on the value of fn :
| |
any( arr, [fn] ) | arr: any[] fn?: any -> boolean | Behaviour depends on the value of fn :
| ||
clean( arr ) | arr: any[] -> any[] | Recursively removes all metatables. | ||
clone( arr, [deep] ) | arr: any[] deep?: boolean -> any[] | Make a copy of the input table. Preserves metatables. | ||
contains( arr, val ) | arr: any[] val: any -> boolean | Check if arr contains val . | ||
containsAny( arr, t ) | arr: any[] t: any[] -> boolean | Check if arr contains any of the values in the table t . | ||
containsAll( arr, t ) | arr: any[] t: any[] -> boolean | Check if arr contains all values in the table t . | ||
convolve( x, y ) | x: number[] y: number[] -> number[] | Convolute two number arrays. | ||
condenseSparse( arr ) | arr: any[] -> any[] | Remove nil values from arr while preserving order. | ||
count( arr, fn ) | arr: any[] fn: any -> integer | Behaviour depends on value of val :
| ||
diff( arr, [order|1] ) | arr: number[] order?: number -> number[] | Differentiates arr . The length of the result is #arr - order long. | ||
each( arr, fn ) | arr: any[] fn: fun(elem: any, i?: integer) | Loops over the array part of arr and passes each element as the first argument to fn . This function returns nothing. | ||
filter( arr, fn ) | arr: any[] fn: fun(elem: any, i?: integer): boolean -> any[] | Makes a copy of arr with only elements for which fn returned true. | ||
find( arr, fn, [default] ) | arr: any[] fn: any default?: any -> any?, integer? | Behaviour depends on the value of fn :
| ||
find_index( arr, fn, [default] ) | arr: any[] fn: any default?: any -> integer? | Behaviour depends on the value of fn :
| ||
get( arr, indexes ) | arr: any[] indexes: integer|integer[] -> any[] | Extracts a subset of arr . | ||
int( arr, [start|1], [stop|#arr] ) | arr: number[] start?: number stop?: number -> number[] | Integrates arr from index start to stop . Effectively does . | ||
intersect( arr1, arr2 ) | arr1: any[] arr2: any[] -> any[] | Returns an array with elements that are present in both tables. | ||
intersects( arr1, arr2 ) | arr1: any[] arr2: any[] -> boolean | Checks if the two inputs have at least one element in common. | ||
insert( arr, val, [index], [unpackVal] ) insert( arr, val, [unpackVal] ) | arr: any[] val: any index?: integer unpackVal?: boolean -> any[] | Inserts values into arr . If val is an array and unpackVal is true then the individual elements of val are inserted. index is the location to start the insertion. Default is at the end of arr . | ||
last( arr ) | arr: any[] -> any | Returns the last element of arr . | ||
len( arr ) | arr: any[] -> integer | Returns the length of the array but it also works on proxy arrays like mw.loadData or mw.loadJsonData. | ||
map( arr, fn ) | arr: any[] fn: fun(elem: any, i?: integer): any -> any[] | Returns a new table were each element of arr is modified by fn . | ||
max_by( arr, fn ) | arr: any[] fn: fun(elem: any): any -> any, integer | Find the element for which fn returned the largest value. The returned value of fn needs to be comparable using the < operator. Returns three values: The element with the largest fn value, its fn result, and its index. | ||
max( arr ) | arr: any[] -> any, integer | Find the largest value in the array. The values need to be comparable using the < operator. Returns two values: the element and its index. | ||
min( arr ) | arr: any[] -> any, integer | Find the smallest value in the array. The values need to be comparable using the < operator. Returns two values: the element and its index. | ||
new( [arr|{}] ) | arr?: any[] -> any[] | Turn the input table into an Array. This makes it possible to use the colon : operator to access the Array methods. It also enables the use of math operators with the array.
local x = arr.new{ 1, 2, 3 }
local y = arr{ 4, 5, 6 } -- Alternative notation
mw.logObject( -x ) --> { -1, -2, -3 }
mw.logObject( x + 2 ) --> { 3, 4, 5 }
mw.logObject( x - 2 ) --> { -1, 0, 1 }
mw.logObject( x * 2 ) --> { 2, 4, 6 }
mw.logObject( x / 2 ) --> { 0.5, 1, 1.5 }
mw.logObject( x ^ 2 ) --> { 1, 4, 9 }
mw.logObject( x + y ) --> { 5, 7, 9 }
mw.logObject( x .. y ) --> { 1, 2, 3, 4, 5, 6 }
mw.logObject( (x .. y):reject{3, 4, 5} ) --> { 1, 2, 6 }
mw.logObject( x:sum() ) --> 6
mw.logObject( x:update( {1, 3}, y:get{2, 3} * 2 ) ) --> { 10, 2, 12 }
| ||
newIncrementor( [start|1], [step|1] ) | start?: number step?: number -> Incrementor | Returns a new incrementor function. Every time this incrementor function is called it returns a number step higher than the previous call. The current value can be obtained with inc.n or set inc.n = number where inc is an incrementor function. The step size can be changed with inc.step = number . | ||
range( stop ) range( start, stop, [step|1] ) | start: number stop: number step?: number -> number[] | Returns a table containing a sequence of numbers from start to stop (both inclusive if ints, end-exclusive if floats) by step . range(4) produces {1, 2, 3, 4} (start defaults to 1 ). range(0, 4) produces {0, 1, 2, 3, 4} . When step is given, it specifies the increment (or decrement). | ||
reduce( arr, fn, [accumulator|arr[1]] ) | arr: any[] fn: fun(elem: any, acc: any, i?: integer): any accumulator?: any -> any | Condenses the array into a single value.
For each element If no local t = { 1, 2, 3, 4 }
local sum = arr.reduce( t, function(elem, acc) return acc + elem end ) -- sum == 10
| ||
reject( arr, val ) | arr: any[] val: any -> any[] | Make a copy off arr with certain values removed.
Behaviour for different values of
| ||
rep( val, n ) | val: any n: number -> any[] | Returns a table with n copies of val. | ||
scan( arr, fn, [accumulator|arr[1]] ) | arr: any[] fn: fun(elem: any, acc: any, i?: integer): any accumulator?: any -> any[] | Condenses the array into a single value while saving every accumulator value.
For each element If no local t = { 1, 2, 3, 4 }
local x = arr.scan( t, function(elem, acc) return acc + elem end ) -- x = { 1, 3, 6, 10 }
| ||
set( arr, indexes, values ) | arr: any[] indexes: integer|integer[] values: any|any[] -> any[] | Update a range of index with a range of values.
If if only one value is given but multiple indexes than that value is set for all those indexes. Ifvalues is a table then it must of the same length as indexes . | ||
slice( arr, [start|1], [stop|#arr] ) slice( arr, stop ) | arr: any[] start?: number stop?: number -> any[] | Returns a table containing all the elements of arr between the start and stop indices. The start and stop indices are inclusive. If start or stop are negative values then they are referenced to the end of the table. | ||
split( arr, index ) | arr: any[] index: number -> any[], any[] | Split arr into two arrays. Retuns two tables. The first contains elements from [1, index], and the second from [index + 1, #arr]. | ||
sum( arr ) | arr: number[] -> number | Returns the sum of all elements of arr . | ||
take( arr, count, [start|1] ) | arr: any[] count: number start?: number -> any[] | Extract a subtable from arr of count elements long starting from the start index. | ||
take_every( arr, n, [start|1], [count|#arr] ) | arr: any[] n: integer start?: integer count?: integer -> any[] | Extract a subtable from arr .
local t = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
local x = arr.take_every( t, 2 ) --> x = { 1, 3, 5, 7, 9 }
local x = arr.take_every( t, 2, 3 ) --> x = { 3, 5, 7, 9 }
local x = arr.take_every( t, 2, 3, 2 ) --> x = { 3, 5 }
| ||
unique( arr, [fn] ) | arr: any[] fn?: fun(elem: any): any -> any[] | Return a new table with all duplicates removed. fn is an optional function to generate an id for each element. The result will then contain elements that generated unique ids. The order of first occurance is preserved. | ||
zip( ... ) | ...any[] -> any[][] | Combine elements with the same index from multiple arrays.
local x = {1, 2, 3}
local y = {4, 5, 6, 7}
local z = arr.zip( x, y ) --> z = { { 1, 4 }, { 2, 5 }, { 3, 6 }, { 7 } }
| ||
Break Isolation | get_module_store(name) | (string) | Given a name of a module, return an object. The object that is returned will be the same object when the same name is given, even across different {{#invoke}} s | |
Clean image | clean{ file = [string|'no'], width = [string|nil], height = [string|nil], link = [string|nil], align = [string|nil] } | table | Used to clean up user input; extracts only the file name of the input and returns its own formatted link.
Input is a table with fields:
| |
DPLlua | ask( ... ) | tables | ask takes a series of tables each containing the settings for a DPL query; it will return the same number of result tables as input tables. All formatting settings are stripped from the config. If the config does not contains include , the result will be a simple list of page names.
{ <pagename#1>, <pagename#2>, <pagename#3>, } A query with an { ['include'] = { ['template#1'] = { [1] = val#1, [2] = val#2, ['param1'] = val#3, ['param2'] = val#4, }, ['template#2'] = { [3] = val#5, ['param1'] = val#6, }, ['%0'] = val#7 }, ['title'] = <pagename> } You can also do If the config value The output contains a If the DPL throws an error it will be available in the Differences with normal DPL:
|notcategory = val#1 |notcategory = val#2Note of warning, if you include content containing § symbols the result may be unreliable. If you include a whole template (e.g. include = '{some template}' ), content inside strip markers (not nowiki) can't be cleaned up inside lua so pipe characters (| ) will be replaced with § characters and the { and } characters are replaced by ❴ (U+2774) and ❵ (U+2775). Use include = '{some template}, {some template}:1:2:3' instead for the problem parameters. | |
Experience | _total_xp(level) | number | Returns the total experience needed to reach a level (from 0). | |
_between(level_low, level_high) | number, number | Given a level_low and a level_high, return how much experience is needed, starting at level_low, to reach level_high. This is assuming you start at exactly level_low and end as soon as you reach level_high, with no extra experience. | ||
_to_next(level) | number | Given a level, return the experience needed to get to the next level. Returns nil for level 500 | ||
_level_at(xp) | number | Returns the level you would be at if you had xp experience. When the result is < 500, _total_xp(result) <= xp and xp < _total_xp(result+1) | ||
MerchantHideDiscount | () | No input needed | Returns a Lua Table structured as p[<name of hide>] = <level for discount> | |
Mw.html extension | addClassIf(cond, class) | boolean, string | If cond = true it behaves the same as the normal addClass function, otherwise it's a no-op. Ex.: mw.html.create('div'):addClassIf(true, 'align-left-1') | |
attrIf(cond, name, value) | boolean, string/table, string/nil | Similar to addClassIf | ||
cssIf(cond, name, value) | boolean, string/table, string/nil | Similar to addClassIf | ||
doneIf(cond) | boolean | Similar to addClassIf | ||
tagIf(cond, tag) | boolean, string | Similar to addClassIf | ||
wikitextIf(cond, text) | boolean, string | Similar to addClassIf | ||
na() | N/A | Shortcut for :tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done() | ||
naIf(cond) | boolean | Similar to addClassIf | ||
tr([settings]) | table/nil | Shortcut for :tag('tr') but also auto closes the previous 'tr', 'th' or 'td' tag (so you don't need to add :done() before it). settings is a table with keys:
| ||
th([settings]) | string/table/nil | Shortcut for :tag('th'):wikitext(settings) if settings is a string. Also auto closes the previous 'th' or 'td' tag. settings can also be a table with keys:
| ||
td([settings]) | string/table/nil | Same as :th() . Example:
local tbl = mw.html.create('table')
tbl:tr{ class='sortable' }
:th{'foo', attr={'data-sort-type', 'number'}}
:th('bar')
:tr()
:td('buz')
:attr('data-sort-value', 10)
:td{'N/A', class='table-na'}
| ||
IF(cond) | boolean | Allows for if-blocks without breaking the chain. If the condition is true it is a no-op, if false everything inside the balanced IF-END block will be ignored. Can be nested. Ex.:
mw.html.create('div')
:IF(true)
:wikitext('Conditional text')
:END()
:...
Note: This only prevents elements from being added to your html object, it does not protect against statements that throw errors. I.e mw.html.create('div')
:IF(false)
:wikitext(5 * nil) -- This will still throw an error
:END()
| ||
ELSEIF(cond) | boolean | Used together with IF() . | ||
ELSE() | N/A | Used together with IF() . | ||
END() | N/A | Used together with IF() . Make sure the IF-END tags are balanced, it wont throw an error if they are not. | ||
exec(func, ...) | function, any | Call a function without breaking the chain. See module docs for more info. | ||
addFunction(func, name) | function, string | Add a function to the mw.html class that can then be used on mw.html object. See module docs for more info. | ||
Paramtest | is_empty(arg) | String | Returns true if arg is not defined or contains only whitespace | |
has_content(arg) | String | Returns true if arg exists and does not only contain whitespace | ||
default_to(arg1,arg2) | String, Any value | If arg1 exists and does not only contain whitespace, the function returns arg1, otherwise returns arg2 | ||
defaults{ {arg1,arg2},...} | {String, Any value}... | Does the same as default_to() run over every table passed; for technical reasons, all nil are replaced with false | ||
ProfessionList | currency_cell(amount) | amount : number, or nil | Returns the HTML to create a table currency cell using module:currency._cell .If instead it is supplied with nil then a 10x1 cell is created with the text "unknown". | |
unknown_value_cell(size) | size : integer | Returns the HTML to create a size x1 cell with the text "unknown".Use within :node() | ||
unknown_value_edit_cell(page,size) | page : string, name of pagesize : integer | Returns the HTML to create a size x1 cell with a link to edit the specified page.Use within :node() | ||
generate_recipe_table(queryString) | queryString : string, SMW queryscreenFunction : function, table > boolean | Generates a table of recipes and activities from a Semantic MediaWiki query string and a screening function.
Module:RecipeTreeSearch | ||
one_column_image_text(sort,materials) | sort : string, to sort the multicolumn cell bymaterials : table, each entry is a name/quantity pair for an item to display | Produces 1 cells with multiple lines of text in a standard format to display a list of materials for a recipe. Includes the quantity of each material. | ||
two_column_image_text(sort,image,text,link) | sort : string, to sort the multicolumn cell by
link : string, page to link the image and text to | Produces 2 cells in a standard format to display an image and some text, which link to the same page. For example: the image and name of a skill node, but linking to the item they make. | ||
three_column_image_text(sort,quantity,image,text,link) | sort : string, to sort the multicolumn cell by
link : string, page to link the image and text to | Produces 3 cells in a standard format to display a quantity, an image, and some text, which link to the same page. For example: the quantity, image, and name of a recipe product. | ||
Purge | _purge( anchor, text, tag, noinplace ) | string/table, string, string, boolean | Generates a purge link
All args are optional.
| |
Quantity box | _main(quantity) | String | Takes inputs and gives outputs like Template:Quantity box. See the template documentation for more info. | |
RecipeTreeSearch | main(item) | item: string name of an item page | Returns a Lua Table structured as a Recipe, but containing additional information about a multi-stage crafting process such including: All base materials, including their buy price in shops All intermediate materials, including both the number of materials and the number of times the crafting step needs to be performed Total values for XP, KP, duration | |
treeSearch(inputs) | inputs: table of name/quantity pairs for the inputs to a recipe | Use this function on the inputs to a recipe rather than the name of the output of a recipe. This allows the tree to be searched before the final recipe has been made.
IMPORTANT: within the Infobox Recipe that constructs a recipe, it is imporant to use this function instead of main(), so that this module will not attempt to use the recipe before it has been constructed. Returns a the same structure Lua Table as main(), however it lacks any information on the final step. | ||
getRecipe(item) | item: string name of an item page | Returns a Lua Table structured as a Recipe for the single step | ||
getShopBuyPrice(item) | item: string name of an item page | Returns the value that the item can be bought at, or nil if it cannot be bought | ||
getShopSellPrice(item) | item: string name of an item page | Returns the value that the item can be sold for, or nil if it cannot be sold | ||
Yesno | (arg) | Any value | Reads arg for yes/no and returns the appropriate boolean or nil | |
(arg1,arg2) | Any value, Any value | Reads arg1 for yes/no and returns the appropriate boolean; returns arg2 if arg1 was not an applicable value |