Module:Utils: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| (6 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
local _m = {} | local _m = {} | ||
local cal = require "Module:Calendar/gregorian" | |||
--- Return a if not `nil` or the empty string, or b otherwise | --- Return a if not `nil` or the empty string, or b otherwise | ||
| Line 17: | Line 18: | ||
_m.date = function(y, m, d, short) | _m.date = function(y, m, d, short) | ||
return cal.to_formatted(cal.from{year = y, month = m, day = d}, {style = short and "en-intl-short" or "en-intl"}) | |||
end | end | ||
_m. | _m.find = function(tab, field, val) | ||
for i, v in ipairs(tab) do | |||
if v[field] == val then return i, v end | |||
end | |||
return nil | |||
end | end | ||
return _m | return _m | ||
Latest revision as of 07:28, 24 May 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
local _m = {}
local cal = require "Module:Calendar/gregorian"
--- Return a if not `nil` or the empty string, or b otherwise
_m.def = function(a, b)
return (a ~= nil and a ~= "") and a or b
end
--- Extracts the argument from all the reasonable ways an argument may have been passed
_m._a = function (x)
if type(x) ~= "table" then
return x
end
-- Did we get a frame
if type(x.args) == "table" then return x.args[1] end
-- Also support passing the argument in a table
return x[1]
end
_m.date = function(y, m, d, short)
return cal.to_formatted(cal.from{year = y, month = m, day = d}, {style = short and "en-intl-short" or "en-intl"})
end
_m.find = function(tab, field, val)
for i, v in ipairs(tab) do
if v[field] == val then return i, v end
end
return nil
end
return _m