Jump to content

Module:Yesno/doc

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 14:10, 20 September 2013 (create). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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 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, it allows the return of a default value.

Usage

First, load the module.

local yesno = require('Module:Yesno')

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

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

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

You can specify a default value for input other than the types above. If you don't supply a default, the module will return true.

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

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

Note that the blank string also functions this way:

yesno('')        -- Returns true.
yesno('', 'bar') -- Returns "bar".

Although the blank 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 blank string as false is important for your module, you will need to remove blank arguments at an earlier stage of processing.