Jump to content

Module:Boolean

From Humanipedia


Allows for boolean operations not easily compatible with {{#ifexpr:...}}.

Usage[edit source]

{{#invoke:Boolean|operator|value1|value2|n=number_of_expected_parameters|true=result_if_condition_is_true|false=result_if_condition_is_false}} (remove value2 for negation operations)

For AND, prints the value of "true" if both the first and second parameters are filled, prints the parameter of "false" or nothing otherwise.

For OR, prints the value of "true" if the first or second parameters are filled, prints the parameter of "false" or nothing otherwise.

For NOT, prints the value of "true" if the first parameter is empty, prints the parameter of "false" or nothing otherwise.

For XOR, prints the value of "true" if either the first or the second parameters are filled (but not both), prints the parameter of "false" or nothing otherwise.

For NAND, NOR, and XNOR, prints the value of "true" if the AND, OR, or XOR condition is not satisfied, prints the parameter of "false" or nothing otherwise.

Examples[edit source]

AND[edit source]

  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.

OR[edit source]

  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.

NOT[edit source]

  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.

XOR[edit source]

  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.

NAND[edit source]

  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.

NOR[edit source]

  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.

XNOR[edit source]

  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.
  • Lua error in package.lua at line 80: module 'Module:Yesno' not found.



local p = {}
local makeInvokeFunc = require("Module:MakeInvokeFunc")

local function notEmpty(t)
	return (t ~= nil and t ~= "" and t ~= 0 and t ~= false)
end


p._and = function(args)
	local allTrue = true
	local top = tonumber(args['n']) or 2
	for i = 1, top do
		if args[i] == nil then args[i] = false end
		allTrue = allTrue and notEmpty(args[i])
	end
	if allTrue then return args['true'] or 'true' end
	return args['false'] or ''
end

p._or = function(args)
	local oneTrue = false
	local top = tonumber(args['n']) or 2
	for i = 1, top do
		if args[i] == nil then args[i] = false end
		oneTrue = oneTrue or notEmpty(args[i])
	end
	if oneTrue then return args['true'] or 'true' end
	return args['false'] or ''
end

p._not = function(args)
	if not notEmpty(args[1]) then return args['true'] or 'true' end
	return args['false'] or ''
end

p._xor = function(args)
	local xorResult = false
	local top = tonumber(args['n']) or 2
	for i = 1, top do
		if args[i] == nil then args[i] = false end
		xorResult = xorResult ~= notEmpty(args[i])
	end
	if xorResult then return args['true'] or 'true' end
	return args['false'] or ''
end

p._nand = function(args)
	local args2 = {}
	for k,v in pairs(args) do
		if (tonumber(k) ~= nil) then
			args2[tonumber(k)] = v
		end
	end
	mw.logObject(args2)
	return p._not({ p._and({ args2[1], args2[2] }), ['true'] = args['true'], ['false'] = args['false'] })
end

p._nor = function(args)
	local args2 = {}
	for k,v in pairs(args) do
		if (tonumber(k) ~= nil) then
			args2[tonumber(k)] = v
		end
	end
	mw.logObject(args2)
	return p._not({ p._or(args2), ['true'] = args['true'], ['false'] = args['false'] })
end

p._xnor = function(args)
	local args2 = {}
	for k,v in pairs(args) do
		if (tonumber(k) ~= nil) then
			args2[tonumber(k)] = v
		end
	end
	mw.logObject(args2)
	return p._not({ p._xor(args2), ['true'] = args['true'], ['false'] = args['false'] })
end

p._main = function(args)
	local args2 = {}
	for k,v in pairs(args) do
		if tonumber(k) ~= nil then
			if tonumber(k) ~= 1 then
				args2[tonumber(k) - 1] = v
			end
		end
	end
	args2['true'] = args['true']
	args2['false'] = args['false']
	return p["_" .. args[1]](args2)
end

p["and"] = makeInvokeFunc(p)("_and")
p.AND = p["and"]
p["&&"] = p["and"]
p["or"] = makeInvokeFunc(p)("_or")
p.OR = p["or"]
p["||"] = p["or"]
p["!!"] = p["or"]
p["not"] = makeInvokeFunc(p)("_not")
p.NOT = p["not"]
p["!"] = p["not"]
p["xor"] = makeInvokeFunc(p)("_xor")
p.XOR = p["xor"]
p["^^"] = p["xor"]
p["nand"] = makeInvokeFunc(p)("_nand")
p.NAND = p["nand"]
p["nor"] = makeInvokeFunc(p)("_nor")
p.NOR = p["nor"]
p["xnor"] = makeInvokeFunc(p)("_xnor")
p.XNOR = p["xnor"]
p["main"] = makeInvokeFunc(p)("_main")

return p