Roovet Articles

Module:Yesno

This module 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.

Syntax

yesno(value, default)

value is the value to be tested. Boolean input or boolean-style input (see below) always evaluates to either true or false, and nil always evaluates to nil. Other values evaluate to default.

Usage

First, load the module. Note that it can only be loaded from other Lua modules, not from normal wiki pages. For normal wiki pages you can use {{yesno}} instead.

local yesno = require('Module:Yesno')

Some input values always return true, and some always return false. nil values always return nil.

-- These always return true:
yesno('yes')
yesno('y')
yesno('true')
yesno('t')
yesno('on')
yesno('1')
yesno(1)
yesno(true)

-- These always return false:
yesno('no')
yesno('n')
yesno('false')
yesno('f')
yesno('off')
yesno('0')
yesno(0)
yesno(false)

-- A nil value always returns nil:
yesno(nil)
yesno()

String values are converted to lower case before they are matched:

-- These always return true:
yesno('Yes')
yesno('YES')
yesno('yEs')
yesno('Y')
yesno('tRuE')

-- These always return false:
yesno('No')
yesno('NO')
yesno('nO')
yesno('N')
yesno('fALsE')

Undefined input ('foo')

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.

-- These return nil:
yesno(nil)
yesno('foo')
yesno({})
yesno(5)
yesno('')
yesno(function() return 'This is a function.' end)
yesno(nil, true)
yesno(nil, 'bar')

-- These return true:
yesno('foo', true)
yesno({}, true)
yesno(5, true)
yesno('', true)
yesno(function() return 'This is a function.' end, true)

-- These return "bar":
yesno('foo', 'bar')
yesno({}, 'bar')
yesno(5, 'bar')
yesno('', 'bar')
yesno(function() return 'This is a function.' end, 'bar')

Although the empty string usually evaluates to false in wikitext, it evaluates to true in Lua. This module prefers the 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

By definition:

yesno(nil)         -- Returns nil.
yesno('foo')       -- Returns nil.
yesno(nil, true)   -- Returns nil.
yesno(nil, false)  -- Returns nil.
yesno('foo', true) -- Returns true.

To get the binary true/false-only values, use code like:

myvariable = yesno(value or false) -- When value is nil, result is false.
myvariable = yesno(value or true)  -- When value is nil, result is true. (XXX: when value is false, result is true...)
myvariable = yesno('foo') or false  -- Unknown string returns nil, result is false.
myvariable = yesno('foo', true) or false  -- Default value (here: true) applies, result is true.

Better suggestions:

local myvariable = yesno(value)
if myvariable == nil then -- value is nil or an unrecognized string
    myvariable = true
end

-- more efficient when value is nil, but more verbose
-- (note the default result has to be written twice)
local myvariable
if value == nil then
    myvariable = true
else
    myvariable = yesno(value, true)
end

-- Module:Yesno (mw-safe)
-- Works with or without mw.ustring/mw.text

local p = {}

-- Safe helpers (use mw.* if available, else pure Lua)
local has_mw = type(mw) == 'table'
local lower = has_mw and mw.ustring and function(s) return mw.ustring.lower(s) end
                      or function(s) return string.lower(s) end

local trim = has_mw and mw.text and function(s) return mw.text.trim(s) end
                     or function(s)
                          s = tostring(s or '')
                          s = s:gsub('^%s+', ''):gsub('%s+$', '')
                          return s
                        end

local truthy = { ['y']=true, ['yes']=true, ['true']=true, ['t']=true, ['on']=true, ['1']=true }
local falsy  = { ['n']=true, ['no']=true,  ['false']=true, ['f']=true, ['off']=true, ['0']=true }

local function normalize(v)
  if type(v) == 'string' then
    v = lower(trim(v))
  end
  return v
end

function p._yesno(val, default)
  val = normalize(val)
  if type(val) == 'boolean' then return val end
  if type(val) == 'number' then
    if val == 1 then return true elseif val == 0 then return false end
  end
  if type(val) == 'string' then
    if truthy[val] then return true end
    if falsy[val]  then return false end
  end
  return default
end

-- Optional: a wikitext-friendly entrypoint that returns "yes"/"no" (not boolean),
-- so you can test with {{#invoke:Yesno|main| value | default=no }}
function p.main(frame)
  local args = (type(frame.getParent) == 'function' and frame:getParent() and frame:getParent().args) or frame.args or {}
  local value   = args[1] or args.value
  local default = args.default
  local d = (default ~= nil) and p._yesno(default) or nil
  local out = p._yesno(value, d)
  if out == true then return 'yes' end
  if out == false then return 'no' end
  return ''
end

setmetatable(p, { __call = function(_, ...) return p._yesno(...) end })
return p