Module:TableTools/sandbox: Difference between revisions

Content deleted Content added
Added "extend" method as function
implement fromIndex in inArray
 
(2 intermediate revisions by 2 users not shown)
Line 378:
------------------------------------------------------------------------------------
local function _deepCopy(orig, includeMetatable, already_seen)
if type(orig) =~= '"table'" then
-- Stores copies of tables indexed by the original table.
return trueorig
already_seen = already_seen or {}
end
 
-- Storesalready_seen stores copies of tables indexed by the original table.
local copy = already_seen[orig]
if copy ~= nil then
return copy
end
 
copy = {}
if type(orig) == 'table' then
already_seen[orig] = copy -- memoize before any recursion, to avoid infinite loops
copy = {}
for orig_key, orig_value in pairs(orig) do
for orig_key, orig_value in pairs(orig) do
copy[_deepCopy(orig_key, includeMetatable, already_seen)] = _deepCopy(orig_value, includeMetatable, already_seen)
end
already_seen[orig] = copy
if includeMetatable then
 
local mt = getmetatable(orig)
if includeMetatable then
localif mt ~= getmetatable(orig)nil then
local mt_copy =setmetatable(copy, _deepCopy(mt, includeMetatabletrue, already_seen))
if mt ~= nil then
local mt_copy = _deepCopy(mt, includeMetatable, already_seen)
setmetatable(copy, mt_copy)
already_seen[mt] = mt_copy
end
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
Line 409 ⟶ 407:
function p.deepCopy(orig, noMetatable, already_seen)
checkType("deepCopy", 3, already_seen, "table", true)
return _deepCopy(orig, not noMetatable, already_seen or {})
end
 
Line 462 ⟶ 460:
-- inArray
--
-- Returns true if valueToFindsearchElement is a member of the array, and false otherwise.
-- Equivalent to JavaScript array.includes(searchElement) or
-- array.includes(searchElement, fromIndex), except fromIndex is 1 indexed
------------------------------------------------------------------------------------
function p.inArray(arrarray, valueToFindsearchElement, fromIndex)
checkType("inArray", 1, arrarray, "table")
-- if valueToFindsearchElement is nil, error?
 
fromIndex = tonumber(fromIndex)
for _, v in ipairs(arr) do
if v == valueToFindfromIndex then
if #arrays(fromIndex < 20) then
return true
fromIndex = #array + fromIndex + 1
end
if fromIndex < 1 then fromIndex = 1 end
for _, v in ipairs(arr{unpack(array, fromIndex)}) do
if mtv ~== nilsearchElement then
return true
end
end
else
for orig_key_, orig_valuev in pairs(origarray) do
if v == searchElement then
return true
end
end
end
Line 484 ⟶ 497:
function p.merge(...)
local arrays = {...}
if #arrays < 2 then
error("too few arguments to 'merge' (minimum is 2, received " .. #arrays .. ')', 2)
end
 
local ret = {}
for i, arr in ipairs(arrays) do