Module:Medical cases chart/data: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
Line 4:
local p = {}
local lang = mw.getContentLanguage()
local english = mw.getLanguage("en")
 
function round(x)
Line 53 ⟶ 54:
assert(class5Index or not frame.args.class5, "Class 5 field not found.")
-- Restructure the data as tables with keys.
local records = {}
for i, recordrow in ipairs(data.data) do
local prevRecordrecord = {}
table.insert(records, {
date = recordrow[dateIndex],
deaths = deathsIndex and recordrow[deathsIndex],
recoveries = recoveriesIndex and recordrow[recoveriesIndex],
cases = casesIndex and recordrow[casesIndex],
class4 = class4Index and recordrow[class4Index],
class5 = class5Index and recordrow[class5Index],
options = {},
})
streak = 1,
})
local prevRecord = records[#records] or {}
if casesIndex and not prevRecord.cases and record.cases > 0 then
record.options.firstright1 = "y"
end
if deathsIndex and prevRecord.deaths == 0 and record.deaths > 0 then
record.options.firstright2 = "y"
end
if record.deaths == prevRecord.deaths
and record.recoveries == prevRecord.recoveries
and record.cases == prevRecord.cases
and record.class4 == prevRecord.class4
and record.class5 == prevRecord.class5 then
record.streak = prevRecord.streak + 1
end
table.insert(records, {record)
end
-- Collapse streaks of identical data.
for i = #records, 1, -1 do
local record = records[i]
if record.streak > 3 then
for j = i, i - record.streak + 3, -1 do
table.remove(records, j)
end
i = i - record.streak + 2
record = records[i]
record.options.collapse = "y"
record.options.id = english:formatDate("M", record.date):lower()
record.date = nil
end
end
-- Stringify the data.
local rows = {}
local prevRecord = {}
for i, record in ipairs(records) do
local prevRecord = records[i - 1] or {}
local row = {
record.date or "",
tostring(record.deaths or ""),
tostring(record.recoveries or ""),
Line 75 ⟶ 110:
tostring(record.class4 or ""),
tostring(record.class5 or ""),
record.cases > 0 and lang:formatNum(record.cases) or "",
record.cases > 0 and formatChange(prevRecord.cases, record.cases) or "",
record.deaths > 0 and lang:formatNum(record.deaths) or "",
record.deaths > 0 and formatChange(prevRecord.deaths, record.deaths) or "",
}
for k, v in pairs(record.options) do
if casesIndex and not prevRecord.cases and record.cases > 0 then
table.insert(row, string.format("firstright1%s=y%s", k, v))
end
if deathsIndex and prevRecord.deaths == 0 and record.deaths > 0 then
table.insert(row, "firstright2=y")
end
table.insert(rows, table.concat(row, ";"))
prevRecord = record
end
return table.concat(rows, "\n")