Module:Election results-STV and Module:Election results-STV/sandbox: Difference between pages

(Difference between pages)
Page 1
Page 2
imported>Gonnym
There is a large amount of code duplication written in this module which makes it more complicated to debug. Consider using for loops and smaller functions to reduce the size of the code
 
imported>Closed Limelike Curves
Accept with numbers with commas
 
Line 4: Line 4:


function p.main(frame)
function p.main(frame)
local function parsenum(x)
return parsenum(x.gsub(",", ""))
end
local args = require('Module:Arguments').getArgs(frame)
local args = require('Module:Arguments').getArgs(frame)
local index, headings, showtotal = {}, {}, {}
local index, headings, showtotal = {}, {}, {}
local cols, rounds = 0, 1
local cols, rounds = 0, 1
local valid = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
local valid = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
local invalidblank = {tonumber(args.invalidblank) or 0,}
local invalidblank = {parsenum(args.invalidblank) or 0,}
local invalid = {tonumber(args.invalid) or 0}
local invalid = {parsenum(args.invalid) or 0}
local blank = {tonumber(args.blank) or 0}
local blank = {parsenum(args.blank) or 0}
local totalvotes = {tonumber(args.totalvotes)}
local totalvotes = {parsenum(args.totalvotes)}
local electorate = {tonumber(args.electorate)}
local electorate = {parsenum(args.electorate)}
local turnout = {tonumber(args.turnout)}
local turnout = {parsenum(args.turnout)}
local row, secondrow
local row, secondrow
local tracking = ''
local tracking = ''
Line 21: Line 24:
local lang = mw.getContentLanguage()
local lang = mw.getContentLanguage()
local function fmt(n)
local function fmt(n)
return n and tonumber(n) and lang:formatNum(tonumber(n)) or nil
return n and parsenum(n) and lang:formatNum(parsenum(n)) or nil
end
end
local function pct(n, d)
local function pct(n, d)
n, d = tonumber(n), tonumber(d)
n, d = parsenum(n), parsenum(d)
if n and d and d > 0 then
if n and d and d > 0 then
return string.format('%.2f', n / d * 100)
return string.format('%.2f', n / d * 100)
Line 35: Line 38:
s = mw.ustring.gsub(s, '&[Mm][Ii][Nn][Uu][Ss];', '-')
s = mw.ustring.gsub(s, '&[Mm][Ii][Nn][Uu][Ss];', '-')
s = mw.ustring.gsub(s, '[—–−]', '-')
s = mw.ustring.gsub(s, '[—–−]', '-')
return tonumber(s) or 0
return parsenum(s) or 0
end
end
end
end
Line 95: Line 98:
table.insert(index, i)
table.insert(index, i)
if args['votes' .. i] then
if args['votes' .. i] then
if tonumber(args['votes' .. i]) then showtotal.votes = 1 end
if parsenum(args['votes' .. i]) then showtotal.votes = 1 end
local votesi = tonumber(args['votes' .. i]) or 0
local votesi = parsenum(args['votes' .. i]) or 0
args['votes' .. i] = votesi
args['votes' .. i] = votesi
valid[1] = valid[1] + votesi
valid[1] = valid[1] + votesi
Line 104: Line 107:
has_party = true
has_party = true
end
end
--[[Do something like this to reduce code duplication below
local has_votes = {}
for j = 2, 13 do
    if args['votes' .. i .. "_" .. j] then
has_votes[j] = true
end
end]]--
if args['votes' .. i .. '_2'] then
if args['votes' .. i .. '_2'] then
has_votes2 = true
has_votes2 = true
Line 152: Line 147:
end
end
if args['votes' .. i .. '_2'] then
if args['votes' .. i .. '_2'] then
if tonumber(args['votes' .. i .. '_2']) then showtotal.votes_2 = 1 end
if parsenum(args['votes' .. i .. '_2']) then showtotal.votes_2 = 1 end
local votesi = tonumber(args['votes' .. i .. '_2']) or 0
local votesi = parsenum(args['votes' .. i .. '_2']) or 0
args['votes' .. i .. '_2'] = votesi
args['votes' .. i .. '_2'] = votesi
valid[2] = valid[2] + votesi
valid[2] = valid[2] + votesi
end
end
if args['votes' .. i .. '_3'] then
if args['votes' .. i .. '_3'] then
if tonumber(args['votes' .. i .. '_3']) then showtotal.votes_3 = 1 end
if parsenum(args['votes' .. i .. '_3']) then showtotal.votes_3 = 1 end
local votesi = tonumber(args['votes' .. i .. '_3']) or 0
local votesi = parsenum(args['votes' .. i .. '_3']) or 0
args['votes' .. i .. '_3'] = votesi
args['votes' .. i .. '_3'] = votesi
valid[3] = valid[3] + votesi
valid[3] = valid[3] + votesi
end
end
if args['votes' .. i .. '_4'] then
if args['votes' .. i .. '_4'] then
if tonumber(args['votes' .. i .. '_4']) then showtotal.votes_4 = 1 end
if parsenum(args['votes' .. i .. '_4']) then showtotal.votes_4 = 1 end
local votesi = tonumber(args['votes' .. i .. '_4']) or 0
local votesi = parsenum(args['votes' .. i .. '_4']) or 0
args['votes' .. i .. '_4'] = votesi
args['votes' .. i .. '_4'] = votesi
valid[4] = valid[4] + votesi
valid[4] = valid[4] + votesi
end
end
if args['votes' .. i .. '_5'] then
if args['votes' .. i .. '_5'] then
if tonumber(args['votes' .. i .. '_5']) then showtotal.votes_5 = 1 end
if parsenum(args['votes' .. i .. '_5']) then showtotal.votes_5 = 1 end
local votesi = tonumber(args['votes' .. i .. '_5']) or 0
local votesi = parsenum(args['votes' .. i .. '_5']) or 0
args['votes' .. i .. '_5'] = votesi
args['votes' .. i .. '_5'] = votesi
valid[5] = valid[5] + votesi
valid[5] = valid[5] + votesi
end
end
if args['votes' .. i .. '_6'] then
if args['votes' .. i .. '_6'] then
if tonumber(args['votes' .. i .. '_6']) then showtotal.votes_6 = 1 end
if parsenum(args['votes' .. i .. '_6']) then showtotal.votes_6 = 1 end
local votesi = tonumber(args['votes' .. i .. '_6']) or 0
local votesi = parsenum(args['votes' .. i .. '_6']) or 0
args['votes' .. i .. '_6'] = votesi
args['votes' .. i .. '_6'] = votesi
valid[6] = valid[6] + votesi
valid[6] = valid[6] + votesi
end
end
if args['votes' .. i .. '_7'] then
if args['votes' .. i .. '_7'] then
if tonumber(args['votes' .. i .. '_7']) then showtotal.votes_7 = 1 end
if parsenum(args['votes' .. i .. '_7']) then showtotal.votes_7 = 1 end
local votesi = tonumber(args['votes' .. i .. '_7']) or 0
local votesi = parsenum(args['votes' .. i .. '_7']) or 0
args['votes' .. i .. '_7'] = votesi
args['votes' .. i .. '_7'] = votesi
valid[7] = valid[7] + votesi
valid[7] = valid[7] + votesi
end
end
if args['votes' .. i .. '_8'] then
if args['votes' .. i .. '_8'] then
if tonumber(args['votes' .. i .. '_8']) then showtotal.votes_8 = 1 end
if parsenum(args['votes' .. i .. '_8']) then showtotal.votes_8 = 1 end
local votesi = tonumber(args['votes' .. i .. '_8']) or 0
local votesi = parsenum(args['votes' .. i .. '_8']) or 0
args['votes' .. i .. '_8'] = votesi
args['votes' .. i .. '_8'] = votesi
valid[8] = valid[8] + votesi
valid[8] = valid[8] + votesi
end
end
if args['votes' .. i .. '_9'] then
if args['votes' .. i .. '_9'] then
if tonumber(args['votes' .. i .. '_9']) then showtotal.votes_9 = 1 end
if parsenum(args['votes' .. i .. '_9']) then showtotal.votes_9 = 1 end
local votesi = tonumber(args['votes' .. i .. '_9']) or 0
local votesi = parsenum(args['votes' .. i .. '_9']) or 0
args['votes' .. i .. '_9'] = votesi
args['votes' .. i .. '_9'] = votesi
valid[9] = valid[9] + votesi
valid[9] = valid[9] + votesi
end
end
if args['votes' .. i .. '_10'] then
if args['votes' .. i .. '_10'] then
if tonumber(args['votes' .. i .. '_10']) then showtotal.votes_10 = 1 end
if parsenum(args['votes' .. i .. '_10']) then showtotal.votes_10 = 1 end
local votesi = tonumber(args['votes' .. i .. '_10']) or 0
local votesi = parsenum(args['votes' .. i .. '_10']) or 0
args['votes' .. i .. '_10'] = votesi
args['votes' .. i .. '_10'] = votesi
valid[10] = valid[10] + votesi
valid[10] = valid[10] + votesi
end
end
if args['votes' .. i .. '_11'] then
if args['votes' .. i .. '_11'] then
if tonumber(args['votes' .. i .. '_11']) then showtotal.votes_11 = 1 end
if parsenum(args['votes' .. i .. '_11']) then showtotal.votes_11 = 1 end
local votesi = tonumber(args['votes' .. i .. '_11']) or 0
local votesi = parsenum(args['votes' .. i .. '_11']) or 0
args['votes' .. i .. '_11'] = votesi
args['votes' .. i .. '_11'] = votesi
valid[11] = valid[11] + votesi
valid[11] = valid[11] + votesi
end
end
if args['votes' .. i .. '_12'] then
if args['votes' .. i .. '_12'] then
if tonumber(args['votes' .. i .. '_12']) then showtotal.votes_12 = 1 end
if parsenum(args['votes' .. i .. '_12']) then showtotal.votes_12 = 1 end
local votesi = tonumber(args['votes' .. i .. '_12']) or 0
local votesi = parsenum(args['votes' .. i .. '_12']) or 0
args['votes' .. i .. '_12'] = votesi
args['votes' .. i .. '_12'] = votesi
valid[12] = valid[12] + votesi
valid[12] = valid[12] + votesi
end
end
if args['votes' .. i .. '_13'] then
if args['votes' .. i .. '_13'] then
if tonumber(args['votes' .. i .. '_13']) then showtotal.votes_13 = 1 end
if parsenum(args['votes' .. i .. '_13']) then showtotal.votes_13 = 1 end
local votesi = tonumber(args['votes' .. i .. '_13']) or 0
local votesi = parsenum(args['votes' .. i .. '_13']) or 0
args['votes' .. i .. '_13'] = votesi
args['votes' .. i .. '_13'] = votesi
valid[13] = valid[13] + votesi
valid[13] = valid[13] + votesi
Line 250: Line 245:
args['font-weight' .. i] = 'bold'
args['font-weight' .. i] = 'bold'
args['class' .. i] = 'sortbottom'
args['class' .. i] = 'sortbottom'
ovalid[1] = tonumber(args['valid']) or valid[1]
ovalid[1] = parsenum(args['valid']) or valid[1]
ovalid[2] = tonumber(args['valid2']) or valid[2]
ovalid[2] = parsenum(args['valid2']) or valid[2]
ovalid[3] = tonumber(args['valid3']) or valid[3]
ovalid[3] = parsenum(args['valid3']) or valid[3]
ovalid[4] = tonumber(args['valid4']) or valid[4]
ovalid[4] = parsenum(args['valid4']) or valid[4]
ovalid[5] = tonumber(args['valid5']) or valid[5]
ovalid[5] = parsenum(args['valid5']) or valid[5]
ovalid[6] = tonumber(args['valid6']) or valid[6]
ovalid[6] = parsenum(args['valid6']) or valid[6]
ovalid[7] = tonumber(args['valid7']) or valid[7]
ovalid[7] = parsenum(args['valid7']) or valid[7]
ovalid[8] = tonumber(args['valid8']) or valid[8]
ovalid[8] = parsenum(args['valid8']) or valid[8]
ovalid[9] = tonumber(args['valid9']) or valid[9]
ovalid[9] = parsenum(args['valid9']) or valid[9]
ovalid[10] = tonumber(args['valid10']) or valid[10]
ovalid[10] = parsenum(args['valid10']) or valid[10]
ovalid[11] = tonumber(args['valid11']) or valid[11]
ovalid[11] = parsenum(args['valid11']) or valid[11]
ovalid[12] = tonumber(args['valid12']) or valid[12]
ovalid[12] = parsenum(args['valid12']) or valid[12]
ovalid[13] = tonumber(args['valid3']) or valid[13]
ovalid[13] = parsenum(args['valid3']) or valid[13]
end
end


Line 335: Line 330:
:wikitext('First count')
:wikitext('First count')
:attr('colspan', 2)
:attr('colspan', 2)
--[[consider using a smaller function which accepts a text to handle this
local function create_row(row, text)
row
:tag('th')
:wikitext(text)
:attr('colspan', 2)
return row
end
]]
if has_votes2 then
if has_votes2 then
row
row