Module:Arguments: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(fix bug where explicitly deleted args were still appearing when iterated over with pairs or ipairs - code courtesy of User:Jackmcbarn) |
(apply changes from sandbox - all tests pass) |
||
Line 46: | Line 46: | ||
local function tidyValNoChange(key, val) |
local function tidyValNoChange(key, val) |
||
return val |
return val |
||
⚫ | |||
local function matchesTitle(given, title) |
|||
local tp = type( given ) |
|||
return (tp == 'string' or tp == 'number') and mw.title.new( given ).prefixedText == title |
|||
end |
end |
||
Line 82: | Line 87: | ||
local title = parent:getTitle():gsub('/sandbox$', '') |
local title = parent:getTitle():gsub('/sandbox$', '') |
||
local found = false |
local found = false |
||
if |
if matchesTitle(options.wrappers, title) then |
||
⚫ | |||
⚫ | |||
for _,v in pairs(options.wrappers) do |
for _,v in pairs(options.wrappers) do |
||
if v |
if matchesTitle(v, title) then |
||
found = true |
found = true |
||
break |
break |
||
end |
end |
||
end |
end |
||
⚫ | |||
⚫ | |||
end |
end |
||
Line 165: | Line 170: | ||
setmetatable(args, metatable) |
setmetatable(args, metatable) |
||
local function mergeArgs( |
local function mergeArgs(tables) |
||
--[[ |
--[[ |
||
-- Accepts multiple tables as input and merges their keys and values |
-- Accepts multiple tables as input and merges their keys and values |
||
-- into one table |
-- into one table. If a value is already present it is not overwritten; |
||
-- |
-- tables listed earlier have precedence. We are also memoizing nil |
||
-- |
-- values, which can be overwritten if they are 's' (soft). |
||
-- are 's' (soft). |
|||
--]] |
--]] |
||
for _, t in ipairs(tables) do |
for _, t in ipairs(tables) do |
||
for key, val in |
for key, val in pairs(t) do |
||
if metaArgs[key] == nil and nilArgs[key] ~= 'h' then |
if metaArgs[key] == nil and nilArgs[key] ~= 'h' then |
||
local tidiedVal = tidyVal(key, val) |
local tidiedVal = tidyVal(key, val) |
||
Line 263: | Line 267: | ||
-- Called when pairs is run on the args table. |
-- Called when pairs is run on the args table. |
||
if not metatable.donePairs then |
if not metatable.donePairs then |
||
mergeArgs( |
mergeArgs(argTables) |
||
metatable.donePairs = true |
metatable.donePairs = true |
||
metatable.doneIpairs = true |
|||
end |
end |
||
return pairs(metaArgs) |
return pairs(metaArgs) |
||
end |
|||
local function inext(t, i) |
|||
-- This uses our __index metamethod |
|||
local v = t[i + 1] |
|||
if v ~= nil then |
|||
return i + 1, v |
|||
end |
|||
end |
end |
||
metatable.__ipairs = function () |
metatable.__ipairs = function (t) |
||
-- Called when ipairs is run on the args table. |
-- Called when ipairs is run on the args table. |
||
return inext, t, 0 |
|||
if not metatable.doneIpairs then |
|||
mergeArgs(ipairs, argTables) |
|||
metatable.doneIpairs = true |
|||
⚫ | |||
return ipairs(metaArgs) |
|||
end |
end |
||