Documentation for this module may be created at Module:List/doc
local p = {}
local w_frame = require( 'module:frame' )
local w_table = require( 'module:table' )
function sort( list, template, options )
local baseCompare, compare
if options then
local optionList = mw.text.split( options, ' ' )
if w_table.contains( optionList, 'desc' ) then
baseCompare = function ( fst, snd )
return fst > snd
end
end
if w_table.contains( optionList, 'numeric' ) then
compare = function ( fst, snd )
return baseCompare( tonumber( fst ), tonumber( snd ) )
end
end
end
baseCompare = baseCompare or function ( fst, snd )
return fst <= snd
end
compare = compare or baseCompare
if template then
local frame = mw.getCurrentFrame()
for i, v in pairs( list ) do
list[i] = { frame:expandTemplate{ title = template, args = { v } }, v }
end
table.sort( list, function ( fst, snd )
return compare( fst[1], snd[1] )
end )
for i, v in pairs( list ) do
list[i] = v[2]
end
else
table.sort( list, compare )
end
return list
end
function p.map( f )
local args = w_frame.args( f )
if not args.list then
return args.default
end
local list = w_table.trim( mw.text.split( args.list, args.insep or args.sep or ',' ) )
if #list == 0 then
return args.default
end
if args.duplicates == 'strip' then
list = w_table.unique( list )
end
if args.presort then
sort( list, args.presort, args.presortoptions )
end
if args.template then
local frame = mw.getCurrentFrame()
for i, v in pairs( list ) do
list[i] = frame:expandTemplate{ title = args.template, args = { v } }
end
end
if args.postsort then
sort( list, args.postsort, args.postsortoptions )
end
if #list == 0 then
return args.default
end
return table.concat( list, args.outsep or args.sep or ', ' )
end
function p.find( f )
local args = w_frame.args( f )
if not args.list then
return args.default
end
local list = mw.text.split( args.list, args.insep or ',' )
if #list == 0 then
return args.default
end
if args.template then
local frame = mw.getCurrentFrame()
local elem
for i, v in pairs( list ) do
elem = frame:expandTemplate{ title = args.template, args = args.fieldsep and mw.text.split( v, args.fieldsep ) or { v } }
if mw.text.trim( elem ) ~= '' then
return args.intro .. elem .. args.outro
end
end
end
return args.default
end
return p