-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nonePass): Add complement of Ramda’s anyPass (#327)
Ref #234
- Loading branch information
1 parent
e8bb5d1
commit 6b875ea
Showing
4 changed files
with
76 additions
and
0 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
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; |
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,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); | ||
}); | ||
}); |