模块:Str test

来自吾萌百科
星河讨论 | 贡献2023年8月26日 (六) 15:15的版本 (创建页面,内容为“local module = {} function module.main(frame) local args = frame.args local str = args[1] local ptn = mw.text.decode(mw.text.unstripNoWiki(args[2])) if mw.ustring.find(str, ptn) == nil then return '' else return 1 end end function module.MultiMatch(frame) local parent = frame:getParent() if parent and parent:getTitle() == "Template:Str substr" then frame = parent end local args = frame.args local str = args[1] local result = "" local i = 2 wh…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

可在模块:Str test/doc创建此模块的帮助文档

local module = {}

function module.main(frame)
	local args = frame.args
	local str = args[1]
	local ptn = mw.text.decode(mw.text.unstripNoWiki(args[2]))
	if mw.ustring.find(str, ptn) == nil 
	then return ''
	else return 1 end
end

function module.MultiMatch(frame)
	local parent = frame:getParent()
	if parent and parent:getTitle() == "Template:Str substr" then
		frame = parent
	end
	local args = frame.args
	local str = args[1]
	local result = ""
	local i = 2
	while(frame.args[i])do
		local ptn = mw.text.decode(mw.text.unstripNoWiki(args[i]))
		local found = mw.ustring.match(str, ptn)
		result = result .. (found and "1" or "0")
		i = i+1
	end
	return result
end

function module.Switch(frame)
	local parent = frame:getParent()
	if parent and parent:getTitle() == "Template:Str switch" then
		frame = parent
	end
	local str = frame.args[1]
	for key, value in pairs(frame.args) do
		if type(key) == "string" and key ~= "#default" then
			local ptn = mw.text.decode(mw.text.trim(mw.text.unstripNoWiki(value)))
			if ptn ~= "" and mw.ustring.match(str, ptn) then
				return key
			end
		end
	end
	return frame.args["#default"] or ""
end

return module