Module:Yesno
Dis modül transfüs stringi "yes" in true e "no" in false.
Syntax
[redakön fonäti]yesno(value, default)
value is the input value to be tested.
- Boolean input or boolean-style input (see below) always evaluates to either
trueorfalse. nilalways evaluates tonil.- Other values evaluate to
defaultdefaulting tonilif not specified.
Usage
[redakön fonäti]Call from User page
[redakön fonäti]For normal wiki pages, you can use the template {{yesno}}.
In your User page:
- Paste the following
{{yesno|yEs}} = {{yesno|true}}
- Click on
Preview
In this wiki page of documentation (you can click edit source to see the call then click Cancel), the two templates are replaced with:
yes = yes
- Click on
Cancel - Your User page remains unmodified.
Call in Debug console
[redakön fonäti]The Lua Scribunto Debug Console is a safe, interactive tool for learning Lua without installing or saving anything.
- Click
Edit sourceon theModule:Yesno page. - Do not modify the Lua code in the editor and do not click
Publish changes. - Scroll to the bottom of the page to the
Debug consolesection. - Paste the comment and call into the gray input box (above the
Clearbutton). - Press
Enter(↲) once to execute the Lua commands.
--[[How to use the Debug Console with a funny gibberish tribute to Volapük]]-- print("Menade bal – püki bal: " .. _VERSION) -- "1 mankind – 1 language:" + Lua version
The result will appear below the Lua print command line:
Menade bal – püki bal: Lua 5.1
- Press
Up arrow(↑) to cycle through previous Lua commands. - Optionally, click
Clearto clear only theDebug Console; the Lua editor above will remain unmodified.
The Module:Yesno page is an internal Lua module intended to be loaded by other Lua modules or the Debug Console.
It does not:
- declare a package table such as:
local p = {}; -- mandatory package table: the only variable with single letter - return a package table; instead it returns an anonymous
function. - declare a named function like
function p.yesno(frame).
In Debug Console: The two steps below should be pasted together as a single input.
- To load the module and name its function, run
local yesno = require('Module:Yesno') - Call the module's
local function(named the same as the module but in lowercase).
local yesno = require('Module:Yesno') print(yesno('yes') and yesno('y') and yesno('true') and yesno('1') and yesno(1) and yesno(true) and yesno('s') and yesno('si') and yesno('veratik'))
true
The local function yesno is already defined above:
print(yesno('no') or yesno('n') or yesno('false') or yesno('f') or yesno('off') or yesno('0') or yesno(0) or yesno(false) or yesno('nö') or yesno('dobälik'))
false
A nil value always returns nil:
print(yesno() and yesno(nil) and yesno('t') and yesno('on') and yesno('foo') and yesno({}) and yesno(5) and yesno('') and yesno(function() return 'nil' end))
nil
String values are converted to lower case before they are matched:
print(yesno('Yes') and yesno('YES') and yesno('yEs') and yesno('Y') and yesno('tRuE'))
true
print(yesno('No') or yesno('NO') or yesno('nO') or yesno('N') or yesno('fALsE'))
false
Optional second parameter
[redakön fonäti]You can specify a default value if yesno receives input other than that listed above. If you don't supply a default, the module will return nil for these inputs.
print(yesno(nil, true) and yesno(nil, false) and yesno(nil, 'bar'))
nil
print(yesno('foo', true) and yesno({}, true) and yesno(5, true) and yesno('', true) and yesno(function() return 'true' end, true))
true
These return the same solfège syllable "la":
local yesno = require('Module:Yesno') -- After optional "Clear", restore local yesno local lstObj = {'foo', {}, 5, '', function() return 'pitch A' end}; mw.logObject(lstObj) local res = "To a familiar ♪ tune: ♫ " for _, obj in ipairs(lstObj) do res = res .. yesno(obj, 'la') .. " " end; print(res)
table#1 { "foo", table#2 { }, 5, "", function#1, } To a familiar ♪ tune: ♫ la la la la la
Empty string
[redakön fonäti]Although the empty string usually evaluates to false in wikitext, it evaluates to true in Lua.
The module Yesno prefers the true Lua behaviour over the wikitext behaviour.
If treating the empty string as false is important for your module, you will need to convert empty strings to a value that evaluates to false before passing them to this module. In the case of arguments received from wikitext, this can be done by using Module:Arguments.
Handling nil results
[redakön fonäti]To get the binary true/false-only values, use code like:
local yesno = require('Module:Yesno') -- Restore local yesno local myVariable; local value = nil myVariable = yesno(value or false) -- When value is nil, result is false. print(myVariable) myVariable = yesno('foo') or false -- Unknown string returns nil, result is false. print(myVariable) -- Terse coding when the default is false: myVariable = yesno(value or false, false) -- if value is nil, yesno() is called with false instead; result is false. -- If value is 'foo', yesno() is called with 'foo'; 'foo' makes no sense, so the default (false) is returned. -- if value is 'yes' or 'no', yesno() is called with 'yes' or 'no'; result is true or false. print(myVariable)
false false false
local yesno = require('Module:Yesno') -- Restore local yesno local myVariable; local value = nil myVariable = yesno(value or true) -- When value is nil or false, result is true print(myVariable) myVariable = yesno('foo', true) -- Default value (here: true) applies, result is true. print(myVariable) -- Terse coding when the default is true: myVariable = yesno(value or true, true) -- if value is nil, yesno() is called with true instead; result is true. -- If value is 'foo', yesno() is called with 'foo'; 'foo' makes no sense, so the default (true) is returned. -- if value is 'yes' or 'no', yesno() is called with 'yes' or 'no'; result is true or false. print(myVariable)
true true true
Better suggestions:
local yesno = require('Module:Yesno') -- Restore local yesno local myVariable; local value = nil local myVariable = yesno(value) if myVariable == nil then -- Post-test: value is nil or an unrecognized string myVariable = true end print(myVariable)
true
local yesno = require('Module:Yesno') -- Restore local yesno -- Note: the default result has to be written twice local myVariable; local value = nil if value == nil then -- Pre-test is more efficient when value is nil, but more verbose myVariable = true -- bypass yesno else myVariable = yesno(value, true) end print(myVariable)
true
Identity function
[redakön fonäti]local yesno = require('Module:Yesno') -- #if yesno then returns its input parameter print(mw.getCurrentFrame():callParserFunction("#if", yesno("Yes"), "Yes", "no"))
Yes
- Go to the top of the
Module:Yesno page - Click
Cancelunder the Lua editor - The
Module:Yesno remains unmodified.
-- Function allowing for consistent treatment of boolean-like wikitext input.
-- It works similarly to the template {{yesno}}.
return function (val, default)
-- If your wiki uses non-ascii characters for any of "yes", "no", etc., you
-- should replace "val:lower()" with "mw.ustring.lower(val)" in the
-- following line.
val = type(val) == 'string' and val:lower() or val
if val == nil then
return nil
elseif val == true
or val == 'yes'
or val == 'y'
or val == 'true'
or val == 'si'
or val == 's'
or val == 'veratik'
or tonumber(val) == 1
then
return true
elseif val == false
or val == 'no'
or val == 'n'
or val == 'false'
or val == 'nö'
or val == 'n'
or val == 'dobälik'
or tonumber(val) == 0
then
return false
else
return default
end
end