Module:TableTools/sandbox: Difference between revisions

Content deleted Content added
Improved documentation formatting. Improved isArray and added isArrayLike. Applied Johnuniq's bugfix in _deepCopy.
clean whitespace
Line 1:
------------------------------------------------------------------------------------
-- TableTools TableTools --
-- --
-- This module includes a number of functions for dealing with Lua tables. --
Line 76:
exists[v] = true
end
end
end
return ret
end
 
------------------------------------------------------------------------------------
Line 145:
local nums = {}
for k, v in pairs(t) do
if type(k) == 'string' then
local num = mw.ustring.match(k, pattern)
if num then
Line 160:
--
-- Given a table with keys like ("foo1", "bar1", "foo2", "baz2"), returns a table
-- of subtables in the format
-- { [1] = {foo = 'text', bar = 'text'}, [2] = {foo = 'text', baz = 'text'} }
-- Keys that don't end with an integer are stored in a subtable named "other".
Line 291:
checkTypeMulti('keysToList', 2, keySort, { 'function', 'boolean', 'nil' })
end
 
local list = {}
local index = 1
Line 298:
index = index + 1
end
 
if keySort ~= false then
keySort = type(keySort) == 'function' and keySort or defaultKeySort
 
table.sort(list, keySort)
end
 
return list
end
Line 317:
checkType('sortedPairs', 1, t, 'table')
checkType('sortedPairs', 2, keySort, 'function', true)
 
local list = p.keysToList(t, keySort, true)
 
local i = 0
return function()
Line 380:
function p.invert(array)
checkType("invert", 1, array, "table")
 
local map = {}
for i, v in ipairs(array) do
map[v] = i
end
 
return map
end
Line 398:
function p.listToSet(t)
checkType("listToSet", 1, t, "table")
 
local set = {}
for _, item in ipairs(t) do
set[item] = true
end
 
return set
end
Line 415:
-- Stores copies of tables indexed by the original table.
already_seen = already_seen or {}
 
local copy = already_seen[orig]
if copy ~= nil then
return copy
end
 
if type(orig) == 'table' then
copy = {}
Line 427:
end
already_seen[orig] = copy
 
if includeMetatable then
local mt = getmetatable(orig)
Line 444:
function p.deepCopy(orig, noMetatable, already_seen)
checkType("deepCopy", 3, already_seen, "table", true)
 
return _deepCopy(orig, not noMetatable, already_seen)
end
Line 457:
function p.sparseConcat(t, sep, i, j)
local list = {}
 
local list_i = 0
for _, v in p.sparseIpairs(t) do
Line 463:
list[list_i] = v
end
 
return table.concat(list, sep, i, j)
end
Line 503:
function p.inArray(arr, valueToFind)
checkType("inArray", 1, arr, "table")
 
-- if valueToFind is nil, error?
 
for _, v in ipairs(arr) do
if v == valueToFind then
Line 511:
end
end
 
return false
end