Anonymous user
Module:Yesno/doc: Difference between revisions
m
Adapt for this wiki
(→Module Quality: Use {{ModuleQuality}}) |
imported>FrozenPlum m (Adapt for this wiki) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 1:
{{Documentation subpage}}
<noinclude><!--▼
<!--
▲<translate><!--T:1--> This module provides a consistent interface for processing boolean or boolean-style string input.</translate>
▲<translate><!--T:2--> While Lua allows the <tvar name=1><code>true</code></tvar> and <tvar name=2><code>false</code></tvar> boolean values, wikicode templates can only express boolean values through strings such as <tvar name=3>"1"</tvar>, <tvar name=4>"0"</tvar>, <tvar name=5>"yes"</tvar>, <tvar name=6>"no"</tvar>, etc.</translate>
▲<translate><!--T:3--> This module processes these kinds of strings and turns them into boolean input for Lua to process.</translate>
▲<translate><!--T:4--> It also returns <tvar name=1><code>nil</code></tvar> values as <tvar name=1><code>nil</code></tvar>, to allow for distinctions between <tvar name=1><code>nil</code></tvar> and <tvar name=2><code>false</code></tvar>.</translate>
▲<translate><!--T:5--> The module also accepts other Lua structures as input, i.e. booleans, numbers, tables, and functions.</translate>
▲<translate><!--T:6--> If it is passed input that it does not recognise as boolean or <tvar name=1><code>nil</code></tvar>, it is possible to specify a default value to return.</translate>
▲== Syntax == <!--T:33-->
<syntaxhighlight lang="lua">
yesno(value, default)
</syntaxhighlight>
== Usage ==
▲<translate><!--T:11--> First, load the module. Note that it can only be loaded from other Lua modules, not from normal wiki pages.</translate>
<syntaxhighlight lang="lua">
Line 41 ⟶ 25:
</syntaxhighlight>
<syntaxhighlight lang="lua">
--
yesno('yes')
yesno('y')
Line 54 ⟶ 38:
yesno(true)
--
yesno('no')
yesno('n')
Line 63 ⟶ 47:
yesno(false)
--
yesno(nil)
</syntaxhighlight>
String values are converted to lower case before they are matched:
<syntaxhighlight lang="lua">
--
yesno('Yes')
yesno('YES')
Line 78 ⟶ 59:
yesno('tRuE')
--
yesno('No')
yesno('NO')
Line 86 ⟶ 67:
</syntaxhighlight>
<syntaxhighlight lang="lua">
--
yesno('foo')
yesno({})
Line 96 ⟶ 77:
yesno(function() return 'This is a function.' end)
--
yesno('foo', true)
yesno({}, true)
Line 102 ⟶ 83:
yesno(function() return 'This is a function.' end, true)
--
yesno('foo', 'bar')
yesno({}, 'bar')
Line 108 ⟶ 89:
yesno(function() return 'This is a function.' end, 'bar')
</syntaxhighlight>
<!--T:26-->
Note that the blank string also functions this way:
<syntaxhighlight lang="lua">
yesno('') --
yesno('', true) --
yesno('', 'bar') --
</syntaxhighlight>
<includeonly>{{Sandbox other||▼
<!-- Template categories/interwikis here -->
[[Category:Modules]]
|