Module:Template input utils: Difference between revisions

Ganaram inukshuk (talk | contribs)
m minor renaming
Ganaram inukshuk (talk | contribs)
to-table functions remove original values from args table; this is now toggleable
Line 13: Line 13:
-- table, as it may require further processing (EG, parsing to number, ratio,  
-- table, as it may require further processing (EG, parsing to number, ratio,  
-- kv-pairs, etc).
-- kv-pairs, etc).
function p.numbered_args_to_table(args, max_num, key_fmt)
function p.numbered_args_to_table(args, max_num, key_fmt, keep_originals)
local max_num = max_num or 100
local max_num = max_num or 100
local key_fmt = key_fmt or "Entry %d"
local key_fmt = key_fmt or "Entry %d"
local keep_originals = keep_originals or false -- Denotes whether to keep originals in table or remove them; default false
local entries = {}
local entries = {}
Line 22: Line 23:
local entry = args[key] -- Extract
local entry = args[key] -- Extract
table.insert(entries, entry) -- Insert
table.insert(entries, entry) -- Insert
args[key] = nil -- Remove original
if not keep_originals then
args[key] = nil -- Remove original
end
end
end
Line 35: Line 38:
-- table, as it may require further processing (EG, parsing to number, ratio,  
-- table, as it may require further processing (EG, parsing to number, ratio,  
-- kv-pairs, etc).
-- kv-pairs, etc).
function p.numbered_header_data_args_to_table(args, max_num, header_fmt, data_fmt, is_strict_pair)
function p.numbered_header_data_args_to_table(args, max_num, header_fmt, data_fmt, is_strict_pair, keep_originals)
local max_num    = max_num or 100
local max_num    = max_num or 100
local header_fmt = header_fmt or "Header %d"
local header_fmt = header_fmt or "Header %d"
local data_fmt  = data_fmt or "Data %d"
local data_fmt  = data_fmt or "Data %d"
local is_strict_pair = is_strict_pair or false -- Denotes whether a pair must have both header and data
local is_strict_pair = is_strict_pair or false -- Denotes whether a pair must have both header and data
local keep_originals = keep_originals or false -- Denotes whether to keep originals in table or remove them; default false
local entries = {}
local entries = {}
Line 60: Line 64:
-- Remove originals
-- Remove originals
args[header_key] = nil
if not keep_originals then
args[data_key] = nil
args[header_key] = nil
args[data_key] = nil
end
end
end