hypixel skyblock

This module is a module used to convert types into other types with metamethod support for the module's methods.

Loading the module

To load this module and make its methods available for use, Add this line of code to the start of your module:

require('Module:Constructor')

This module does not need to be loaded under a table to be used. Once the module is loaded, any methods described below will be available as global variables in the loaded module.

Alternatively, you can use this module using the code below by using Module:LoadLib. Note that if you use this function, you do not need to add the module to the loader query because this module is implicitly loaded by the function.

local loadLib = require('Module:LoadLib')

_G = loadLib(_G, {
    { 'Module:Constructor' },
    -- Place any other modules to load in here...
})

Depending on the settings you used in loadLib(), the methods of this module may be available under their respective variables in the module this module was loaded in.

Methods

All methods are available below as fields under the required object name.

String

String(<value>(any))
This Method returns <value> converted to a string value when it is called. It will respect any __tostring metamethods and will use that metamethod before trying to convert is value to a string (see #Metamethods for an in-depth metamethod explanation).

  • This method Also has contains all methods from Module:String as fields on this method. (this includes native functions like string.gsub()).

Examples

  • String({ "foo" })"table"
  • String(true)"true"
  • String()""

Number

Number(<value>(any))
This Method returns <value> converted to a number value when the method is called. It will respect any __tonumber metamethods and will use that metamethod before trying to convert is value to a string (see #Metamethods for an in-depth metamethod explanation).

  • This method also has contains all methods from the lua math library as fields on this class. (for example, math.max()).
  • If <value> is a string or table, it will return the length of that value (for a table, it will use table.length().

Examples

  • Number("FooBar")6
  • Number(true)1
  • Number()0
  • Number(function() end)NaN

Table

Table(<...keys>(any))
This Class returns each value of <...keys> packed into a table similar to table.pack(). If the length of the arguments is less than 2, and the value has a __totable metafield, it will use that metafield on the table (see #Metamethods for an in-depth metamethod explanation).

  • This method Also has all methods from Module:Table as fields on this class. (this includes native functions like table.concat()).

Examples

  • Table("foo", "bar"){ "foo", "bar" }
  • Table(true){ true }
  • Table(){}

Boolean

Boolean(<value>(any)<nilDefault>(any))
This Class returns <value> converted to a boolean value when it's constructor is called. It will respect any __toboolean metamethods and will use that metamethod before trying to convert is value to a string (see #Metamethods for an in-depth metamethod explanation). The parameter <nilDefault> may be given to as a default if <value> is nil.

  • This method also contains a method called isBoolean(<v>(any)) which determines if the value is boolean.
  • This method will return false if <value> is any of the following:
    • nil
    • false
    • 0
    • ''
    • NaN
    • any variant/alias of the string "false", ex: "f"false
  • Else, it will return true.

Examples

  • Boolean("foo")true
  • Boolean(true)true
  • Boolean(0)false
  • Boolean(1)true
  • Boolean()false

Function

Function(<value>(any))
This Method cannot be called as a function, but has some useful methods. The following consists of the method's methods.

  • makeClassMethod(<type>(any)<f>(function))

Function.makeMethod

Function.makeClassMethod(<type>(any)<v>(functiontable))
This method creates a method with <v> if it is a function. If <v> is a table, it iterates over the table where each key is a method to be made into the type set by <type>. The returned value (the return value is a table with each value as a static method if the passed value was a table of methods) is meant to be used in table.makeClass().

Metamethods

Each method listed here has a corresponding metamethod which is used when it's respective function is used and the value being passed to the function has a metatable to call the metamethod from.

Note that String() uses the native __tostring metamethod. It's documentation can be found here.
Also note that __totable(), __toboolean(), and __tonumber() are the same in how they operate when used.
Also note that Function() does not have a metamethod due to MediaWiki modules not having access to loadstring().

The following list shows which metamethod corresponds to which method listed here:

  • __totable Corresponds to Table().
  • __tostring Corresponds to String().
  • __tonumber Corresponds to Number().
  • __toboolean Corresponds to Boolean().

Syntax

__totable

__totable(<t>(table)<entries>(table)<iter>(function)<len>(number))
If the metamethod is a function, it is called with the following parameters:

  • <t> is the table the method was called on.
  • <entries> is a table containing the keys and values of the table the method was called on.
  • <iter> is a iterator for use in a for...<variables>...in...<iterator>...do...<block>...end loop.
    • You can simply pass this variable inside the function to the loop to start iterating. Note that this iterator has already had a value passed into it so there is no need to call it when using.
  • <len> Is the total length of the table. This includes named parameters.

If the metamethod is not a function, it uses the value of the metamethod.

__tostring/__tonumber/__toboolean

__tostring(<v>(any))/__tonumber(<v>(any))/__toboolean(<v>(any))
If the metamethod is a function, it is called with the following parameters:

  • <v> is the value the method was called on.

If the metamethod is not a function, it uses the value of the metamethod.

Submodules

No submodules found for this page. Try purging the page or viewing all subpages.


See Also

Hypixel SkyBlock Wiki Standard Lua Libraries (hsw/stdll) v · d · e
Type Libraries
Module Loading Utilities
General Utilities
Meta Modules
Object-oriented
Caching
Module:Constructor/doc

Module Code

local loader = require('Module:Loader')
local string, table, yesno, arguments, makeClass, lu =
	loader.require('String', 'Table', 'Yesno', 'Arguments', 'MakeClass', 'LibU')
local getArgs = arguments.getArgs

-- Begin Exports
local p = {}
local NaN = 0/0
math.NaN = NaN
math.Infinity = math.huge
p.Infinity = math.huge
p.NaN = NaN

function p.isNaN(v)
	return type(v) == 'number' and tostring(v) == '-nan'
end

local function makeStaticMethods(t)
	local tmp = {}
	table.each(t, function(v, k)
		tmp[k] = { 'static', v }
	end)
	return tmp
end

local function length(v)
	if pcall(table.length, v) then
		return table.length(v)
	elseif pcall(string.len, v) then
		return string.len(v)
	elseif v == true then
		return 1
	elseif type(v) == 'number' then
		return v
	else
		return 0
	end
end

(function()
	tmp = makeStaticMethods(math)
	
	p.Number = makeClass.makeClass(function(self, n)
		local n = n ~= nil and n or 0
		
		local mt = getmetatable(n) or {}
		local tp = type(n)
		local tn = tonumber(n)
		
		if mt.__tonumber then
			return type(mt.__tonumber) == 'function' and mt.__tonumber(n) or mt.__tonumber
		elseif n == true then
			return 1
		elseif n == false then
			return 0
		elseif n == '' then
			return 0
		elseif tn ~= nil then
			return tn
		else
			return NaN
		end
	end, tmp, nil, { ignoreTableRet = true })
end)();

(function()
	tmp = makeStaticMethods(string)
	p.String = makeClass.makeClass(function(self, v) return tostring(v ~= nil and v or '') end, tmp, nil, { ignore = true })
end)();

(function()
	p.Boolean = makeClass.makeClass(function(self, v, nilDefault)
		local mt = getmetatable(v) or {}
		local lowerS = tostring(v):lower()
		
		if mt.__toboolean then
			return type(mt.__toboolean) == 'function' and mt.__toboolean(v) or mt.__toboolean
		elseif nilDefault ~= nil and v == nil then
			return nilDefault
		elseif v == false or v == true then
			return v
		elseif v == nil or v == '' or tonumber(v) == 0 or isNaN(v) then
			return false
		elseif lowerS == 'yes' 
		or lowerS == 'y' 
		or lowerS == 'o'
		or lowerS == 't'
		or lowerS == 'tru'
		or lowerS == 'true'
		then
			return true
		elseif lowerS == 'no' 
		or lowerS == 'n' 
		or lowerS == 'f'
		or lowerS == 'off'
		or lowerS == 'fal'
		or lowerS == 'false'
		then
			return false
		elseif type(v) == 'function' or length(v) > 0 then
			return true
		else
			return false
		end
	end, {
		isBoolean = { 'static', function(v)
			return v == false or v == true
		end, },
	}, nil, { ignore = true })
end)();

(function()
	tmp = makeStaticMethods(table)
	p.Table = makeClass.makeClass(function(self, ...)
		local t = {}
		local args = table.pack(... or {})
		local mt = getmetatable(args[1]) or {}
		
		if table.length(args) <= 2 and mt.__totable and type(args[1]) == 'table' then
			if type(args[2]) == 'table' then
				setmetatable(args[1], args[2])
			end
			return type(mt.__totable) == 'function' and mt.__totable(args[1], table.entries(args[1]), table.sortedPairs(args[1]), table.length(args[1]))
		elseif type(args[1]) == 'table' and table.length(args[1]) == 1 then
			return args[1]
		end
		
		table.each(args, function(v, k)
			t[k] = v
		end)
		
		return t
	end, tmp, nil, { ignore = true })
end)();

(function()
	local methods = {}
	
	function methods:callMethod(f, thisArg, ...)
		lu.checkType(1, f, 'function')
		return f(thisArg, ...)
	end
	
	p.Function = makeClass.makeClass(function(self)
		return function() end
	end, makeStaticMethods(methods), nil, { ignore = true })
end)();

-- Pushes all exports to global table
for k, v in pairs(p) do
	_G[k] = v
end

-- Finish Module/Exports
return p