Added "not"
This commit is contained in:
parent
9121402cfb
commit
9479b61c09
|
@ -25,7 +25,7 @@ always() // => true
|
||||||
never() // => false
|
never() // => false
|
||||||
```
|
```
|
||||||
|
|
||||||
**and** and **or** combines predicates
|
**and**, **or** and **not** allow to combine or negate predicates
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const a = and((x: number) => x > 2, (x: number) => x < 5);
|
const a = and((x: number) => x > 2, (x: number) => x < 5);
|
||||||
|
@ -38,6 +38,10 @@ o(1) // => true
|
||||||
o(2) // => false
|
o(2) // => false
|
||||||
o(3) // => false
|
o(3) // => false
|
||||||
o(4) // => true
|
o(4) // => true
|
||||||
|
const n = not((x: number) => x == 1);
|
||||||
|
n(0) // => true
|
||||||
|
n(1) // => false
|
||||||
|
n(2) // => true
|
||||||
```
|
```
|
||||||
|
|
||||||
**attr** gets an object's attribute:
|
**attr** gets an object's attribute:
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "tk-functional",
|
"name": "tk-functional",
|
||||||
"version": "0.2.0",
|
"version": "0.2.1",
|
||||||
"description": "Typescript/Javascript helpers for functional-style programming",
|
"description": "Typescript/Javascript helpers for functional-style programming",
|
||||||
"source": "src/index.ts",
|
"source": "src/index.ts",
|
||||||
"main": "dist/tk-functional.umd.js",
|
"main": "dist/tk-functional.umd.js",
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { always, and, attr, bool, cmp, fmap, identity, is, isinstance, never, nn, nnu, nop, nu, or, partial, pipe } from "./index";
|
import { always, and, attr, bool, cmp, fmap, identity, is, isinstance, never, nn, nnu, nop, not, nu, or, partial, pipe } from "./index";
|
||||||
|
|
||||||
describe(nop, () => {
|
describe(nop, () => {
|
||||||
it("does nothing", () => {
|
it("does nothing", () => {
|
||||||
|
@ -52,6 +52,19 @@ describe(never, () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe(not, () => {
|
||||||
|
it("negates predicate", () => {
|
||||||
|
const iseven = (x: number) => x % 2 == 0;
|
||||||
|
const isodd = not(iseven);
|
||||||
|
expect(iseven(0)).toBe(true);
|
||||||
|
expect(isodd(0)).toBe(false);
|
||||||
|
expect(iseven(1)).toBe(false);
|
||||||
|
expect(isodd(1)).toBe(true);
|
||||||
|
expect(iseven(2)).toBe(true);
|
||||||
|
expect(isodd(2)).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe(and, () => {
|
describe(and, () => {
|
||||||
it("combines predicates", () => {
|
it("combines predicates", () => {
|
||||||
const f = and((x: number) => x % 2 == 0, (x: number) => x > 5);
|
const f = and((x: number) => x % 2 == 0, (x: number) => x > 5);
|
||||||
|
|
|
@ -48,6 +48,12 @@ export const always = (...args: any[]) => true
|
||||||
*/
|
*/
|
||||||
export const never = (...args: any[]) => false
|
export const never = (...args: any[]) => false
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Negate a predicate
|
||||||
|
*/
|
||||||
|
export function not<A extends any[]>(predicate: (...args: A) => boolean): (...args: A) => boolean {
|
||||||
|
return (...args) => !predicate(...args);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply a boolean "and" to merge predicates
|
* Apply a boolean "and" to merge predicates
|
||||||
|
|
Loading…
Reference in a new issue