跳转到内容

模組:IfUtil

本页使用了标题或全文手工转换
被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由A2569875留言 | 贡献2020年2月27日 (四) 00:54 建立内容为“local p={} local lib_arg={} function p.if2number(frame) local args, working_frame if frame == mw.getCurrentFrame() then -- We're being…”的新页面)编辑。这可能和当前版本存在着巨大的差异。

(差异) ←上一修订 | 最后版本 (差异) | 下一修订→ (差异)

local p={}
local lib_arg={}

function p.if2number(frame)
	local args, working_frame
    if frame == mw.getCurrentFrame() then
        -- We're being called via #invoke. The args are passed through to the module
        -- from the template page, so use the args that were passed into the template.
        if lib_arg.getArgs == nil then lib_arg = require('Module:Arguments') end
        args = lib_arg.getArgs(frame, {
        	parentFirst=true,
        	trim = false,
			removeBlanks = false
        })
        working_frame = frame
    else
        -- We're being called from another module or from the debug console, so assume
        -- the args are passed in directly.
        args = frame
        working_frame = mw.getCurrentFrame()
        if type(args) ~= type({}) then args = {frame} end
    end
    local input_str = args['1'] or args[1] or ''
    local output_hasTwo = args['2'] or args[2] or '1'
    local output_otherwise = args['3'] or args[3] or ''
    local check_split = args['4'] or args[4] or '~'
    local local_check = mw.text.trim(require('Module:TemplateParameters').containsNumber({input_str}))
    if local_check ~= '' then
    	local split_end, split2_first = mw.ustring.find(input_str,check_split)
    	local first_num_str, second_num_str = '', ''
    	if split_end then first_num_str = mw.ustring.sub(input_str,1,split_end-1) end
    	if split2_first then second_num_str = mw.ustring.sub(input_str,split2_first+1,-1) end
    	local first_num_check, second_num_check = mw.text.trim(require('Module:TemplateParameters').containsNumber({first_num_str})),
    		mw.text.trim(require('Module:TemplateParameters').containsNumber({second_num_str}))
    	if first_num_check ~= '' and second_num_check ~= '' then
    		first_num_check, second_num_check = require('Module:TemplateParameters').getNumberValue({first_num_str}),
    			require('Module:TemplateParameters').getNumberValue({second_num_str})
    		local first_num, second_num = tonumber(first_num_check), tonumber(second_num_check)
    		if first_num and second_num then
    			if first_num ~= second_num then
    				return output_hasTwo
				else return output_otherwise end
			else return output_otherwise end
    	else return output_otherwise end
    else return output_otherwise end
end
return p