Content deleted Content added
Allow negative values to indicate nodata |
Fixed off-by-one error |
||
(14 intermediate revisions by 4 users not shown) | |||
Line 1:
--- Example usage:
--- {{#invoke
--- =p.
local p = {}
Line 7:
local english = mw.getLanguage("en")
local function round(x)
return (math.modf(x + (x < 0 and -0.5 or 0.5)))
end
local function formatChange(previous, current)
if not previous or previous == 0 then
return
Line 21:
local change = current / previous * 100 - 100
local sign = change < 0 and "−" or "+"
return mw.ustring.format("%s%s%%", sign, lang:formatNum(round(math.abs(change))))▼
if (math.abs(change)) >= 10 then
return mw.ustring.format("%s%.0f%%", sign, (math.abs(change)))
end
if (math.abs(change)) < 1 then
return mw.ustring.format("%s%.3f%%", sign, (math.abs(change)))
end
end
function p.
local data = mw.ext.data.get(
local dateIndex
Line 34 ⟶ 41:
local class5Index
for i, field in ipairs(data.schema.fields) do
if field.name == "date" or field.name ==
dateIndex = i
elseif field.name == "deaths" or field.name ==
deathsIndex = i
elseif field.name == "recoveries" or field.name ==
recoveriesIndex = i
elseif field.name == "cases" or field.name ==
casesIndex = i
elseif field.name == "class4" or field.name ==
class4Index = i
elseif field.name == "class5" or field.name ==
class5Index = i
end
end
assert(dateIndex, "Date field not found.")
assert(deathsIndex or not
assert(recoveriesIndex or not
assert(casesIndex or not
assert(class4Index or not
assert(class5Index or not
-- Restructure the data as tables with keys.
Line 69 ⟶ 76:
}
local prevRecord = records[#records] or {}
if casesIndex and not prevRecord.cases and record.cases and record.cases > 0 then
record.options.firstright1 = "y"
end
if deathsIndex and prevRecord.deaths == 0 and record.deaths and record.deaths > 0 then
record.options.firstright2 = "y"
end
if deathsIndex and (prevRecord.deaths or prevRecord.assumedDeaths) and not record.deaths then
record.assumedDeaths = prevRecord.deaths or prevRecord.assumedDeaths
end
if casesIndex and (prevRecord.cases or prevRecord.assumedCases) and not record.cases then
record.assumedCases = prevRecord.cases or prevRecord.assumedCases
end
if record.deaths == prevRecord.deaths
Line 88 ⟶ 101:
for i = #records, 1, -1 do
local record = records[i]
if i < #records and record.streak > 3 then
for j = i, i - record.streak + 3, -1 do
table.remove(records, j)
Line 106 ⟶ 119:
local row = {
record.date or "",
tostring(record.deaths
tostring(
tostring(record.cases
tostring(
tostring(
record.cases
record.cases
record.deaths
record.deaths
}
for k, v in pairs(record.options) do
Line 124 ⟶ 137:
end
function p.externalData(frame)
local args = {}
for k,v in pairs(frame.args) do
if (v or '') ~= '' then
args['data'..k] = v
end
end
return p._externalData(args)
end
return p
|