Module:TypesLib

From The Remnant 2 Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:TypesLib/doc

-- This library allows conversion between types. Template args are always received as a string, making conversion necessary in some cases.
-- Add additional conversions as needed
local cfg = mw.loadData('Module:TypesLib/config')
local i18n = cfg.i18n

local lib = {}

-- Deprecated, use lib.bool instead
function lib.string_to_bool(val)
	return val == 'true' and val ~= 'false'
end

function lib.to_bool(val, cast_nil)
    local t = type(val)
    if (t == 'string') then
    	local _v = string.lower(val)
    	for _, v in ipairs(i18n.false_values) do
    		if v == _v then return false end
    	end
    	return true
    elseif (t == 'number') then
    	if val == 0 then return false end
    	return true
    elseif (t == 'boolean') then
    	return val
    elseif (t == 'nil') then
    	if cast_nil == nil or cast_nil == true then return false end
    else
    	error(string.format(i18n.err.invalid_boolean, tostring(val)))
    end
end

return lib