in partnership with mediatemple

Ticket #653 (closed enhancement: wontfix)

Opened 6 months ago

Last modified 2 days ago

Add custom xor implementation

Reported by: zilenCe Owned by:
Type: enhancement Priority: trivial
Milestone: Mootools version 1.2 Component: Core
Keywords: Cc:

Description

Since JavaScript? lacks the xor operator it would be handy to have this function as a utility Function:

function $xor(a, b){
	return (a || b) && !(a && b);
}

Returns true if a OR b is true but not if both are
usage:

$xor(true, false) //returns true
$xor(true, true) //returns false
$xor(false, false) //returns false

Change History

Changed 6 months ago by JanK

!a != !b

Changed 6 months ago by JanK

oops, sorry forget my last comment

Changed 6 months ago by ale

It'd be more correct (and clear) like the boolean rules definitions:

function $xor	(a, b) {
		return ( (!a && b) || (a && !b) );
	}

Changed 5 months ago by kamicane

  • milestone changed from Mootools version 1.2 to Mootools version 1.3

Changed 5 months ago by anonymous

There are a few ways to do this that would make more sense than creating a new function. Javascript has a bitwise XOR operator and != essentially works as a XOR operator when dealing with booleans.

true ^ true = 0
true ^ false = 1
true != true = false
true != false = true

Now when you're not dealing with booleans, you just need to negate the expressions.

Changed 5 months ago by mattcoz

oops, wasn't logged in, that last comment was me

Changed 3 months ago by JanK

I don't think it's needed in MooTools, but here is a short version which converts the arguments to boolean before comparing them.

function $xor(a,b){return !a != !b;};
console.log($xor(0,0)); // false
console.log($xor(0,1)); // true
console.log($xor(1,0)); // true
console.log($xor(1,1)); // false

Changed 2 days ago by tomocchino

  • status changed from new to closed
  • resolution set to wontfix
  • milestone set to Mootools version 1.2

since it's such a simple function and seems to only be used in truly rare situations, im going to close this ticket. i don't think this needs to be part of the core

Note: See TracTickets for help on using tickets.