Module:Util: Difference between revisions

From Path of Exile Wiki
Jump to navigation Jump to search
>OmegaK2
(Created page with "-- Utility stuff local util = {cast={}} util.cast = {} util.cast.bool_false = {'false', '0', 'disabled', 'off', 'no', ''} function util.cast.bool(str) -- Takes a string...")
 
>OmegaK2
(renamed the functions and made them take abitary values)
Line 1: Line 1:
-- Utility stuff
-- Utility stuff


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


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


function util.cast.int(str, default)
function util.cast.number(value, default)
     -- Attempts to cast a string to int
     -- Takes an abitary value and attempts to cast it to int
     -- If default is nil and the conversion fails, an error will be returned
     --
     if string.match(str, '^%d+$') then
    -- For strings: if default is nil and the conversion fails, an error will be returned
         return tonumber(str)
     local t = type(value)
    elseif default ~= nil then
   
         return default
    if t == 'nil' then
        return 0
    elseif t == 'boolean' then
        if value then
            return 1
        else
            return 0
        end
    elseif t == 'number' then
         return value
    elseif t == 'string' then
        val = tonumber(value)
        if val == nil then
            if default ~= nil then
                return default
            else
                error('"' .. value .. '" is not an integer')
            end
         else
            return val
        end
     else
     else
         error('"' .. str .. '" is not an integer')
         error('"' .. value .. ' is of uncaptured type "' .. t .. '"')
     end
     end
end
end


return util
return util

Revision as of 14:44, 9 July 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().

ru:Модуль:Util

-- 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 t == 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, default)
    -- Takes an abitary value and attempts to cast it to int
    --
    -- For strings: if default is nil and the conversion fails, an error will be returned
    local t = type(value)
    
    if t == 'nil' then
        return 0
    elseif t == 'boolean' then
        if value then
            return 1
        else
            return 0
        end
    elseif t == 'number' then
        return value
    elseif t == 'string' then
        val = tonumber(value)
        if val == nil then
            if default ~= nil then
                return default
            else
                error('"' .. value .. '" is not an integer')
            end
        else
            return val
        end
    else
        error('"' .. value .. ' is of uncaptured type "' .. t .. '"')
    end
end

return util