Module:Sandbox/Premeditated/Nobel

local wd = require('Module:Wd')

local p = {}

local function comma_to_dots(x) if x then res, _ = string.gsub(x, "%,", ".") return tonumber(res) or x	end return '' end

local function find(f, l) for _, v in pairs(l) do    if v == f then return true end end return false end

local function get_unit(result) local allowed_lang = {"nb", "nn", "en"} local lang = {}

for index, type_unit in pairs(mw.wikibase.getBestStatements( result, 'P5061' )) do   	if find(type_unit["mainsnak"]["datavalue"]["value"]["language"], allowed_lang) then lang[type_unit["mainsnak"]["datavalue"]["value"]["language"]] = type_unit["mainsnak"]["datavalue"]["value"]["text"] end end

if lang["nb"] then return lang["nb"] elseif lang["nn"] then return lang["nn"] elseif lang["en"] then return lang["en"] else return mw.wikibase.getLabel( result ) end end

local function round(num, numDecimalPlaces) return tonumber(string.format("%." .. (numDecimalPlaces or 0) .. "f", num)) end

local function find_population_density(popu, area, item_p) local population_density = round(popu/area, 2) local type_SI if item_p then claims = mw.wikibase.getBestStatements( item_p, 'P2046' ) result = claims[1].mainsnak.datavalue.value.unit result = mw.ustring.sub(result, mw.ustring.find(result, "Q"), -1) type_SI =  .. get_unit(result) ..  else type_SI = "km²" end return mw.language.getContentLanguage:formatNum(population_density) .. " innb./" .. type_SI end

local function wd_match(item_p, pid, last_val) if wd._property{item_p, pid} ~= "" then if pid == "P2046" then return find_population_density(last_val, tonumber(wd._property{'raw','normal+', item_p, 'P2046'}), item_p) elseif pid == "P1082" then return find_population_density(tonumber(wd._property{'raw','normal+', item_p, 'P1082'}), last_val) end end if wd._property{item_p,'P1082'} ~= "" and wd._property{item_p,'P2046'} ~= "" then return find_population_density(tonumber(wd._property{'raw','normal+', item_p, 'P1082'}), tonumber(wd._property{'raw','normal+', item_p, 'P2046'}), item_p) end return '' end

function p.main(frame) local area = comma_to_dots(frame.args[1]) --P2046 local population = comma_to_dots(frame.args[2]) --P1082 local population_density = comma_to_dots(frame.args[3]) local item_p = mw.wikibase.getEntityIdForCurrentPage local item_p = "Q55" if population_density == "uten" then return '' end if area ==  and population ==  and population_density == '' then return wd_match(item_p) end if population_density ~= '' then if type(population_density) == "number" then return mw.language.getContentLanguage:formatNum(population_density) .. " innb./km²" else return population_density .. " innb./km²" end end if type(area) == "number" and type(population) == "number" then return find_population_density(population, area) end if type(area) == "number" and population == '' then return wd_match(item_p, 'P1082', area) end if type(population) == "number" and area == '' then return wd_match(item_p, 'P2046', population) end return "" end

return p