http://proplan.55bet-pro.com/wiki/index.php?action=history&feed=atom&title=M%C3%B3dulo%3ATableToolsMódulo:TableTools - Histórico de revisão-55BET Pro2026-03-13T18:43:26ZHistórico de revisões para esta página neste wikiMediaWiki 1.42.7http://proplan.55bet-pro.com/wiki/index.php?title=M%C3%B3dulo:TableTools&diff=79&oldid=prevMódulo:TableTools - Histórico de revisão-55BET Pro2024-06-27T11:11:54Z<p>uma edição</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<tr class="diff-title" lang="pt-BR">
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">← Edição anterior</td>
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">Edição das 08h11min de 27 de junho de 2024</td>
</tr><tr><td colspan="2" class="diff-notice" lang="pt-BR"><div class="mw-diff-empty">(Sem diferença)</div>
</td></tr></table>Henryzordhttp://proplan.55bet-pro.com/wiki/index.php?title=M%C3%B3dulo:TableTools&diff=78&oldid=prevMódulo:TableTools - Histórico de revisão-55BET Pro2024-04-27T01:49:03Z<p>Update from sandbox per request</p>
<p><b>Página nova</b></p><div>------------------------------------------------------------------------------------<br />
-- TableTools --<br />
-- --<br />
-- This module includes a number of functions for dealing with Lua tables. --<br />
-- It is a meta-module, meant to be called from other Lua modules, and should not --<br />
-- be called directly from #invoke. --<br />
------------------------------------------------------------------------------------<br />
<br />
local libraryUtil = require('libraryUtil')<br />
<br />
local p = {}<br />
<br />
-- Define often-used variables and functions.<br />
local floor = math.floor<br />
local infinity = math.huge<br />
local checkType = libraryUtil.checkType<br />
local checkTypeMulti = libraryUtil.checkTypeMulti<br />
<br />
------------------------------------------------------------------------------------<br />
-- isPositiveInteger<br />
--<br />
-- This function returns true if the given value is a positive integer, and false<br />
-- if not. Although it doesn't operate on tables, it is included here as it is<br />
-- useful for determining whether a given table key is in the array part or the<br />
-- hash part of a table.<br />
------------------------------------------------------------------------------------<br />
function p.isPositiveInteger(v)<br />
return type(v) == 'number' and v >= 1 and floor(v) == v and v < infinity<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- isNan<br />
--<br />
-- This function returns true if the given number is a NaN value, and false if<br />
-- not. Although it doesn't operate on tables, it is included here as it is useful<br />
-- for determining whether a value can be a valid table key. Lua will generate an<br />
-- error if a NaN is used as a table key.<br />
------------------------------------------------------------------------------------<br />
function p.isNan(v)<br />
return type(v) == 'number' and v ~= v<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- shallowClone<br />
--<br />
-- This returns a clone of a table. The value returned is a new table, but all<br />
-- subtables and functions are shared. Metamethods are respected, but the returned<br />
-- table will have no metatable of its own.<br />
------------------------------------------------------------------------------------<br />
function p.shallowClone(t)<br />
checkType('shallowClone', 1, t, 'table')<br />
local ret = {}<br />
for k, v in pairs(t) do<br />
ret[k] = v<br />
end<br />
return ret<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- removeDuplicates<br />
--<br />
-- This removes duplicate values from an array. Non-positive-integer keys are<br />
-- ignored. The earliest value is kept, and all subsequent duplicate values are<br />
-- removed, but otherwise the array order is unchanged.<br />
------------------------------------------------------------------------------------<br />
function p.removeDuplicates(arr)<br />
checkType('removeDuplicates', 1, arr, 'table')<br />
local isNan = p.isNan<br />
local ret, exists = {}, {}<br />
for _, v in ipairs(arr) do<br />
if isNan(v) then<br />
-- NaNs can't be table keys, and they are also unique, so we don't need to check existence.<br />
ret[#ret + 1] = v<br />
elseif not exists[v] then<br />
ret[#ret + 1] = v<br />
exists[v] = true<br />
end<br />
end<br />
return ret<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- numKeys<br />
--<br />
-- This takes a table and returns an array containing the numbers of any numerical<br />
-- keys that have non-nil values, sorted in numerical order.<br />
------------------------------------------------------------------------------------<br />
function p.numKeys(t)<br />
checkType('numKeys', 1, t, 'table')<br />
local isPositiveInteger = p.isPositiveInteger<br />
local nums = {}<br />
for k in pairs(t) do<br />
if isPositiveInteger(k) then<br />
nums[#nums + 1] = k<br />
end<br />
end<br />
table.sort(nums)<br />
return nums<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- affixNums<br />
--<br />
-- This takes a table and returns an array containing the numbers of keys with the<br />
-- specified prefix and suffix. For example, for the table<br />
-- {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix "a", affixNums will return<br />
-- {1, 3, 6}.<br />
------------------------------------------------------------------------------------<br />
function p.affixNums(t, prefix, suffix)<br />
checkType('affixNums', 1, t, 'table')<br />
checkType('affixNums', 2, prefix, 'string', true)<br />
checkType('affixNums', 3, suffix, 'string', true)<br />
<br />
local function cleanPattern(s)<br />
-- Cleans a pattern so that the magic characters ()%.[]*+-?^$ are interpreted literally.<br />
return s:gsub('([%(%)%%%.%[%]%*%+%-%?%^%$])', '%%%1')<br />
end<br />
<br />
prefix = prefix or ''<br />
suffix = suffix or ''<br />
prefix = cleanPattern(prefix)<br />
suffix = cleanPattern(suffix)<br />
local pattern = '^' .. prefix .. '([1-9]%d*)' .. suffix .. '$'<br />
<br />
local nums = {}<br />
for k in pairs(t) do<br />
if type(k) == 'string' then<br />
local num = mw.ustring.match(k, pattern)<br />
if num then<br />
nums[#nums + 1] = tonumber(num)<br />
end<br />
end<br />
end<br />
table.sort(nums)<br />
return nums<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- numData<br />
--<br />
-- Given a table with keys like {"foo1", "bar1", "foo2", "baz2"}, returns a table<br />
-- of subtables in the format<br />
-- {[1] = {foo = 'text', bar = 'text'}, [2] = {foo = 'text', baz = 'text'}}.<br />
-- Keys that don't end with an integer are stored in a subtable named "other". The<br />
-- compress option compresses the table so that it can be iterated over with<br />
-- ipairs.<br />
------------------------------------------------------------------------------------<br />
function p.numData(t, compress)<br />
checkType('numData', 1, t, 'table')<br />
checkType('numData', 2, compress, 'boolean', true)<br />
local ret = {}<br />
for k, v in pairs(t) do<br />
local prefix, num = mw.ustring.match(tostring(k), '^([^0-9]*)([1-9][0-9]*)$')<br />
if num then<br />
num = tonumber(num)<br />
local subtable = ret[num] or {}<br />
if prefix == '' then<br />
-- Positional parameters match the blank string; put them at the start of the subtable instead.<br />
prefix = 1<br />
end<br />
subtable[prefix] = v<br />
ret[num] = subtable<br />
else<br />
local subtable = ret.other or {}<br />
subtable[k] = v<br />
ret.other = subtable<br />
end<br />
end<br />
if compress then<br />
local other = ret.other<br />
ret = p.compressSparseArray(ret)<br />
ret.other = other<br />
end<br />
return ret<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- compressSparseArray<br />
--<br />
-- This takes an array with one or more nil values, and removes the nil values<br />
-- while preserving the order, so that the array can be safely traversed with<br />
-- ipairs.<br />
------------------------------------------------------------------------------------<br />
function p.compressSparseArray(t)<br />
checkType('compressSparseArray', 1, t, 'table')<br />
local ret = {}<br />
local nums = p.numKeys(t)<br />
for _, num in ipairs(nums) do<br />
ret[#ret + 1] = t[num]<br />
end<br />
return ret<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- sparseIpairs<br />
--<br />
-- This is an iterator for sparse arrays. It can be used like ipairs, but can<br />
-- handle nil values.<br />
------------------------------------------------------------------------------------<br />
function p.sparseIpairs(t)<br />
checkType('sparseIpairs', 1, t, 'table')<br />
local nums = p.numKeys(t)<br />
local i = 0<br />
local lim = #nums<br />
return function ()<br />
i = i + 1<br />
if i <= lim then<br />
local key = nums[i]<br />
return key, t[key]<br />
else<br />
return nil, nil<br />
end<br />
end<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- size<br />
--<br />
-- This returns the size of a key/value pair table. It will also work on arrays,<br />
-- but for arrays it is more efficient to use the # operator.<br />
------------------------------------------------------------------------------------<br />
function p.size(t)<br />
checkType('size', 1, t, 'table')<br />
local i = 0<br />
for _ in pairs(t) do<br />
i = i + 1<br />
end<br />
return i<br />
end<br />
<br />
local function defaultKeySort(item1, item2)<br />
-- "number" < "string", so numbers will be sorted before strings.<br />
local type1, type2 = type(item1), type(item2)<br />
if type1 ~= type2 then<br />
return type1 < type2<br />
elseif type1 == 'table' or type1 == 'boolean' or type1 == 'function' then<br />
return tostring(item1) < tostring(item2)<br />
else<br />
return item1 < item2<br />
end<br />
end<br />
------------------------------------------------------------------------------------<br />
-- keysToList<br />
--<br />
-- Returns an array of the keys in a table, sorted using either a default<br />
-- comparison function or a custom keySort function.<br />
------------------------------------------------------------------------------------<br />
function p.keysToList(t, keySort, checked)<br />
if not checked then<br />
checkType('keysToList', 1, t, 'table')<br />
checkTypeMulti('keysToList', 2, keySort, {'function', 'boolean', 'nil'})<br />
end<br />
<br />
local arr = {}<br />
local index = 1<br />
for k in pairs(t) do<br />
arr[index] = k<br />
index = index + 1<br />
end<br />
<br />
if keySort ~= false then<br />
keySort = type(keySort) == 'function' and keySort or defaultKeySort<br />
table.sort(arr, keySort)<br />
end<br />
<br />
return arr<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- sortedPairs<br />
--<br />
-- Iterates through a table, with the keys sorted using the keysToList function.<br />
-- If there are only numerical keys, sparseIpairs is probably more efficient.<br />
------------------------------------------------------------------------------------<br />
function p.sortedPairs(t, keySort)<br />
checkType('sortedPairs', 1, t, 'table')<br />
checkType('sortedPairs', 2, keySort, 'function', true)<br />
<br />
local arr = p.keysToList(t, keySort, true)<br />
<br />
local i = 0<br />
return function ()<br />
i = i + 1<br />
local key = arr[i]<br />
if key ~= nil then<br />
return key, t[key]<br />
else<br />
return nil, nil<br />
end<br />
end<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- isArray<br />
--<br />
-- Returns true if the given value is a table and all keys are consecutive<br />
-- integers starting at 1.<br />
------------------------------------------------------------------------------------<br />
function p.isArray(v)<br />
if type(v) ~= 'table' then<br />
return false<br />
end<br />
local i = 0<br />
for _ in pairs(v) do<br />
i = i + 1<br />
if v[i] == nil then<br />
return false<br />
end<br />
end<br />
return true<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- isArrayLike<br />
--<br />
-- Returns true if the given value is iterable and all keys are consecutive<br />
-- integers starting at 1.<br />
------------------------------------------------------------------------------------<br />
function p.isArrayLike(v)<br />
if not pcall(pairs, v) then<br />
return false<br />
end<br />
local i = 0<br />
for _ in pairs(v) do<br />
i = i + 1<br />
if v[i] == nil then<br />
return false<br />
end<br />
end<br />
return true<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- invert<br />
--<br />
-- Transposes the keys and values in an array. For example, {"a", "b", "c"} -><br />
-- {a = 1, b = 2, c = 3}. Duplicates are not supported (result values refer to<br />
-- the index of the last duplicate) and NaN values are ignored.<br />
------------------------------------------------------------------------------------<br />
function p.invert(arr)<br />
checkType("invert", 1, arr, "table")<br />
local isNan = p.isNan<br />
local map = {}<br />
for i, v in ipairs(arr) do<br />
if not isNan(v) then<br />
map[v] = i<br />
end<br />
end<br />
<br />
return map<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- listToSet<br />
--<br />
-- Creates a set from the array part of the table. Indexing the set by any of the<br />
-- values of the array returns true. For example, {"a", "b", "c"} -><br />
-- {a = true, b = true, c = true}. NaN values are ignored as Lua considers them<br />
-- never equal to any value (including other NaNs or even themselves).<br />
------------------------------------------------------------------------------------<br />
function p.listToSet(arr)<br />
checkType("listToSet", 1, arr, "table")<br />
local isNan = p.isNan<br />
local set = {}<br />
for _, v in ipairs(arr) do<br />
if not isNan(v) then<br />
set[v] = true<br />
end<br />
end<br />
<br />
return set<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- deepCopy<br />
--<br />
-- Recursive deep copy function. Preserves identities of subtables.<br />
------------------------------------------------------------------------------------<br />
local function _deepCopy(orig, includeMetatable, already_seen)<br />
if type(orig) ~= "table" then<br />
return orig<br />
end<br />
<br />
-- already_seen stores copies of tables indexed by the original table.<br />
local copy = already_seen[orig]<br />
if copy ~= nil then<br />
return copy<br />
end<br />
<br />
copy = {}<br />
already_seen[orig] = copy -- memoize before any recursion, to avoid infinite loops<br />
<br />
for orig_key, orig_value in pairs(orig) do<br />
copy[_deepCopy(orig_key, includeMetatable, already_seen)] = _deepCopy(orig_value, includeMetatable, already_seen)<br />
end<br />
<br />
if includeMetatable then<br />
local mt = getmetatable(orig)<br />
if mt ~= nil then<br />
setmetatable(copy, _deepCopy(mt, true, already_seen))<br />
end<br />
end<br />
<br />
return copy<br />
end<br />
<br />
function p.deepCopy(orig, noMetatable, already_seen)<br />
checkType("deepCopy", 3, already_seen, "table", true)<br />
return _deepCopy(orig, not noMetatable, already_seen or {})<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- sparseConcat<br />
--<br />
-- Concatenates all values in the table that are indexed by a number, in order.<br />
-- sparseConcat{a, nil, c, d} => "acd"<br />
-- sparseConcat{nil, b, c, d} => "bcd"<br />
------------------------------------------------------------------------------------<br />
function p.sparseConcat(t, sep, i, j)<br />
local arr = {}<br />
<br />
local arr_i = 0<br />
for _, v in p.sparseIpairs(t) do<br />
arr_i = arr_i + 1<br />
arr[arr_i] = v<br />
end<br />
<br />
return table.concat(arr, sep, i, j)<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- length<br />
--<br />
-- Finds the length of an array, or of a quasi-array with keys such as "data1",<br />
-- "data2", etc., using an exponential search algorithm. It is similar to the<br />
-- operator #, but may return a different value when there are gaps in the array<br />
-- portion of the table. Intended to be used on data loaded with mw.loadData. For<br />
-- other tables, use #.<br />
-- Note: #frame.args in frame object always be set to 0, regardless of the number<br />
-- of unnamed template parameters, so use this function for frame.args.<br />
------------------------------------------------------------------------------------<br />
function p.length(t, prefix)<br />
-- requiring module inline so that [[Module:Exponential search]] which is<br />
-- only needed by this one function doesn't get millions of transclusions<br />
local expSearch = require("Module:Exponential search")<br />
checkType('length', 1, t, 'table')<br />
checkType('length', 2, prefix, 'string', true)<br />
return expSearch(function (i)<br />
local key<br />
if prefix then<br />
key = prefix .. tostring(i)<br />
else<br />
key = i<br />
end<br />
return t[key] ~= nil<br />
end) or 0<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- inArray<br />
--<br />
-- Returns true if valueToFind is a member of the array, and false otherwise.<br />
------------------------------------------------------------------------------------<br />
function p.inArray(arr, valueToFind)<br />
checkType("inArray", 1, arr, "table")<br />
-- if valueToFind is nil, error?<br />
<br />
for _, v in ipairs(arr) do<br />
if v == valueToFind then<br />
return true<br />
end<br />
end<br />
return false<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- merge<br />
--<br />
-- Given the arrays, returns an array containing the elements of each input array<br />
-- in sequence.<br />
------------------------------------------------------------------------------------<br />
function p.merge(...)<br />
local arrays = {...}<br />
local ret = {}<br />
for i, arr in ipairs(arrays) do<br />
checkType('merge', i, arr, 'table')<br />
for _, v in ipairs(arr) do<br />
ret[#ret + 1] = v<br />
end<br />
end<br />
return ret<br />
end<br />
<br />
------------------------------------------------------------------------------------<br />
-- extend<br />
--<br />
-- Extends the first array in place by appending all elements from the second<br />
-- array.<br />
------------------------------------------------------------------------------------<br />
function p.extend(arr1, arr2)<br />
checkType('extend', 1, arr1, 'table')<br />
checkType('extend', 2, arr2, 'table')<br />
<br />
for _, v in ipairs(arr2) do<br />
arr1[#arr1 + 1] = v<br />
end<br />
end<br />
<br />
return p</div>infobox>Pppery