Theme: iWiki Log in Register

Diff: Module:GetParameters

Comparing revision #1 (2021-10-07 13:22:34) with revision #2 (2023-02-04 10:26:35).

OldNew
local p = {}
local p = {}
--[[
--[[
Helper function that populates the argument list given that user may need to use a mix of
Helper function that populates the argument list given that user may need to use a mix of
named and unnamed parameters.  This is relevant because named parameters are not
named and unnamed parameters.  This is relevant because named parameters are not
identical to unnamed parameters due to string trimming, and when dealing with strings
identical to unnamed parameters due to string trimming, and when dealing with strings
we sometimes want to either preserve or remove that whitespace depending on the application.
we sometimes want to either preserve or remove that whitespace depending on the application.
]]
]]
function p.getParameters( frame_args, arg_list )
function p.getParameters( frame_args, arg_list )
    local new_args = {};
    local new_args = {};
    local index = 1;
    local index = 1;
    local value;
    local value;
    
    
    for i,arg in ipairs( arg_list ) do
    for i,arg in ipairs( arg_list ) do
        value = frame_args[arg]
        value = frame_args[arg]
        if value == nil then
        if value == nil then
            value = frame_args[index];
            value = frame_args[index];
            index = index + 1;
            index = index + 1;
        end
        end
        new_args[arg] = value;
        new_args[arg] = value;
    end
    end
    
    
    return new_args;
    return new_args;
end        
end        
--[[
--[[
Helper Function to interpret boolean strings
Helper Function to interpret boolean strings
]]
]]
function p.getBoolean( boolean_str )
function p.getBoolean( boolean_str )
    local boolean_value;
    local boolean_value;
    
    
    if type( boolean_str ) == 'string' then
    if type( boolean_str ) == 'string' then
        boolean_str = boolean_str:lower();
        boolean_str = boolean_str:lower();
        if boolean_str == 'false' or boolean_str == 'no' or boolean_str == '0' 
        if boolean_str == 'false' or boolean_str == 'no' or boolean_str == '0' 
                or boolean_str == '' then
                or boolean_str == '' then
            boolean_value = false;
            boolean_value = false;
        else
        else
            boolean_value = true;
            boolean_value = true;
        end    
        end    
    elseif type( boolean_str ) == 'boolean' then
    elseif type( boolean_str ) == 'boolean' then
        boolean_value = boolean_str;
        boolean_value = boolean_str;
    else
    else
        error( 'No boolean value found' );
        error( 'No boolean value found' );
    end    
    end    
    return boolean_value
    return boolean_value
end
end
function p.defined(frame)
function p.defined(frame)
	local arg = mw.text.trim(frame.args[1])
	local arg = mw.text.trim(frame.args[1])
	--if arg == tostring(tonumber(arg)) then -- undesired result for '-0'
	--if arg == tostring(tonumber(arg)) then -- undesired result for '-0'
	--	arg = tonumber(arg)
	--	arg = tonumber(arg)
	--end
	--end
	--if mw.ustring.find(arg, '^%s*-?[1-9][0-9]*%s*$') ~= nil or arg == '0' then
	--if mw.ustring.find(arg, '^%s*-?[1-9][0-9]*%s*$') ~= nil or arg == '0' then
	--	arg = tonumber(arg)
	--	arg = tonumber(arg)
	--end
	--end
	if mw.ustring.find(arg, '^-?[1-9][0-9]*$') ~= nil then
	if mw.ustring.find(arg, '^-?[1-9][0-9]*$') ~= nil then
		arg = tonumber(arg)
		arg = tonumber(arg)
	elseif arg == '0' then
	elseif arg == '0' then
		arg = 0
		arg = 0
	end
	end
	return frame:getParent().args[arg] ~= nil
	return frame:getParent().args[arg] ~= nil
end
end
return p
return p