Module:Util: Difference between revisions

From Path of Exile 2 Wiki
Jump to navigation Jump to search
>OmegaK2
mNo edit summary
>OmegaK2
m (changed arg to value for function find_in_nested_array)
Line 104: Line 104:
     --
     --
     -- args: Table containing:
     -- args: Table containing:
     --  arg: value of the argument
     --  value: value of the argument
     --  tbl: table of valid options
     --  tbl: table of valid options
     --  key: key or table of key of in tbl
     --  key: key or table of key of in tbl
Line 115: Line 115:
         for _, item in ipairs(args.tbl) do
         for _, item in ipairs(args.tbl) do
             for _, k in ipairs(args.key) do
             for _, k in ipairs(args.key) do
                 if item[k] == args.arg then
                 if item[k] == args.value then
                     rtr = item
                     rtr = item
                     break
                     break
Line 123: Line 123:
     elseif args.key == nil then
     elseif args.key == nil then
         for _, item in ipairs(args.tbl) do
         for _, item in ipairs(args.tbl) do
             if item == args.arg then
             if item == args.value then
                 rtr = item
                 rtr = item
                 break
                 break
Line 130: Line 130:
     else
     else
         for _, item in ipairs(args.tbl) do
         for _, item in ipairs(args.tbl) do
             if item[args.key] == args.arg then
             if item[args.key] == args.value then
                 rtr = item
                 rtr = item
                 break
                 break
Line 142: Line 142:
      
      
     if args.rtrvalue or args.rtrvalue == nil then
     if args.rtrvalue or args.rtrvalue == nil then
         return args.arg
         return args.value
     end
     end
      
      

Revision as of 12:30, 26 September 2015

Module documentation[view] [edit] [history] [purge]


This is a meta module.

This module is meant to be used only by other modules. It should not be invoked in wikitext.

Lua logo

This module depends on the following other modules:

Overview

Provides utility functions for programming modules.

Structure

Group Description
util.cast utilities for casting values (i.e. from arguments)
util.html shorthand functions for creating some html tags
util.misc miscellaneous functions

Usage

This module should be loaded with require().

-- Utility stuff

local util = {}

util.cast = {}
util.cast.bool_false = {'false', '0', 'disabled', 'off', 'no', ''}

function util.cast.boolean(value)
    -- Takes an abitary value and casts it to a bool value
    -- 
    -- for strings false will be according to util.cast.bool_false
    local t = type(value)
    if t == 'nil' then
        return false
    elseif t == 'boolean' then
        return value
    elseif t == 'number' then
        if value == 0 then return false end
        return true
    elseif t == 'string' then
        local tmp = string.lower(value)
        for _, v in ipairs(util.cast.bool_false) do
            if v == tmp then
                return false
            end
        end
        return true
    else
        error('"' .. value .. ' is of uncaptured type "' .. t .. '"')
    end
    
end

function util.cast.number(value, args)
    -- Takes an abitary value and attempts to cast it to int
    --
    -- args:
    --  default: for strings, if default is nil and the conversion fails, an error will be returned
    --  min: error if <min
    --  max: error if >max
    if args == nil then
        args = {}
    end
    
    local t = type(value)
    local val
    
    if t == 'nil' then
        val = nil
    elseif t == 'boolean' then
        if value then
            val = 1
        else
            val = 0
        end
    elseif t == 'number' then
        val = value
    elseif t == 'string' then
        val = tonumber(value)
    end
    
    if val == nil then
        if args.default ~= nil then
            val = args.default
        else
            error('"' .. value .. '" of type "' .. t .. '" is not an integer')
        end
    end
    
    if args.min ~= nil and val < args.min then
        error('"' .. val .. '" is too small. Minimum: "' .. args.min .. '"')
    end
    
    if args.max ~= nil and val > args.max then
        error('"' .. val .. '" is too large. Maximum: "' .. args.max .. '"')
    end
    
    return val
end

util.html = {}
function util.html.abbr(abbr, text, class)
    tag = mw.html.create('abbr')
    tag
        :attr('title', text or '')
        :attr('class', class or '')
        :wikitext(abbr or ' ')
        :done()
        
    return tostring(tag)
end

util.table = {}
function util.table.find_in_nested_array(args) 
    -- Iterates thoguh the given nested array and finds the given value
    -- 
    -- ex.   
    -- data = {
    -- {a=5}, {a=6}}
    -- find_nested_array{arg=6, tbl=data, key='a'} -> 6
    -- find_nested_array(arg=10, tbl=data, key='a'} -> nil
    -- -> returns "6"
    
    --
    -- args: Table containing:
    --  value: value of the argument
    --  tbl: table of valid options
    --  key: key or table of key of in tbl
    --  rtrkey: if key is table, return this key instead of the value instead
    --  rtrvalue: default: true
    
    local rtr
    
    if type(args.key) == 'table' then
        for _, item in ipairs(args.tbl) do
            for _, k in ipairs(args.key) do
                if item[k] == args.value then
                    rtr = item
                    break
                end
            end
        end
    elseif args.key == nil then
        for _, item in ipairs(args.tbl) do
            if item == args.value then
                rtr = item
                break
            end
        end
    else
        for _, item in ipairs(args.tbl) do
            if item[args.key] == args.value then
                rtr = item
                break
            end
        end
    end
    
    if rtr == nil then
        return rtr
    end
    
    if args.rtrvalue or args.rtrvalue == nil then
        return args.value
    end
    
    if args.rtrkey ~= nil then 
        return rtr[args.rtrkey]
    else
        return rtr
    end
end

util.misc = {}
function util.misc.get_frame(frame)
    if frame == nil or type(frame) == 'table' then
        frame = mw.getCurrentFrame()
    end
    return frame
end

function util.misc.add_category(categories, namespace)
    if type(categories) == 'string' then
        categories = {categories}
    end
    
    
    local title = mw.title.getCurrentTitle()
    local sub_blacklist = {
        doc = true,
        sandbox = true,
        sandbox2 = true,
        testcases = true,
    }
    
    if namespace ~= nil and title.namespace ~= namespace then
        return ''
    end
    
    if sub_blacklist[title.subpageText] then
        return ''
    end
   
    local cats = {}
    
    for i, cat in ipairs(categories) do
        cats[i] = string.format('[[Category:%s]]', cat)
    end
    return table.concat(cats)
end

return util