Modul:Array
Izgled
local Array,Internal = {},{}
setmetatable(Array,{
__call=function(self,...) return self.create(...) end
})
function Array.pairs(array)
return array().pairsAsc()
end
function Array.pairsAsc(array)
return array().pairsAsc()
end
function Array.pairsDesc(array)
return array().pairsDesc()
end
function Array.length(array)
return array().length()
end
function Array.position(array,position)
return array().position(array,position)
end
function Array.inArray(array,value)
return array().inArray(value)
end
function Array.insert(array,arg1,arg2)
return array().insert(arg1,arg2)
end
function Array.print(array)
result = Internal.printHelperArray(array,0)
mw.log(result)
return result
end
function Internal.printHelperArray(array,level)
if array().length() == 0 then return 'Array (0)' end
local whitespace = ''
local i = 1
for a=i,level do whitespace = whitespace .. ' ' end
local result = 'Array (' .. array().length() .. ')\r\n' .. whitespace .. '(\r\n'
local y = true
local keys,values = array().getKeys(),array().getValues()
while y do
if type(values[keys[i]]) == 'table' and values[keys[i]]().type == 'array' then
result = result .. whitespace .. ' [' .. keys[i] .. '] => ' .. Internal.printHelperArray(values[keys[i]],level+4) .. '\r\n'
elseif type(values[keys[i]]) == 'table' then
result = result .. whitespace .. ' [' .. keys[i] .. '] => ' .. Internal.printHelperTable(values[keys[i]],level+4) .. '\r\n'
elseif type(values[keys[i]]) == 'string' then
result = result .. whitespace .. ' [' .. keys[i] .. '] => (string) "' .. values[keys[i]] .. '"\r\n'
elseif type(values[keys[i]]) == 'number' then
result = result .. whitespace .. ' [' .. keys[i] .. '] => (number) ' .. values[keys[i]] .. '\r\n'
elseif type(values[keys[i]]) == 'boolean' then
local bool = ''
if values[keys[i]] then bool = 'true' else bool = 'false' end
result = result .. whitespace .. ' [' .. keys[i] .. '] => (boolean) ' .. bool .. '\r\n'
elseif type(values[keys[i]]) == 'function' then
result = result .. whitespace .. ' [' .. keys[i] .. '] => (function) ' .. 'function' .. '\r\n'
else
result = result .. whitespace .. ' [' .. keys[i] .. '] => ' .. values[keys[i]] .. '\r\n'
end
i = i + 1
if keys[i] == nil then y = false end
end
result = result .. whitespace .. ')'
return result
end
function Internal.printHelperTable(tableVal,level)
local whitespace = ''
local i = 1
for a=i,level do whitespace = whitespace .. ' ' end
local count = 0
for _ in pairs(tableVal) do count = count + 1 end
local result = 'Table (' .. count .. ')\r\n' .. whitespace .. '(\r\n'
for key,value in pairs(tableVal) do
if type(value) == 'table' and value().type == 'array' then
result = result .. whitespace .. ' [' .. key .. '] => ' .. Internal.printHelperArray(value,level+4) .. '\r\n'
elseif type(value) == 'table' then
result = result .. whitespace .. ' [' .. key .. '] => ' .. Internal.printHelperTable(value,level+4) .. '\r\n'
elseif type(value) == 'string' then
result = result .. whitespace .. ' [' .. key .. '] => (string) "' .. value .. '"\r\n'
elseif type(value) == 'number' then
result = result .. whitespace .. ' [' .. key .. '] => (number) ' .. value .. '\r\n'
elseif type(value) == 'boolean' then
local bool = ''
if key then bool = 'true' else bool = 'false' end
result = result .. whitespace .. ' [' .. key .. '] => (boolean) ' .. bool .. '\r\n'
elseif type(value) == 'function' then
result = result .. whitespace .. ' [' .. key .. '] => (function) ' .. 'function' .. '\r\n'
else
result = result .. whitespace .. ' [' .. key .. '] => ' .. value .. '\r\n'
end
end
result = result .. whitespace .. ')'
return result
end
function Array.create(...)
local newArray,keys,values,intern={},{},{},{}
intern.type='array'
intern.pairsAsc=function(self) -- pairs iterator
local count=0
return function()
count=count+1
return keys[count],values[keys[count]]
end
end
intern.pairsDesc=function(self) -- pairs iterator
local count=intern.length()+1
return function()
count=count-1
return keys[count],values[keys[count]]
end
end
intern.inArray=function(value)
length = intern.length()
for i=1,length,1 do
if values[i] == value then
return true
end
end
return false
end
intern.position=function(self,position)
return values[keys[position]]
end
intern.length=function()
local count = 0
for _ in intern.pairsAsc(keys) do count = count + 1 end
return count
end
intern.print=function()
return Array.print(newArray)
end
intern.getKeys=function()
return keys
end
intern.getValues=function()
return values
end
intern.insert=function(arg1,arg2)
if arg1 ~= nil and arg2 ~= nil then
newArray[arg1]=arg2
return arg1
elseif arg1 ~= nil and arg2 == nil then
local key = intern.getNewKey()
newArray[key]=arg1
return key
end
end
intern.getNewKey=function()
local i = 1
while true do
if keys[i] == nil then
return i
else
i = i + 1
end
end
end
setmetatable(newArray,{
__newindex=function(self,key,value)
if value==nil then -- Handle item delete
local length=self().length()
local keyCount = false
for i=1,length,1 do
if keys[i] == key then
keyCount = i
break
end
end
if keyCount then
table.remove(keys,keyCount)
end
elseif not self[key] then table.insert(keys,key)
end
values[key]=value -- replace/create
end,
__index=function(self,key) return values[key] end,
__call=function(self,...) return intern end
})
for x=1,table.getn(arg) do
if type(arg[x]) == 'table' and pcall(arg[x]) then
if type(arg[x]) == 'table' and arg[x]().type == 'array' then
newArray[intern.getNewKey()]=arg[x]
end
elseif type(arg[x]) == 'table' then
for k,v in pairs(arg[x]) do newArray[k]=v end
else
newArray[intern.getNewKey()]=arg[x]
end
end
return newArray
end
return Array