Module:Unit birthdays

-- local p = {} local h = {}

local split = require('Module:TextUtil').split local render_unit_icon = require('Module:Render/Unit')._icon local lang = mw.getContentLanguage

-- https://stackoverflow.com/a/45167276 function h.sort_on_values(t,...) local a = {...} table.sort(t, function (u,v)		for i = 1, #a do			if u[a[i]] > v[a[i]] then return false end			if u[a[i]] < v[a[i]] then return true end		end	end) end

-- DTS local monthsAbbr = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', }

local monthSearchAbbr = (function(t)	local ret = {}	for i, month in ipairs(t) do		ret[month:lower] = i	end	return ret end)(monthsAbbr)

function h.parseDate(s) if s == nil then return nil end local month, day = string.match(s, '^(%a+)%s-(%d+)$') if not month then day, month = string.match(s, '^(%d+)%s-(%a+)$') end if not month then return nil end month = monthSearchAbbr[month:sub(1,3):lower] if not month then return nil end return { month = month, day = tonumber(day), dateStr = monthsAbbr[month] .. ' ' .. day, } end

function p.main(frame) local flavors = mw.loadData('Module:Data/Unit/Flavor') local birthdays = {} for iname, flavor in pairs(flavors) do		local dateObj = h.parseDate(flavor.BIRTH) if dateObj then local month, day, dateStr if dateObj.month or dateObj.day then month = dateObj.month or 1 day = dateObj.day or 1 dateStr = dateObj.dateStr else month = 99 day = 99 dateStr = 'Unknown' end birthdays[#birthdays+1] = { month = month, day = day, dateStr = dateStr, sortKey = string.format('%02d-%02d', month, day), icon = render_unit_icon{iname, size='small'}, }		end end h.sort_on_values(birthdays, 'sortKey', 'icon')

local wikitable = mw.html.create('table'):addClass('wikitable sortable') local tr = wikitable:tag('tr') tr:tag('th'):wikitext('Date') tr:tag('th'):wikitext('Unit') for i, row in ipairs(birthdays) do		tr = wikitable:tag('tr') tr:tag('td'):attr('data-sort-value', row.sortKey):wikitext(row.dateStr) tr:tag('td'):wikitext(row.icon) end return tostring(wikitable) end

return p