Module:Util

From Path of Exile 2 Wiki
Revision as of 14:08, 9 July 2015 by >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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
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 = {cast={}}
util.cast = {}
util.cast.bool_false = {'false', '0', 'disabled', 'off', 'no', ''}

function util.cast.bool(str)
    -- Takes a string and casts it to a bool value
    -- False will be according to util.cast.bool_false
    if str == nil then
        return false
    end
    local tmp = string.lower(str)
    for _, v in ipairs(util.cast.bool_false) do
        if v == tmp then
            return false
        end
    end
    return true
end

function util.cast.int(str, default)
    -- Attempts to cast a string to int
    -- If default is nil and the conversion fails, an error will be returned
    if string.match(str, '^%d+$') then
        return tonumber(str)
    elseif default ~= nil then
        return default
    else
        error('"' .. str .. '" is not an integer')
    end
end

return util