Kontent qismiga oʻtish

Modul:Range

Vikipediya, ochiq ensiklopediya

}}


local function ranges( args )
    local value
    
    function inrange( v )
        if tonumber( v ) then return tonumber( v ) == value end -- simple number - just compare
        local lp, a, b, rp = v:match( '^([([]?)(.-)%.%.(.-)([)%]]?)$' ) -- a range
        local na, nb = tonumber( a ), tonumber( b )
        if  a ~= '' and not na -- both a and b must be either empty or a valid number
            or b ~= '' and not nb 
            or not ( na or nb ) -- at least one of na, nb must be a number
        then 
            error(string.format( 'Bad parameter calling Range:iswitch: "%s" is not a number or valid range', v ), 0 )
            return false
        end 
        
        local llok = not na or na < value or lp ~= '(' and na == value 
        -- left-limit-ok: no ll or ll < value or ll == value and not open range
            
        local rlok = not nb or value < nb or rp ~= ')' and value == nb 
        -- right-limit-ok: no rl or value < rl or value == rl and not open range
        
        return llok and rlok
    end

    function match( s )
        if type( s ) == 'number' then return s == value end
        local res = false
        s:gsub("%S+", function( c ) res = res or inrange( c ) end)
        return res
    end
    
    value = tonumber( args.value )
    if not value then error('Range:iswitch: should have a numeric "value" parameter', 0) end
    for k, v in pairs( args ) do
        if k ~= 'value' and k~= 'default' and match( k ) then return v end
    end
    return args['default'] or ''
end

return { iswitch = function( frame ) return ranges( frame.args ) end, }