Skip to content

Commit

Permalink
Add R.xor (Exclusive OR) (#2646)
Browse files Browse the repository at this point in the history
* Add xor module with respective test

* Update see docs. Change implementation of xor to return boolean type

* Added test for xor to check coercion to boolean and currying

* Update return expression on xor to make it simpler
  • Loading branch information
rferromoreno authored and CrossEye committed Feb 18, 2019
1 parent efd899b commit 7d55e91
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
2 changes: 1 addition & 1 deletion source/and.js
Expand Up @@ -12,7 +12,7 @@ import _curry2 from './internal/_curry2';
* @param {Any} a
* @param {Any} b
* @return {Any} the first argument if it is falsy, otherwise the second argument.
* @see R.both
* @see R.both, R.xor
* @example
*
* R.and(true, true); //=> true
Expand Down
1 change: 1 addition & 0 deletions source/index.js
Expand Up @@ -249,6 +249,7 @@ export { default as when } from './when';
export { default as where } from './where';
export { default as whereEq } from './whereEq';
export { default as without } from './without';
export { default as xor } from './xor';
export { default as xprod } from './xprod';
export { default as zip } from './zip';
export { default as zipObj } from './zipObj';
Expand Down
3 changes: 1 addition & 2 deletions source/or.js
@@ -1,6 +1,5 @@
import _curry2 from './internal/_curry2';


/**
* Returns `true` if one or both of its arguments are `true`. Returns `false`
* if both arguments are `false`.
Expand All @@ -13,7 +12,7 @@ import _curry2 from './internal/_curry2';
* @param {Any} a
* @param {Any} b
* @return {Any} the first argument if truthy, otherwise the second argument.
* @see R.either
* @see R.either, R.xor
* @example
*
* R.or(true, true); //=> true
Expand Down
26 changes: 26 additions & 0 deletions source/xor.js
@@ -0,0 +1,26 @@
import _curry2 from './internal/_curry2';

/**
* Exclusive disjunction logical operation.
* Returns `true` if one of the arguments is truthy and the other is falsy.
* Otherwise, it returns `false`.
*
* @func
* @memberOf R
* @category Logic
* @sig a -> b -> Boolean
* @param {Any} a
* @param {Any} b
* @return {Boolean} true if one of the arguments is truthy and the other is falsy
* @see R.or, R.and
* @example
*
* R.xor(true, true); //=> false
* R.xor(true, false); //=> true
* R.xor(false, true); //=> true
* R.xor(false, false); //=> false
*/
var xor = _curry2(function xor(a, b) {
return Boolean(!a ^ !b);
});
export default xor;
55 changes: 55 additions & 0 deletions test/xor.js
@@ -0,0 +1,55 @@
var R = require('../source');
var eq = require('./shared/eq');

describe('xor', function() {
it('compares two values with exclusive or', function() {
eq(R.xor(true, true), false);
eq(R.xor(true, false), true);
eq(R.xor(false, true), true);
eq(R.xor(false, false), false);
});

it('when both values are truthy, it should return false', function() {
eq(R.xor(true, 'foo'), false);
eq(R.xor(42, true), false);
eq(R.xor('foo', 42), false);
eq(R.xor({}, true), false);
eq(R.xor(true, []), false);
eq(R.xor([], {}), false);
eq(R.xor(new Date(), true), false);
eq(R.xor(true, Infinity), false);
eq(R.xor(Infinity, new Date()), false);
});

it('when both values are falsy, it should return false', function() {
eq(R.xor(null, false), false);
eq(R.xor(false, undefined), false);
eq(R.xor(undefined, null), false);
eq(R.xor(0, false), false);
eq(R.xor(false, NaN), false);
eq(R.xor(NaN, 0), false);
eq(R.xor('', false), false);
});

it('when one argument is truthy and the other is falsy, it should return true', function() {
eq(R.xor('foo', null), true);
eq(R.xor(null, 'foo'), true);
eq(R.xor(undefined, 42), true);
eq(R.xor(42, undefined), true);
eq(R.xor(Infinity, NaN), true);
eq(R.xor(NaN, Infinity), true);
eq(R.xor({}, ''), true);
eq(R.xor('', {}), true);
eq(R.xor(new Date(), 0), true);
eq(R.xor(0, new Date()), true);
eq(R.xor([], null), true);
eq(R.xor(undefined, []), true);
});

it('returns a curried function', function() {
eq(R.xor()(true)(true), false);
eq(R.xor()(true)(false), true);
eq(R.xor()(false)(true), true);
eq(R.xor()(false)(false), false);
});
});

0 comments on commit 7d55e91

Please sign in to comment.