Module:LoadData

local p = {}

-- Finds the next key key <= or >= the given i.
-- operator is ±1
local function findItemRange(data, i, operator)
    local bestIndex = nil
    i = i * operator
    for k, v in pairs(data) do
    	local kop = type(k) == 'number' and k * operator
        if kop and kop <= i and (bestIndex == nil or kop > bestIndex * operator) then
        	bestIndex = k
        end
    end
    if bestIndex then return data[bestIndex] else return nil end
end

local function load(datamodule, frame)
	local args = frame.args
    local data = mw.loadData(datamodule)
    for i = 1, 20 do
        if args[i] then data = data[tonumber(args[i]) or args[i]]
        elseif args[i .. ' lteq'] then
            data = findItemRange(data, tonumber(args[i .. ' lteq']), 1)
        elseif args[i .. ' gteq'] then
            data = findItemRange(data, tonumber(args[i .. ' gteq']), -1)
        else break end
    end
    
    if data == nil then
    	return args['if_nil'] -- not a required argument, OK to return nil here.
    end
    
    if type(data) == 'table' then
    	-- Put the table into another table because the return value of loadData
    	-- is a "fake" table that only has certain metamethods.
    	local realdata = {}
    	for k, v in pairs(data) do
    		realdata[k] = v
    	end
    	data = realdata
    else
    	data = { data }
    end
    
    if args['template'] then
    	return mw.text.unstripNoWiki(args['template']):format(unpack(data))
    elseif args['preprocess'] then
    	return frame:preprocess(mw.text.unstripNoWiki(args['preprocess']):format(unpack(data)))
    else
    	return table.concat(data)
    end
end

return setmetatable({}, {
	__index = function(t, k)
		return function(frame)
			return load('Module:' .. k, frame)
    	end
	end
})

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.