Mô đun:ParameterAliasNormalizer

local ModuleObj = {}
local getArgs = require("Module:Arguments").getArgs

--[[ 
   Hàm dùng để chuẩn bị dữ liệu
--]]
function ModuleObj.main(frame)
   local moduleArgs = getArgs(frame, {frameOnly = true})
   local callerArgs = getArgs(frame, {parentOnly = true})
   return ModuleObj._main(frame, moduleArgs, callerArgs)
end

--[[ 
   Chuẩn hóa tên của tham số về tên gốc
--]]
function ModuleObj._main(frame, moduleArgs, callerArgs)
    -- Replace dấu dollar thành số nếu có
    replaceDollar(moduleArgs)
    
    -- Lowercase hóa key của bảng callerArgs
    callerArgs = lowerKey(callerArgs)

    -- Hàm kiểm tra chuỗi có rỗng không
	local function isEmpty(s)
	  return s == nil or mw.text.trim(s) == ''
    end

    -- Lấy tên bản mẫu gốc
	local templateName = moduleArgs.__template
    moduleArgs.__template = nil;

    -- Lấy giá trị của đối số ánh xạ gán cho đối số gốc
	for baseParam, aliasesStr in pairs(moduleArgs) do
		for alias in mw.text.gsplit(aliasesStr, '\\', true) do
			alias = mw.ustring.lower(mw.text.trim(alias))
			if callerArgs[alias] ~= nil then
	            local value = callerArgs[alias]
	            if isEmpty(value) == false then
	                callerArgs[baseParam] = value
	            end
            	callerArgs[alias] = nil
            end
		end
    end
    
    -- Tạo bản mẫu với bảng đối số callerArgs
	local content = frame:expandTemplate{ title = templateName, args = callerArgs }
	return content
end

--[[
    Hàm lowercase hóa key của các pair trong một table
--]]
function lowerKey(table)
    local lowercased = {}
    for key, value in pairs(table) do
        lowercased[mw.ustring.lower(key)] = value
    end
    return lowercased
end

--[[
	Hàm thay dấu dollar thành số
	Ví dụ: "combatant${1,3} = bên $" sẽ sinh ra 3 đối số:
	"combatant1 = bên 1", "combatant2 = bên 2", "combatant3 = bên 3"
--]]
function replaceDollar(table)
	local temp = {}
	
	-- Tìm các key có pattern phù hợp, sinh các cặp key-value tương ứng
	-- và xóa cặp key-value ban đầu
	for key, value in pairs(table) do
		if (mw.ustring.match(key, "${(%d+,%d+)}")) then
			local points = mw.text.split(mw.ustring.match(key, "${(%d+,%d+)}"), ",", true)
			for i = points[1], points[2], 1 do
				local k = mw.ustring.gsub(key, "(${%d+,%d+})", i)
				local v = mw.ustring.gsub(value, "%$", i)
				temp[k] = v
			end
			table[key] = nil
		end
	end
	
	for key, value in pairs(temp) do
		table[key] = value
	end
end

return ModuleObj
Chúng tôi bán
Bài viết liên quan
So sánh Hutao và Childe - Genshin Impact
So sánh Hutao và Childe - Genshin Impact
Bài viết có thể rất dài, nhưng phân tích chi tiết, ở đây tôi muốn so sánh 𝐠𝐢𝐚́ 𝐭𝐫𝐢̣ của 2 nhân vật mang lại thay vì tập trung vào sức mạnh của chúng
Tìm hiểu về Chainsaw Man anime trước khi xem
Tìm hiểu về Chainsaw Man anime trước khi xem
Câu chuyện lấy bối cảnh ở một thế giới giả tưởng nơi tồn tại những con quái vật được gọi là ác quỷ, và thế giới này đang phải chịu sự tàn phá của chúng.
Fun Fact về Keqing - Genshin Impact
Fun Fact về Keqing - Genshin Impact
Keqing có làn da trắng và đôi mắt màu thạch anh tím sẫm, với đồng tử hình bầu dục giống con mèo với những dấu hình kim cương trên mống mắt
1-In-60 Rule: Quy Luật Giúp Bạn Luôn Tập Trung Vào Mục Tiêu Của Mình
1-In-60 Rule: Quy Luật Giúp Bạn Luôn Tập Trung Vào Mục Tiêu Của Mình
Quy luật "1-In-60 Rule" có nguồn gốc từ ngành hàng không.