Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
efd899b
commit 7d55e91
Showing
5 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |