יחידה:Utility
ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:Utility/תיעוד
--[==[
this is a utility module.
it exports 3 functions:
* empty( s )
returns true if its parameter is nil, or a string made of zero or more white spaces.
* extract_options( frame, optionsPrefix )
the value of optionsPrefix is refered to as "options" below. "options" is also its devault velue.
looks in frame.args for an argument named "options", and tries to decode it as a JSON string.
then loop over arguments whose names are "options1", "options2" etc., decodes each as a JSON string,
and extends the table with the new or overriding values. if no valid JSON was found in any of the parameters,
an empty table returns.
* build_namelist( basepage, subpage ):
expects basepage as a string. returns a table of strings, made by basepags, '/', and subpage.
if subpage is nil, basepage is returned. if it's a string, then the namelist will have 2 items:
basepage, and basepage + '/' + subpage
if subpage is a list, loops over its members in the same manner, and return a list of size n + 1 - the first item
is basepage, and the rest are the concatanated names as described.
]==]
local p = {}
p.empty = function( s )
return s == nil or type( s ) == 'string' and mw.text.trim( s ) == ''
end
p.extract_options = function ( frame, optionsPrefix )
optionsPrefix = optionsPrefix or 'options'
local options, n, more = {}
repeat
ok, more = pcall( mw.text.jsonDecode, frame.args[optionsPrefix .. ( n or '' )] )
if ok and type( more ) == 'table' then
for k, v in pairs( more ) do options[k] = v end
end
n = ( n or 0 ) + 1
until not ok
return options
end
p.build_namelist = function ( template_name, sp )
local res = { template_name }
if sp then
if type( sp ) == 'string' then sp = { sp } end
for _, p in ipairs( sp ) do table.insert( res, template_name .. '/' .. p ) end
end
return res
end
return p