Skip to content

Commit

Permalink
feat(nonePass): Add complement of Ramda’s anyPass (#327)
Browse files Browse the repository at this point in the history
Ref #234
  • Loading branch information
Undistraction authored and char0n committed Feb 1, 2018
1 parent e8bb5d1 commit 6b875ea
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,16 @@ declare namespace RamdaAdjunct {
*/
notAllPass(predicates: Array<Function>): Function;

/**
* Takes a list of predicates and returns a predicate that returns true for a given list of
* arguments if none of the provided predicates are satisfied by those arguments. It is the
* complement of Ramda's anyPass.
*
* The function returned is a curried function whose arity matches that of the
* highest-arity predicate.
*/
nonePass(predicates: Array<Function>): Function;

/**
* Identity type.
*/
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,6 @@ export { default as defaultWhen } from './defaultWhen';
export { default as notBoth } from './notBoth';
export { default as neither } from './neither';
export { default as notAllPass } from './notAllPass';
export { default as nonePass } from './nonePass';
// Types
export { default as Identity } from './fantasy-land/Identity';
33 changes: 33 additions & 0 deletions src/nonePass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { complement, compose, anyPass } from 'ramda';


/**
* Takes a list of predicates and returns a predicate that returns true for a given list of
* arguments if none of the provided predicates are satisfied by those arguments. It is the
* complement of Ramda's anyPass.
*
* The function returned is a curried function whose arity matches that of the
* highest-arity predicate.
*
* @func nonePass
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/2.5.0|v2.5.0}
* @category Logic
* @sig [(*... -> Boolean)] -> (*... -> Boolean)
* @param {Array} predicates An array of predicates to check
* @return {Function} The combined predicate
* @see {@link http://ramdajs.com/docs/#anyPass|anyPass}
* @example
*
* const gt10 = R.gt(R.__, 10)
* const even = (x) => x % 2 === 0;
* const f = RA.nonePass([gt10, even]);
*
* f(12); //=> false
* f(8); //=> false
* f(11); //=> false
* f(9); //=> true
*/
const nonePass = compose(complement, anyPass);

export default nonePass;
32 changes: 32 additions & 0 deletions test/nonePass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as RA from '../src/index';
import eq from './shared/eq';


describe('nonePass', function () {
const odd = n => n % 2 !== 0;
const divBy3 = n => n % 3 === 0;
const lt20 = n => n < 20;
const plusEq = (w, x, y, z) => w + x === y + z;

it('reports whether all predicates are satisfied by a given value', function () {
const ok = RA.nonePass([odd, divBy3, lt20]);
eq(ok(9), false); // all ps succeed
eq(ok(12), false); // p1 fails
eq(ok(7), false); // p2 fails
eq(ok(21), false); // p3 fails
eq(ok(2), false); // p1 and p2 fails
eq(ok(18), false); // p1 and p3 fails
eq(ok(23), false); // p2 and p3 fails
eq(ok(26), true); // all ps fail
});

it('returns true on empty predicate list', function () {
eq(RA.nonePass([])(3), true);
});

it('returns a curried function whose arity matches that of the highest-arity predicate', function () {
eq(RA.nonePass([odd, divBy3, plusEq]).length, 4);
eq(RA.nonePass([odd, divBy3, plusEq])(26, 26, 26, 28), true);
eq(RA.nonePass([odd, divBy3, plusEq])(26)(26)(26)(28), true);
});
});

0 comments on commit 6b875ea

Please sign in to comment.