"ماڈیول:table" کے نسخوں کے درمیان فرق

حذف شدہ مندرجات اضافہ شدہ مندرجات
«--[[ ------------------------------------------------------------------------------------ -- table (formerly TableTool...» مواد پر مشتمل نیا صفحہ بنایا
 
کوئی خلاصۂ ترمیم نہیں
 
سطر 52:
--]]
function export.isPositiveInteger(v)
ifreturn type(v) == 'number' and v >= 1 and floor(v) == v and v < infinity then
return true
else
return false
end
end
 
سطر 115 ⟵ 111:
Equivalent to mw.clone?
]]
local function export.deepcopy(orig, noMetatableincludeMetatable, already_seen)
-- Stores copies of tables indexed by the original table.
local orig_type = type(orig)
already_seen = already_seen or {}
local copy
if orig_type == 'table' then
local copy = already_seen[orig]
if copy ~= nil then
return copy
end
if type(orig) == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[export.deepcopy(orig_key, noMetatableincludeMetatable, already_seen)] = export.deepcopy(orig_value, noMetatableincludeMetatable, already_seen)
end
already_seen[orig] = copy
if not noMetatable then
setmetatable(copy, export.deepcopy(getmetatable(orig)))
if includeMetatable then
local mt = getmetatable(orig)
if mt ~= nil then
local mt_copy = deepcopy(mt, includeMetatable, already_seen)
setmetatable(copy, mt_copy)
end
end
else -- number, string, boolean, etc
سطر 130 ⟵ 138:
end
return copy
end
 
function export.deepcopy(orig, noMetatable, already_seen)
checkType("deepcopy", 3, already_seen, "table", true)
return deepcopy(orig, not noMetatable, already_seen)
end
 
--[[
------------------------------------------------------------------------------------
-- append
--
-- This appends two tables together and returns the result. Compare the Lisp
-- expression (append list1 list2).
------------------------------------------------------------------------------------
--]]
function export.append(t1, t2)
checkType('append', 1, t1, 'table')
checkType('append', 2, t2, 'table')
local ret = {}
for _, v in ipairs(t1) do
table.insert(ret, v)
end
for _, v in ipairs(t2) do
table.insert(ret, v)
end
return ret
end
 
سطر 170 ⟵ 205:
------------------------------------------------------------------------------------
--]]
function export.numKeys(t, checked)
if not checked then
checkType('numKeys', 1, t, 'table')
checkType('numKeys', 1, t, 'table')
end
local isPositiveInteger = export.isPositiveInteger
local nums = {}
سطر 183 ⟵ 220:
table.sort(nums)
return nums
end
 
function export.maxIndex(t)
checkType('maxIndex', 1, t, 'table')
local positiveIntegerKeys = export.numKeys(t)
if positiveIntegerKeys[1] then
return math.max(unpack(positiveIntegerKeys))
else
return 0 -- ???
end
end
 
سطر 348 ⟵ 395:
 
--[[
Recursively compare two values that may be tables, including tables with
Takes table and a value to be found.
nested tables as values. Return true if both values are structurally equal.
If the value is in the array portion of the table, return true.
Note that this handles arbitary levels of nesting. If all tables are known
If the value is in the hashmap or not in the table, return false.
to be lists (with only integral keys), use export.deepEqualsList, which will
be more efficient.
 
NOTE: This is *NOT* smart enough to properly handle cycles; in such a case, it
will get into an infinite loop.
]]
function export.containsdeepEquals(listx, xy)
if type(x) == "table" and type(y) == "table" then
for _, v in ipairs(list) do
-- Two tables are the same if they have the same number of elements
if v == x then return true end
-- and all keys that are present in one of the tables compare equal
-- to the corresponding keys in the other table, using structural
-- comparison.
local sizex = 0
for key, value in pairs(x) do
if not export.deepEquals(value, y[key]) then
return false
end
sizex = sizex + 1
end
local sizey = export.size(y)
if sizex ~= sizey then
return false
end
return true
end
return x == y
end
 
--[[
Recursively compare two values that may be lists (i.e. tables with integral
keys), including lists with nested lists as values. Return true if both values
are structurally equal. Note that this handles arbitary levels of nesting.
Results are undefined if tables with non-integral keys are present anywhere in
either structure; if that may be the case, use export.deepEquals, which will
handle such tables correctly but be less efficient on lists than
export.deepEqualsList.
 
NOTE: This is *NOT* smart enough to properly handle cycles; in such a case, it
will get into an infinite loop.
]]
function export.deepEqualsList(x, y)
if type(x) == "table" and type(y) == "table" then
if #x ~= #y then
return false
end
for key, value in ipairs(x) do
if not export.deepEqualsList(value, y[key]) then
return false
end
end
return true
end
return x == y
end
 
--[[
Given a list and a value to be found, return true if the value is in the array
portion of the list. Shallow comparison is used unless `deepCompare` is given
(in which case comparison is done using `deepEqualsList`).
]]
function export.contains(list, x, deepCompare)
checkType('contains', 1, list, 'table')
if deepCompare then
for _, v in ipairs(list) do
if export.deepEqualsList(v, x) then return true end
end
else
for _, v in ipairs(list) do
if v == x then return true end
end
end
return false
end
 
--[[
Given a general table and a value to be found, return true if the value is in
either the array or hashmap portion of the table. Shallow comparison is used
unless `deepCompare` is given (in which case comparison is done using
`deepEquals`).
]]
function export.tableContains(tbl, x, deepCompare)
checkType('tableContains', 1, tbl, 'table')
if deepCompare then
for _, v in pairs(tbl) do
if export.deepEquals(v, x) then return true end
end
else
for _, v in pairs(tbl) do
if v == x then return true end
end
end
return false
end
 
--[[
Given a list and a value to be inserted, append or insert the value if not
already present in the list. Shallow comparison is used unless `deepCompare`
is given (in which case comparison is done using `deepEqualsList`). Appends to
the end, like the default behavior of table.insert(), unless `pos` is given,
in which case insertion happens at position `pos` (i.e. before the existing
item at position `pos`).
 
NOTE: The order of `item` and `pos` is reversed in comparison to table.insert(),
which uses `table.insert(list, item)` to insert at the end but
`table.insert(list, pos, item)` to insert at position POS.
]]
-- append to list if element not already present
function export.insertIfNot(list, item, pos, deepCompare)
if not export.contains(list, item, deepCompare) then
if pos then
table.insert(list, pos, item)
else
table.insert(list, item)
end
end
end
 
سطر 467 ⟵ 623:
 
--[=[
Joins an array with serial comma and serial conjunction, normally "and". An improvement on
An improvement on mw.text.listToText, which doesn't properly handle serial
commas.
Options:
- conj
Conjunction to use; defaults to "and".
- italicizeConj
Italicize conjunction: for [[Module:Template:also]]
سطر 490 ⟵ 649:
local conj
if length > 1 then
conj = options.conj or "and"
if options.italicizeConj then
conj = "''" .. conj .. "''"