Module:Weather box/sandbox: Difference between revisions

Content deleted Content added
m simplify
undo my edits: it's an interesting idea, but for it to really make sense, Module:Weather box/row/sandbox should use the generated table rather than looking directly at template parameters
Line 11:
end
 
local function isAny(args, suffix)
local months = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' }
local monthToNumber = require("Module:TableTools").invert(months)
for k_, vmonth in pairsipairs(argsmonths) do
 
if stripToNil(args[month .. suffix]) then
local autoSubtableMt = {
return valtrue
__index = function(self, key)
local val = {}
self[key] = val
return val
end
}
local function unAutoSubtable(t)
t.unAutoSubtable = nil
return setmetatable(t, nil)
end
 
local function makeAutoSubtabler(t)
t = t or {}
t.unAutoSubtable = unAutoSubtable
return setmetatable(t, autoSubtableMt)
end
 
-- Generate tables (arrays) for monthly values for each statistic from the args
-- table.
local generateMonthTables = require("Module:fun").memoize(function (args)
local tables = makeAutoSubtabler()
for k, v in pairs(args) do
local month, suffix = k:match("^(%u%l%l) (.+)$")
if month then
local monthNumber = monthToNumber[month]
if monthNumber then
tables[suffix][monthNumber] = stripToNil(v)
end
end
end
return tables:unAutoSubtable()
end)
 
local function isAny(args, suffix)
local monthTable = generateMonthTables(args)[suffix:gsub("^ ", "")]
return monthTable ~= nil
end