“模块:Paramtest”与“模块:Yesno”:页面之间的差异

爱来自星云工艺喵!(づ。◕◡◡◕。)づ
(页面间差异)
(已保护“模块:Paramtest”([编辑=仅允许管理员](无限期)[移动=仅允许管理员](无限期)))
 
(已保护“模块:Yesno”([编辑=仅允许管理员](无限期)[移动=仅允许管理员](无限期)))
 
第1行: 第1行:
--[[
-- Function allowing for consistent treatment of boolean-like wikitext input.
{{Helper module
-- It works similarly to the template {{yesno}}.
|name=Paramtest
|fname1 = is_empty(arg)
|ftype1 = String
|fuse1 = Returns true if arg is not defined or contains only whitespace
|fname2 = has_content(arg)
|ftype2 = String
|fuse2 = Returns true if arg exists and does not only contain whitespace
|fname3 = default_to(arg1,arg2)
|ftype3 = String, Any value
|fuse3 = If arg1 exists and does not only contain whitespace, the function returns arg1, otherwise returns arg2
|fname4 = defaults{ {arg1,arg2},...}
|ftype4 = {String, Any value}...
|fuse4 = Does the same as <code>default_to()</code> run over every table passed; for technical reasons, all <code>nil</code> are replaced with <code>false</code>
}}
--]]
--
-- Tests basic properties of parameters
--


local p = {}
return function (val, default)
 
-- 如果你的维基使用非ASCII字符来表示 "是"、"否 "等等
--
-- 你应该在下面一行用 "mw.ustring.lower(val) "替换 "val:lower()"。
-- Tests if the parameter is empty, all white space, or undefined
val = type(val) == 'string' and mw.ustring.lower(val) or val
--
if val == nil then
 
return nil
function p.is_empty(arg)
elseif val == true
return not string.find(arg or '', '%S')
or val == ''
end
or val == 'yes'
 
or val == 'y'
--
or val == 'true'
-- Returns the parameter if it has any content, the default (2nd param)
or val == 't'
--
or tonumber(val) == 1
 
then
function p.default_to(arg, default)
return true
if string.find(arg or '', '%S') then
elseif val == false
return arg
or val == '否'
or val == 'no'
or val == 'n'
or val == 'false'
or val == 'f'
or tonumber(val) == 0
then
return false
else
else
return default
return default
end
end
end
end
--
-- Returns a list of paramaters if it has any content, or the default
--
function p.defaults(...)
local ret = {}
for i, v in ipairs(...) do
if string.find(v[1] or '', '%S') then
table.insert(ret,v[1])
else
-- or false, because nil is removed
table.insert(ret,v[2] or false)
end
end
return unpack(ret)
end
--
-- Tests if the parameter has content
-- The same as !is_empty, but this is more readily clear
--
function p.has_content(arg)
return string.find(arg or '', '%S')
end
--
-- uppercases first letter
--
function p.ucfirst(arg)
if not arg or arg:len() == 0 then
return nil
elseif arg:len() == 1 then
return arg:upper()
else
return arg:sub(1,1):upper() .. arg:sub(2)
end
end
--
-- uppercases first letter, lowercases everything else
--
function p.ucflc(arg)
if not arg or arg:len() == 0 then
return nil
elseif arg:len() == 1 then
return arg:upper()
else
return arg:sub(1,1):upper() .. arg:sub(2):lower()
end
end
return p

2023年9月23日 (六) 22:31的最新版本

Module documentation[view][edit][history][purge]
This documentation is transcluded from 模块:Yesno/doc. Changes can be proposed in the talk page.

Module:Yesno provides a consistent interface for processing boolean or boolean-style string input. While Lua allows the true and false boolean values, wikicode templates can only express boolean values through strings such as "yes", "no", etc. This module processes these kinds of strings and turns them into boolean input for Lua to process. It also returns nil values as nil, to allow for distinctions between nil and false. The module also accepts other Lua structures as input, i.e. booleans, numbers, tables, and functions. If it is passed input that it does not recognise as boolean or nil, it is possible to specify a default value to return.


-- Function allowing for consistent treatment of boolean-like wikitext input.
-- It works similarly to the template {{yesno}}.

return function (val, default)
	-- 如果你的维基使用非ASCII字符来表示 "是"、"否 "等等
	-- 你应该在下面一行用 "mw.ustring.lower(val) "替换 "val:lower()"。
	val = type(val) == 'string' and mw.ustring.lower(val) or val
	if val == nil then
		return nil
	elseif val == true 
		or val == '是'
		or val == 'yes'
		or val == 'y'
		or val == 'true'
		or val == 't'
		or tonumber(val) == 1
	then
		return true
	elseif val == false
		or val == '否'
		or val == 'no'
		or val == 'n'
		or val == 'false'
		or val == 'f'
		or tonumber(val) == 0
	then
		return false
	else
		return default
	end
end