Module:Utils/doc: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 3: | Line 3: | ||
<code>def(value, default)</code> | <code>def(value, default)</code> | ||
:Returns <code>value</code> if it is not <code>nil</code> or <code>""</code>, and <code>default</code>otherwise | :Returns <code>value</code> if it is not <code>nil</code> or <code>""</code>, and <code>default</code>otherwise | ||
<code>_a(arg)</code> | <code>_a(arg)</code> | ||
:Extracts the argument from the common ways mediawiki passes arguments to modules: | :Extracts the argument from the common ways mediawiki passes arguments to modules: | ||
| Line 8: | Line 9: | ||
:* if <code>arg</code> is a table and that table has a <code>args</code> field, <code>arg.args[1]</code> is returned (Called from MediaWiki’s <nowiki>{{#invoke:}}</nowiki> parser function) | :* if <code>arg</code> is a table and that table has a <code>args</code> field, <code>arg.args[1]</code> is returned (Called from MediaWiki’s <nowiki>{{#invoke:}}</nowiki> parser function) | ||
:* if <code>arg</code> is a table but doesn’t contain an <code>args</code> field, <code>args[1]</code> is returned (Called with a table as an argument list<ref>Some mediawiki functions do that, idk why — Annwan</ref>) | :* if <code>arg</code> is a table but doesn’t contain an <code>args</code> field, <code>args[1]</code> is returned (Called with a table as an argument list<ref>Some mediawiki functions do that, idk why — Annwan</ref>) | ||
<code>date(y, m, d, short = false)</code> | <code>date(y, m, d, short = false)</code> | ||
:Formats a date. | :Formats a date. | ||
:If <code>short</code> is set to <code>true</code>, uses shortened months name instead of full length month names. | :If <code>short</code> is set to <code>true</code>, uses shortened months name instead of full length month names. | ||
<code>find(tab, field, val)</code> | |||
:Locate an entry in a list of table (useful for search through json data) | |||
:'''Parameters''' | |||
::<code>tab: table</code>: the list to search through | |||
::<code>field: any</code>: the field in the objects to search by | |||
::<code>value: any</code>: the value to match against | |||
:'''Returns''' | |||
::<code>nil, nil</code> if the value wasn't found | |||
::<code>index, value</code>: the <code>index</code> at which the match is, and its <code>value</code> | |||
Latest revision as of 18:18, 5 January 2026
Utility functions to help creating modules
def(value, default)
- Returns
valueif it is notnilor"", anddefaultotherwise
_a(arg)
- Extracts the argument from the common ways mediawiki passes arguments to modules:
- if
argis not a table, it is returned as is (Called from Lua like a function) - if
argis a table and that table has aargsfield,arg.args[1]is returned (Called from MediaWiki’s {{#invoke:}} parser function) - if
argis a table but doesn’t contain anargsfield,args[1]is returned (Called with a table as an argument list[1])
- if
date(y, m, d, short = false)
- Formats a date.
- If
shortis set totrue, uses shortened months name instead of full length month names.
find(tab, field, val)
- Locate an entry in a list of table (useful for search through json data)
- Parameters
tab: table: the list to search throughfield: any: the field in the objects to search byvalue: any: the value to match against
- Returns
nil, nilif the value wasn't foundindex, value: theindexat which the match is, and itsvalue
- ↑ Some mediawiki functions do that, idk why — Annwan