Siirry sisältöön

Moduuli:testi

Wikisanakirjasta

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:testi/ohje

local apu = require('Moduuli:Mallinetyokalut')

local m = {}

function m.yhdista(eka, toka)
	local out = {}
    
    for k,v in pairs(eka) do
		out[k] = v
    end
    for k,v in pairs(toka) do
		out[k] = v
	end

	return out
end

function m.Luettele(frame)
	local paramt
	local pframe
	
	local cur = frame
	local args = frame.args
	
	while cur and cur.args.frame == "parent" do
		pframe = cur:getParent()
		args = m.yhdista(args, pframe.args)
		cur = pframe
	end
	
	if args.taulukkoparametri then
    	local args_t = apu.numeroidut_parametrit(args, { args.taulukkoparametri })
    	paramt = args_t[args.taulukkoparametri]
    else
    	paramt = args
    end
    
    return mw.text.listToText( paramt, frame.args.erotin or ", ", frame.args.erotin or " ja ")
end

local function keys(tbl)
	local keys = {}
	local i = 1
	for k, v in pairs(tbl) do
		if v ~= "" then
			keys[i] = k
			i = i + 1
		end
	end
	
	local function cmp(a, b) 
		if type(a) == "number" and type(b) == "string" then
			return true
		elseif type(a) == "string" and type(b) == "number" then
			return false
		else
			return a < b
		end
	end
	
	table.sort(keys, cmp)
	
	return keys
end

function m.Parametrit(frame)
	local args = frame:getParent().args
    local out = {}
    local pos = 1
    local keys = keys(args)
    
    -- nimettömät parametrit
    for i, k in ipairs(keys) do
    	if tonumber(k) then
    		if tonumber(k) == i then
				out[pos] = args[k]
			else
				out[pos] = k .. "=" .. args[k]
			end
		else
			out[pos] = k .. "=" .. args[k]
		end
		pos = pos + 1
    end

    return table.concat(out, "|")
end

return m