Module:Constructor

local getArgs = require('Module:Arguments').getArgs local string = require('Module:String') local table = require('Module:Table') local yesno = require('Module:Yesno') local lu = require('Module:LibU')

-- 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 = table.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 = table.makeClass(function(self, v) return tostring(v ~= nil and v or '') end, tmp, nil, { ignore = true }) end);

(function	p.Boolean = table.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 = table.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 = table.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