Module talk:Yesno

From Feelin' Blue Wiki
Latest comment: 27 September by Aidan9382 in topic Edit request 25 September 2025
Jump to navigationJump to search

Template:Permprot Template:Copied

Testcases and documentation

[edit source]
{| class="wikitable"
|+ Test Cases (via [[Template:Yesno/doc]])
|-
! Module output
! Template output
! Expected
|-
| {{#invoke:Yesno|yesno|yes}}
| {{yesno|yes}}
| yes
|-
| {{#invoke:Yesno|yesno|YeS}}
| {{yesno|YeS}}
| yes
|-
| {{#invoke:Yesno|yesno|Y}}
| {{yesno|Y}}
| yes
|-
| {{#invoke:Yesno|yesno|y}}
| {{yesno|y}}
| yes
|-
| {{#invoke:Yesno|yesno|no}}
| {{yesno|no}}
| ''(blank)''
|-
| {{#invoke:Yesno|yesno|}}
| {{yesno|}}
| ''(blank)''
|-
| {{#invoke:Yesno|yesno}}
| {{yesno}}
| ''(blank)''
|-
| {{#invoke:Yesno|yesno|¬}}
| {{yesno|¬}}
| ''(blank)''
|-
| {{#invoke:Yesno|yesno|purplemonkeydishwasher}}
| {{yesno|purplemonkeydishwasher}}
| yes
|-
| {{#invoke:Yesno|yesno|yes|yes=bacon}}
| {{yesno|yes|yes=bacon}}
| bacon
|-
| {{#invoke:Yesno|yesno|no|no=ham}}
| {{yesno|no|no=ham}}
| ham
|-
| {{#invoke:Yesno|yesno|blank= eggs}}
| {{yesno|blank= eggs}}
| 
|-
| {{#invoke:Yesno|yesno|no=ham}}
| {{yesno|no=ham}}
| 
|-
| {{#invoke:Yesno|yesno|blank= eggs|no=ham}}
| {{yesno|blank= eggs|no=ham}}
| 
|-
| {{#invoke:Yesno|yesno|¬|¬=sausage}}
| {{yesno|¬|¬=sausage}}
| sausage
|-
| {{#invoke:Yesno|yesno|purplemonkeydishwasher|def=cup-of-tea}}
| {{yesno|purplemonkeydishwasher|def=cup-of-tea}}
| cup-of-tea
|-
| {{#invoke:Yesno|yesno|purplemonkeydishwasher|yes=bacon}}
| {{yesno|purplemonkeydishwasher|yes=bacon}}
| bacon
|-
| {{#invoke:Yesno|yesno|purplemonkeydishwasher|def=cup-of-tea|yes=bacon}}
| {{yesno|purplemonkeydishwasher|def=cup-of-tea|yes=bacon}}
| cup-of-tea
|-
| {{#invoke:Yesno|yesno|yes|yes=no|no=yes}}
| {{yesno|yes|yes=no|no=yes}}
| no
|-
| {{#invoke:Yesno|yesno|no|yes=no|no=yes}}
| {{yesno|no|yes=no|no=yes}}
| yes
|-
| {{#invoke:Yesno|yesno| no |no=no}}
| {{yesno| no |no=no}}
| no
|}
Note: Pre tags added to suppress script errors. — Mr. Stradivarius ♪ talk ♪ 14:25, 20 September 2013 (UTC)Reply

As you can see, the template does not behave exactly as its documentation claims it does. I was not sure which to regard as authoritative: the ideal or actual implementation. I ultimately went with the former. Removing and not any from the Lua code will make the module behave like exactly like the template.

--Ori.livneh (talk) 10:46, 28 February 2013 (UTC)Reply

Just needs to handle spaces in the params better. -- WOSlinker (talk) 11:41, 2 March 2013 (UTC)Reply
Thanks. Fixed. I moved your test case to the table above. --Ori.livneh (talk) 05:58, 3 March 2013 (UTC)Reply
Yesno has ~8M transclusions. No question it should handle the legacy 1:1. Test it. -DePiep (talk) 22:31, 11 March 2013 (UTC)Reply

The documentation is wrong. The "egg" example assumes thet the input value is blank. But in the code examples the input parameter is omitted. Compare:

  • {{yesno|blank= eggs|no=ham}} →
  • {{yesno||blank= eggs|no=ham}} → eggs

This is what is actually documented:

  • {{yesno|blank= eggs|no=ham|¬=bread}} → bread
I have changed the documentation. Not the table above. -DePiep (talk) 13:16, 23 March 2013 (UTC)Reply

Why?

[edit source]

{{yesno}} is quite simple, so I'm not sure if there is a point in replacing it with functionally equivalent Lua. There are many cases where Lua can be much faster and easier to read, but I'm not sure that is the case here. In fact, because of the slight overhead with #invoke, simple parser functions can actually be slightly faster than the equivalent Lua. I haven't tested that here, but I wouldn't be surprised if there is no performance advantage in a case like this. Seems like a strange template to target for Lua conversion. Dragons flight (talk) 14:57, 12 March 2013 (UTC)Reply

It’s just for compatibility. For some weird reason, yesno might not work and this will.

E Super Maker (😲 shout) 15:47, 23 November 2019 (UTC)Reply

test failed

[edit source]

Template:Resolved One fail found in unit test: value "1= ¬ " (with whitespace). Reason:whitespace not removed before this check. Possible solution; treat '¬' as a character, separate from nil. (if val == nil or val == '¬' then ...). -DePiep (talk) 11:54, 23 March 2013 (UTC)Reply

also fails when 1=0.0
Suggestion: change val = args[1] into val = tostring(args[1]). To prevent treating "000" as number. -DePiep (talk) 14:09, 23 March 2013 (UTC)Reply

Solved. Module is changed, although the numeric issue different from my (incorrect) suggestion. -DePiep (talk) 14:19, 23 March 2013 (UTC)Reply

Thanks for putting the test cases up. I learned something new about ParserFunctions. :) Still seems to be slower than the old template version though. — Mr. Stradivarius ♪ talk ♪ 14:38, 23 March 2013 (UTC)Reply

Null?

[edit source]

The code now says no = "<!-- null -->". But a "" (nullstring) is not the same as ASCII NULL (0x0000). Also I see no use for adding wiki comment to code this way. -DePiep (talk) 17:47, 23 March 2013 (UTC)Reply

Yes, I agree that it doesn't really make sense. It's just in there to be 100% backwards-compatible with the current template. Those html comments should really have been removed from {{yesno}} when it was made subst-able, which would have prevented us from having to second-guess whether they actually affect the output of any substitutions in any meaningful way. I suppose it's possible to download a database dump and to run a search on it to find out, but that's probably a bit much for my poor ailing laptop. Or we could just remove the comments and hope nothing breaks - I'm fairly sure that no-one would have written any template code that purposefully used them. There's no way to be sure that there is no code that was inadvertently written to be dependent on the error, though, and that worries me. — Mr. Stradivarius ♪ talk ♪ 18:24, 23 March 2013 (UTC)Reply
Drop it. Come what may. -DePiep (talk) 00:17, 24 March 2013 (UTC)Reply

Rewrite

[edit source]

I've rewritten this as a pure Lua function. The new version is a lot simpler, and is also designed to work with Lua data types such as booleans, tables, and functions. Dragons flight's criticism above is well-founded, and giving up on the idea of replacing {{yesno}} seemed like the best answer. The new version should hopefully retain the basics of {{yesno}} while playing well with Lua, and allow meta-modules that deal with boolean-style wikitext input to be consistent with each other. (In other words, users won't have to remember whether it was "category=yes" or "category=true" - it should Just WorkTM.) — Mr. Stradivarius ♪ talk ♪ 14:37, 20 September 2013 (UTC)Reply

All testcases failed

[edit source]

is there some issue with the module, all tests on the Module talk:Yesno/testcases has failed. --Jarekt (talk) 16:37, 20 December 2013 (UTC)Reply

Protected edit request on 24 February 2014

[edit source]

Template:Edit protected the information is deceptive

please delete 2600:1013:B018:986F:4493:7A2A:52E2:103A (talk) 04:21, 24 February 2014 (UTC)Reply

File:Red question icon with gradient background.svg Not done: please be more specific about what needs to be changed. Note that this is the talk page for Module:Yesno and there is no information here. What information on which page do you think is deceptive? — {{U|Technical 13}} (tec) 04:27, 24 February 2014 (UTC)Reply

New version

[edit source]

I've put a new version of this module in the sandbox. The new version uses the native Lua string.lower function instead of the Scribunto mw.ustring.lower function. According to my tests it is about 25x faster this way. Would anyone object to me updating the module? — Mr. Stradivarius ♪ talk ♪ 15:22, 7 April 2014 (UTC)Reply

I see no issue with it. Jackmcbarn (talk) 18:57, 7 April 2014 (UTC)Reply
The new version is live as of the 18th. — Mr. Stradivarius ♪ talk ♪ 12:33, 22 April 2014 (UTC)Reply

Doc questions (blank string true/false)

[edit source]
  1. In the documentation, I changed the "nil" example into singular. Or are there are other forms of nil covered and to be documented? (uppercase?, a non-initialised variable?).
  2. The last paragraph is about the '' value (blank string). It says:
Although the blank string usually evaluates to false in wikitext, it evaluates to true in Lua. Is this correct here?
According to this same documentation, it is treated as "not a boolean". It says it returns the default (parameter) value, not the 'true' value. That is: not recognised as a boolean, iic. And elsewhere the doc says "yesno('') returns nil". (I did not test any). -DePiep (talk) 09:06, 8 April 2014 (UTC)Reply
On nil: there is only one kind of nil. However, there may be many variables in any given module whose value may be nil, hence "nil values". There is no "uppercase nil", and in Lua there is no such thing as a non-initialised variable, as variables are initialised when they are first used. On the blank string: saying that it "evaluates to true" is talking about when it is used in if/then statements or with the and and or operators. For example, the following code will return true:
if '' then
	return true
else
	return false
end
Does that clear things up? — Mr. Stradivarius ♪ talk ♪ 09:26, 8 April 2014 (UTC)Reply
Does it clear things up? Don't know. Documentation did not change. -DePiep (talk) 12:10, 8 April 2014 (UTC)Reply
I mean, do you understand it now? — Mr. Stradivarius ♪ talk ♪ 12:34, 8 April 2014 (UTC)Reply

Render T as true as well?

[edit source]

I have seen some applications use T as an option for true as well. It might, therefore, be useful to have the letter T (t) represent a true value as well. CRwikiCA talk 15:15, 14 April 2015 (UTC)Reply

Request

Template:Edit protected I request a change to the module to recognize T or t and F or f as true and false entries. This would make the full code the following:

-- 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 tonumber(val) == 1
	then
		return true
	elseif val == false
		or val == 'no'
		or val == 'n'
		or val == 'false'
		or val == 'f'
		or tonumber(val) == 0
	then
		return false
	else
		return default
	end
end

Where lines 15 and 23 are new compared to the current code. CRwikiCA talk 18:17, 23 April 2015 (UTC)Reply

No opposition, so Template:Done — Martin (MSGJ · talk) 10:43, 27 April 2015 (UTC)Reply
I would be interested to know cases where such "t/f" values may be encountered. I know that in command prompts we may encounter "y/n" choices. But I cannot recall having ever encountered "t/f".
Refs T137584 (« Allow Scribunto code to add a category without changing output »), which would allow to know on what (and how many) pages these values are actually used.
Od1n (talk) 00:05, 20 October 2024 (UTC)Reply

Support on/off detection

[edit source]

Template:Edit fully-protected Please add support for Template:Kbd and Template:Kbd values (as equivalents of Template:Kbd and Template:Kbd, respectively), for consistency with the regular-template version {{Yesno}}, per RfC at Template talk:Yesno#Support on/off detection.  — SMcCandlish ¢ 😼  23:44, 22 February 2018 (UTC)Reply

Very simple change, but i've sandboxed the change and ran the testcases.. Galobtter (pingó mió) 07:52, 25 February 2018 (UTC)Reply
File:Yes check.svg Done — Martin (MSGJ · talk) 12:14, 28 February 2018 (UTC)Reply
Thank you Template:U, for the coding work (Lua gives me a headache), and Template:U, for just getting it implemented, instead of either of you being a thorn in the side about it like several parties were for no defensible reason at the RfC. I've never encountered so much "resist sensible change just because I feel like it" nonsense on a minor technical matter as I did in this particular case. It was weird, verging on inexplicable.  — SMcCandlish ¢ 😼  18:37, 26 May 2018 (UTC)Reply

Upgrade of protection level

[edit source]

I believe we should make this template editor protected (I've seen templates with way less of the amount of transclusions than this one being template protected). Just a suggestion before I would put it up in the list. KylieInTheSkylie (talk) 02:23, 6 April 2020 (UTC)Reply

Just realized that full protection is not Extended confirmed protection. Please disregard this. KylieInTheSkylie (talk) 02:23, 6 April 2020 (UTC)Reply

Multilingual version?

[edit source]

What about a multilingual version? Would there be any conflicts? Trigenibinion (talk) 17:21, 12 March 2021 (UTC)Reply

It seems this is normally called the same in other languages, where they appear to tend to support English as well as the translation, so this should be usable in a multilingual module. Trigenibinion (talk) 17:57, 12 March 2021 (UTC)Reply
There is no need for a multilingual version of this module to exist on this wiki, since its content is in only one language. Furthermore, were one created, it would be a recreation of Module:YesnoTNT, which was deleted per Wikipedia:Templates for discussion/Log/2019 December 11#Module:YesnoTNT, and I would move for it to be speedy deleted per WP:CSD#G4 * Pppery * it has begun... 20:36, 13 March 2021 (UTC)Reply

Option: Binary-only?

[edit source]

I want to "propose" to add an option (feature) that makes the module return T/F only, never nil. For example, it could have para |whennil=<True or False>.

The background is that there are many situations (I meet in programming Lua) where a 'nil' return value is not helpful and still has to be handled separately into binary logic.

But even better, maybe I can learn why this was not added from the start, or why it could be considered bad design/coding. Maybe it could be solved by creating a wrapper like "Module:YesnoNevernil", or is that bad design again? -DePiep (talk) 04:56, 23 December 2021 (UTC)Reply

It's easy enough to just handle the nil in your own code, for example: myvariable = yesno(value) or false -- WOSlinker (talk) 20:09, 23 December 2021 (UTC)Reply
This can also mostly be handled by specifying the default parameter (i.e. myvariable = yesno(value, false)), although this does leave the case yesno(nil, false), which evaluates to nil. The reason I originally programmed the module this way is so that it can be mostly backwards-compatible with Template:Yesno, which it is intended to replace. Originally the plan was to do a straight conversion of Template:Yesno to Lua, but it turned out that doing that was actually slower than just using the template, given the template's simplicity and the initial cost of switching between PHP and Lua. So instead this module became Lua-only, with most of the behaviour of Template:Yesno. The intention there was that module writers could easily incorporate it into modules that convert more complex templates without having to change the template logic too much. — Mr. Stradivarius ♪ talk ♪ 22:11, 23 December 2021 (UTC)Reply
OK, thx. I can use this code solution with confidence now, no research-&-test needed. Might as well add it to the /doc. -DePiep (talk) 16:35, 24 December 2021 (UTC)Reply
The abovementioned yesno(value) or true/false has a common pitfall and should be avoided: if value is "false", yesno(value) or true would gives "true", instead of the presumably expected "false". See section « Standard solution: and/or » on this page. Od1n (talk) 18:15, 22 September 2024 (UTC)Reply

Edit request 25 September 2025

[edit source]

Template:Edit fully-protected

Description of suggested change: Please merge Module:Yesno/sandbox. Uses a lookup table instead of an if-elseif-else statment which is almost 50% as fast as a lookup table as table lookups in Lua are O(1) average case.

I made a benchmark comparing exact current module with the new one: Module:Sandbox/Waddie96. To run, go into edit mode, in the LuaConsole type =p.main(). Output compares the two with 500,000 iterations. My output showed almost 30–50% speed improvement which is a lot for a module transcluded on 31,723,438 pages.

=p.main() Running benchmarks with 500000 iterations... Lookup table took 3.6897 seconds for 8500000 operations If-elseif chain took 5.0883 seconds for 8500000 operations Diff: Diff here.


waddie96 ★ (talk) 01:36, 25 September 2025 (UTC)Reply

So you're saving about 165 nanoseconds per call, and it's unlikely this is getting called so many thousands of times that that really adds up? That's not terribly convincing. OTOH, other than oddly assigning TO_NUMBER = tonumber and TYPE = type without any explanation, it seems like an ok increase in clarity to have almost all the accepted values together at the top. You might go a little further and stick boolean true and false into BOOLEAN_MAP and skip checking types at all, as Lua doesn't do any type conversion on table keys and allows anything except nil and NaN. Anomie 01:58, 25 September 2025 (UTC)Reply
WP:PERF. Probably the amount of time it would take for the parser to reparse almost every page on the project triggered by this edit would greatly exceed the time saved for the forseeable future. And the code as is seems plenty clear enough, so I'm not seeing the clarity argument either. I'm contesting this request. * Pppery * it has begun... 02:25, 25 September 2025 (UTC)Reply
This benchmark case isn't perfectly representitive. Generally, the average template call or invoke from a page will use Yesno just a few times, repeated among a bunch of different template calls. That means, if a singular call ends up using YesNo ~5 times or less in total, the lookup table version technically has more overhead than the current simplistic implementation (upwards of ~2x slower, though that means little at this scale), due to the time it takes to create the lookup table and localise the globals every time. Also, as mentioned above, this is fighting to save milliseconds (if even that much) in the wrong place - it definitely won't be fixing any multi-second or 10s timeout pages, which is where optimisations matter. Aidan9382 (talk) 08:19, 25 September 2025 (UTC)Reply
Yes, you make an excellent point, so lets break it down: The economics of optimization at scale:
Saving time or indefinite use of the slower iteration?
Module:Yesno/sandbox refactored to use a lookup table (BOOLEAN_MAP) → O(1), instead of nested if-elseif-else statements. This reduces the number of operations per call from ~7 to ~2.
If speedup per call is ~70%, you only need a few pageviews per page to amortize the reparse.
Impact on Wikimedia at scale:
  • Pages affected: ~32 million transclusions
  • Estimated calls per year: ~32 billion
  • Aggregate operations saved: ~160 billion operations/year
  • Speedup per call: 71%
  • Even if we consider just one pageview per page, the benefit already exceeds the cost.
Ongoing benefit:
That doesn't only affect the next child down that uses this module, but:
  • Every single page view that uses this module gets the speedup
  • Every edit that triggers reparsing gets the speedup
  • Every template expansion gets the speedup
Savings (rough estimates):
  • Time saved per CPU: ~80 seconds/year (per CPU handling pageviews)
  • Real-world effect: reduced CPU load, faster page loads for millions of pageviews
Break-even on one-time reparse cost:
  • Only 2–3 pageviews per transcluded page are required to offset the one-time parsing cost
  • For Wikimedia’s scale (~4,500 views/second), the break-even is achieved in ~3 hours for the first affected pagesTemplate:Efn
Technical benefits:
  • O(1) lookup vs O(n) linear search
  • Function caching avoids repeated global access overhead
  • Cleaner, more maintainable code
  • Adding new values now only requires updating the lookup table
This optimization demonstrates how small changes in module logic can lead to aggregate performance improvements.
waddie96 ★ (talk) 05:44, 27 September 2025 (UTC)Reply
@Aidan9382 I'm happy to fix those latter cases too going forward, point me in the right direction 😄 waddie96 ★ (talk) 05:46, 27 September 2025 (UTC)Reply
Some comments:
  • The point is that there is no way to "fix" this, because nothing's broken. At the end of the day, you are trying to optimise 14 equality checks. Infact, the time it takes to require this module after it's been cached is longer than an average-case use of the if-else chain function. Even if this would technically improve the performance to the degree of nanoseconds on a bunch of pages (which isn't guaranteed per below), that is the wrong place to be looking for optimisations as a wikipedia editor. The focus should be on pages that spend a significant amount of time (upwards of multiple seconds) on lua evaluation, since that's when it can start to disrupt editing and reading, which is what we care about.
  • Template:Tq is best case scenario, and as I've described above, is not always the case. Let's say you have a page that has {{T1|param=yes}}{{T2|param=no}} on it, and that each of these template calls eventually call into this module to check the value of param. Since both will create seperate lua states, both have to complete the initial uncached require on this module. That time increases significantly when adding a table creation, to the point where this specific setup would have Yesno spending ~2x more time executing than the current version.
  • The idea of Template:Tq is incorrect - the evaluated state of pages are cached, and the majority of views will not cause a reevaluation, so the scale that you suggest this improvement is running at isn't accurate. Also, Template:Tq is essentially the prior two points reiterated.
Aidan9382 (talk) 12:21, 27 September 2025 (UTC)Reply

Template:Notelist-talk