Module:Election results-STV and Module:Election results-STV/sandbox: Difference between pages
(Difference between pages)
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 = { | local invalidblank = {parsenum(args.invalidblank) or 0,} | ||
local invalid = { | local invalid = {parsenum(args.invalid) or 0} | ||
local blank = { | local blank = {parsenum(args.blank) or 0} | ||
local totalvotes = { | local totalvotes = {parsenum(args.totalvotes)} | ||
local electorate = { | local electorate = {parsenum(args.electorate)} | ||
local 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 | 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 = | 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 | 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 | if parsenum(args['votes' .. i]) then showtotal.votes = 1 end | ||
local votesi = | 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 | ||
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 | if parsenum(args['votes' .. i .. '_2']) then showtotal.votes_2 = 1 end | ||
local votesi = | 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 | if parsenum(args['votes' .. i .. '_3']) then showtotal.votes_3 = 1 end | ||
local votesi = | 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 | if parsenum(args['votes' .. i .. '_4']) then showtotal.votes_4 = 1 end | ||
local votesi = | 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 | if parsenum(args['votes' .. i .. '_5']) then showtotal.votes_5 = 1 end | ||
local votesi = | 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 | if parsenum(args['votes' .. i .. '_6']) then showtotal.votes_6 = 1 end | ||
local votesi = | 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 | if parsenum(args['votes' .. i .. '_7']) then showtotal.votes_7 = 1 end | ||
local votesi = | 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 | if parsenum(args['votes' .. i .. '_8']) then showtotal.votes_8 = 1 end | ||
local votesi = | 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 | if parsenum(args['votes' .. i .. '_9']) then showtotal.votes_9 = 1 end | ||
local votesi = | 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 | if parsenum(args['votes' .. i .. '_10']) then showtotal.votes_10 = 1 end | ||
local votesi = | 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 | if parsenum(args['votes' .. i .. '_11']) then showtotal.votes_11 = 1 end | ||
local votesi = | 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 | if parsenum(args['votes' .. i .. '_12']) then showtotal.votes_12 = 1 end | ||
local votesi = | 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 | if parsenum(args['votes' .. i .. '_13']) then showtotal.votes_13 = 1 end | ||
local votesi = | 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] = | ovalid[1] = parsenum(args['valid']) or valid[1] | ||
ovalid[2] = | ovalid[2] = parsenum(args['valid2']) or valid[2] | ||
ovalid[3] = | ovalid[3] = parsenum(args['valid3']) or valid[3] | ||
ovalid[4] = | ovalid[4] = parsenum(args['valid4']) or valid[4] | ||
ovalid[5] = | ovalid[5] = parsenum(args['valid5']) or valid[5] | ||
ovalid[6] = | ovalid[6] = parsenum(args['valid6']) or valid[6] | ||
ovalid[7] = | ovalid[7] = parsenum(args['valid7']) or valid[7] | ||
ovalid[8] = | ovalid[8] = parsenum(args['valid8']) or valid[8] | ||
ovalid[9] = | ovalid[9] = parsenum(args['valid9']) or valid[9] | ||
ovalid[10] = | ovalid[10] = parsenum(args['valid10']) or valid[10] | ||
ovalid[11] = | ovalid[11] = parsenum(args['valid11']) or valid[11] | ||
ovalid[12] = | ovalid[12] = parsenum(args['valid12']) or valid[12] | ||
ovalid[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) | ||
if has_votes2 then | if has_votes2 then | ||
row | row |