m (actually i lie, i haven't. but now it's here anyway.) |
DarthKitty (talk | contribs) m (remove dependency on Module:Array) Tag: sourceedit |
||
(7 intermediate revisions by one other user not shown) | |||
Line 12: | Line 12: | ||
return unpack(t, f, l) |
return unpack(t, f, l) |
||
end |
end |
||
+ | |||
+ | table.clone = mw.clone |
||
function table.contains(a, b) |
function table.contains(a, b) |
||
for k,v in pairs(a) do if(v==b) then return true end end |
for k,v in pairs(a) do if(v==b) then return true end end |
||
return false |
return false |
||
+ | end |
||
+ | |||
+ | function table.indexof(a, b) |
||
+ | for k,v in pairs(a) do if(v==b) then return k end end |
||
+ | return false |
||
+ | end |
||
+ | |||
+ | function table.keys(t) |
||
+ | local obj = {} |
||
+ | for k,v in pairs(t) do table.insert(obj, k) end |
||
+ | return obj |
||
end |
end |
||
--for printing purposes |
--for printing purposes |
||
− | function table.tostring(t, h) |
+ | function table.tostring(t, h, nl, ind, inc) |
− | if |
+ | if nl == nil then nl = "" end |
+ | if ind == nil then ind = "" end |
||
+ | if inc == nil then inc = 0 end |
||
+ | if type(t) ~= "table" then return tostring(t) end |
||
if h=="" or h==nil then h = 100 end |
if h=="" or h==nil then h = 100 end |
||
local i = 1 |
local i = 1 |
||
local l = {} |
local l = {} |
||
− | local array = require "Module:Array" |
||
for k, v in pairs(t) do |
for k, v in pairs(t) do |
||
local auto = false |
local auto = false |
||
Line 32: | Line 47: | ||
end |
end |
||
local ttl = h |
local ttl = h |
||
− | table.insert(l, (auto and "" or k .. " = ") .. ((type(v)=="table" and ttl>1) and table.tostring(v, ttl-1) or (type(v)=="string" and '"' .. v .. '"' or tostring(v)))) |
+ | table.insert(l, (auto and "" or k .. " = ") .. ((type(v)=="table" and ttl>1) and table.tostring(v, ttl-1, nl, ind, inc+1) or (type(v)=="string" and '"' .. v .. '"' or tostring(v)))) |
end |
end |
||
− | return "{ " .. |
+ | return "{ " .. nl .. string.rep(ind, inc+1) .. table.concat(l, ", " .. nl .. (string.rep(ind, inc+1))) .. " " .. nl .. string.rep(ind, inc) .. "}" |
end |
end |
||
Latest revision as of 02:14, 5 September 2017
Documentation for this module may be created at Module:Table/doc
--Expands table.
--it includes "pack" and "unpack" which are default methods of table in Lua 5.2
--Lua 5.2 also handles (.../arg) differently, not like a table but instead comma-separated values that have to be packed
function table.pack(...)
local o = {}
for k,v in pairs(arg) do o[k] = v end
return o
end
function table.unpack(t, f, l)
return unpack(t, f, l)
end
table.clone = mw.clone
function table.contains(a, b)
for k,v in pairs(a) do if(v==b) then return true end end
return false
end
function table.indexof(a, b)
for k,v in pairs(a) do if(v==b) then return k end end
return false
end
function table.keys(t)
local obj = {}
for k,v in pairs(t) do table.insert(obj, k) end
return obj
end
--for printing purposes
function table.tostring(t, h, nl, ind, inc)
if nl == nil then nl = "" end
if ind == nil then ind = "" end
if inc == nil then inc = 0 end
if type(t) ~= "table" then return tostring(t) end
if h=="" or h==nil then h = 100 end
local i = 1
local l = {}
for k, v in pairs(t) do
local auto = false
if k == i then
auto = true
i = i + 1
end
local ttl = h
table.insert(l, (auto and "" or k .. " = ") .. ((type(v)=="table" and ttl>1) and table.tostring(v, ttl-1, nl, ind, inc+1) or (type(v)=="string" and '"' .. v .. '"' or tostring(v))))
end
return "{ " .. nl .. string.rep(ind, inc+1) .. table.concat(l, ", " .. nl .. (string.rep(ind, inc+1))) .. " " .. nl .. string.rep(ind, inc) .. "}"
end
return table