Skip to content

Commit

Permalink
feat(logic): Implement nand
Browse files Browse the repository at this point in the history
  • Loading branch information
dawehner committed Jun 12, 2019
1 parent fddde48 commit ab5168d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,9 +980,10 @@ declare namespace RamdaAdjunct {
neither(firstPredicate: Function, secondPredicate: Function): Function;

/**
* Returns `true` if both arguments are falsy, otherwise `false`.
* Returns true if both arguments are falsy; false otherwise.
*/
nand(a: any, b: any): Boolean;
nand(a: any): (b: any) => Boolean;

/**
* Takes a list of predicates and returns a predicate that returns true for a given list of
Expand Down
23 changes: 11 additions & 12 deletions src/nand.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { and, complement } from 'ramda';

/**
* Returns `true` if both arguments are falsy, otherwise `false`.
* Returns true if both arguments are falsy; false otherwise.
*
* @func nand
* @memberOf RA
Expand All @@ -10,19 +10,18 @@ import { and, complement } from 'ramda';
* @sig a -> b -> Boolean
* @param {Boolean} a
* @param {Boolean} b
* @return {Boolean} True if both arguments are false.
* @return {Boolean} true if both arguments are false
* @example
*
* RA.nand(True, True); //=> False
* RA.nand(False, True); //=> True
* RA.nand(True, False); //=> True
* RA.nand(False, False); //=> True
* RA.nand(1.0, 1.0); //=> False
* RA.nand(1.0, 0); //=> True
* RA.nand(0, 1.0); //=> True
* RA.nand(0, 0); //=> True
* RA.nand(true, true); //=> false
* RA.nand(false, true); //=> true
* RA.nand(true, false); //=> true
* RA.nand(false, false); //=> true
* RA.nand(1.0, 1.0); //=> false
* RA.nand(1.0, 0); //=> true
* RA.nand(0, 1.0); //=> true
* RA.nand(0, 0); //=> true
*/

const nand = complement(and);
const nand = complement(and); // eslint-disable-line ramda/complement-simplification

export default nand;
48 changes: 35 additions & 13 deletions test/nand.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
import * as R from 'ramda';
import eq from './shared/eq';
import { nand } from '../src';
import * as RA from '../src';

describe('nand', () => {
it('compared two values with nand', () => {
eq(nand(true, true), false);
eq(nand(false, true), true);
eq(nand(true, false), true);
eq(nand(false, false), true);
eq(nand(1.0, 1.0), false);
eq(nand(0.0, 1.0), true);
eq(nand(1.0, 0.0), true);
eq(nand(0.0, 0.0), true);
describe('RA.nand', function() {
it('should work on booleans', function() {
eq(RA.nand(true, true), false);
eq(RA.nand(false, true), true);
eq(RA.nand(true, false), true);
eq(RA.nand(false, false), true);
});
});

it('should work on numbers', function() {
eq(RA.nand(1.0, 1.0), false);
eq(RA.nand(0.0, 1.0), true);
eq(RA.nand(1.0, 0.0), true);
eq(RA.nand(0.0, 0.0), true);
});

it('should work on a combination of things', function() {
eq(RA.nand(true, 1.0), false);
eq(RA.nand(null, true), true);
eq(RA.nand(1.0, undefined), true);
eq(RA.nand(0.0, false), true);
eq(RA.nand(null, null), true);
eq(RA.nand(null, undefined), true);
});

it('should support currying', function() {
eq(RA.nand(true, true), false);
eq(RA.nand(true)(true), false);
eq(RA.nand(false, true), true);
eq(RA.nand(false)(true), true);
eq(RA.nand(true, false), true);
eq(RA.nand(true)(false), true);
eq(RA.nand(false, false), true);
eq(RA.nand(false)(false), true);
});
});

0 comments on commit ab5168d

Please sign in to comment.