Module:WsBool

From CrossCode Wiki
Jump to navigation Jump to search
File:Template-info.svg Module Documentation

Checks if data exists and converts the data to a Boolean value.

Syntax

wsBool(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 {{WsBool}} instead.

<syntaxhighlight lang="lua"> local wsBool = require('Module:WsBool') </syntaxhighlight>


-- Function allowing for consistent treatment of boolean-like wikitext input.
-- It works similarly to the template {{WsBool}}.

return function(val, default)
	val = ((type(val) == 'string') and val:lower()) or val;
	if (val == nil) then
		return nil;
	elseif (
		(val == false) or
		(tonumber(val) == 0)
	) then
		return false;
	elseif (
		(val == true) or
		(tonumber(val) == 1)
	) then
		return true;
	else
		return default;	
	end
end