Jump to content

Module:Yesno

Kubuya ku Wikipedia

Lomodule iguqula intambo ethi "yes" ibe yi-true kanye nethi "no" ibe yi-false.

yesno(value, default)

value is the input value to be tested.

  1. Boolean input or boolean-style input (see below) always evaluates to either true or false.
  2. nil always evaluates to nil.
  3. Other values evaluate to default defaulting to nil if not specified.

Call from User page

[Hlela umthombo]

For normal wiki pages, you can use the template {{yesno}}.

In your User page:

  1. Paste the following
{{yesno|yEs}} = {{yesno|true}}
  1. Click on Preview

In this wiki page of documentation (you can click edit to see the call then click Cancel), the two templates are replaced with:

yes = yes

  1. Click on Cancel
  2. Your User page remains unmodified.

Call in Debug console

[Hlela umthombo]

The Lua Scribunto Debug Console is a safe, interactive tool for learning Lua without installing or saving anything.

  1. Click Edit source on the Module:Yesno page.
  2. Do not modify the Lua code in the editor and do not click Publish changes.
  3. Scroll to the bottom of the page to the Debug console section.
  4. Paste the comment and call into the gray input box (above the Clear button).
  5. Press Enter (↲) once to execute the Lua commands.
--[[How to use the Debug Console with Hello World-like greetings]]--
print("Sawubona " .. _VERSION) -- Concatenate the Lua version with `..`

The result will appear below the Lua print command line:

Sawubona Lua 5.1
  1. Press Up arrow (↑) to cycle through previous Lua commands.
  2. Optionally, click Clear to clear only the Debug 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:

  1. declare a package table such as: local p = {}; -- mandatory package table: the only variable with single letter
  2. return a package table; instead it returns an anonymous function.
  3. declare a named function like function p.yesno(frame).

In Debug Console: The two steps below should be pasted together as a single input.

  1. To load the module and name its function, run local yesno = require('Module:Yesno')
  2. 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('t') and yesno('on')) -- and yesno('yebo'))
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('cha'))
false

A nil value always returns nil:

print(yesno() and yesno(nil) 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')) -- and yesno('Yebo'))
true
print(yesno('No') or yesno('NO') or yesno('nO') or yesno('N') or yesno('fALsE')) -- or yesno('Cha'))
false


Optional second parameter

[Hlela umthombo]

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

[Hlela umthombo]

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

[Hlela umthombo]

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

[Hlela umthombo]
local yesno = require('Module:Yesno') -- #if yesno then returns its input parameter
print(mw.getCurrentFrame():callParserFunction("#if", yesno("Yes"), "Yes", "no"))
-- print(mw.getCurrentFrame():callParserFunction("#if", yesno("Yebo"), "Yebo", "cha"))
Yes
  1. Go to the top of the Module:Yesno page
  2. Click Cancel under the Lua editor
  3. 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 == 't'
		or val == 'on'
		or tonumber(val) == 1
	then
		return true
	elseif val == false
		or val == 'no'
		or val == 'n'
		or val == 'false'
		or val == 'f'
		or val == 'off'
		or tonumber(val) == 0
	then
		return false
	else
		return default
	end
end