https://github.com/tomaskraus/not-predicate
Negation of predicate. Typed. Suitable for RxJS, Array.filter and others.
https://github.com/tomaskraus/not-predicate
filter functional ixjs js negation not predicate rxjs ts typescript
Last synced: 2 months ago
JSON representation
Negation of predicate. Typed. Suitable for RxJS, Array.filter and others.
- Host: GitHub
- URL: https://github.com/tomaskraus/not-predicate
- Owner: tomaskraus
- License: mit
- Created: 2022-11-14T15:50:35.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-19T22:00:04.000Z (almost 2 years ago)
- Last Synced: 2025-03-05T07:43:53.207Z (3 months ago)
- Topics: filter, functional, ixjs, js, negation, not, predicate, rxjs, ts, typescript
- Language: TypeScript
- Homepage:
- Size: 890 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://github.com/google/gts)# not-predicate
Predicate negation. A predicate that negates a result of its predicate argument:
```ts
import {not} from 'not-predicate';const isEven = (n: number) => n % 2 === 0;
console.log([1, 2, 8, 5].filter(not(isEven)));
//=> [ 1, 5 ]
```## Why to use
You cannot simply use the `!` negation operator on functions, because you cannot cast a function type to boolean:
```ts
// You cannot do this:
[3, 2, 8, 5].filter(!isEven);
````not` acts as a predicate, so we can use it:
```ts
[3, 2, 8, 5].filter(not(isEven)); //=> [ 3, 5 ]
```Plus, there are more `not-predicate`'s advantages:
- Works well with RxJS, Array.filter and others.
- Typed. With `d.ts` for Javascript.
- Zero-dependency.
- Well tested.## Installation
```bash
$ npm install not-predicate
```## Usage
Typescript / ES module:
```ts
import {not} from 'not-predicate';
```Javascript / CommonJS:
```js
const {not} = require('not-predicate');
```## More
Correctly negates a predicate that uses an index argument:
```ts
const isElementIndexEven = (_: unknown, index: number) => index % 2 === 0;console.log(['a', 'a', 'a', 'abc'].map(not(isElementIndexEven)));
//=> [ false, true, false, true ];
```Deals with `this` object properly:
```ts
const customWindow = {
innerWidth: 640,
innerHeight: 480,
};function canXCoordFit(x) {
return x > 0 && x <= this.innerWidth;
}const xValues = [-10, 20, 680, 600, 800, 5];
const xsThatCanFit1 = xValues.filter(not(canXCoordFit), customWindow);
const xsThatCanFit2 = xValues.filter(not(canXCoordFit).bind(customWindow));
console.log(xsThatCanFit1, xsThatCanFit2);
//=> [ -10, 680, 800 ] [ -10, 680, 800 ]
```A predicate type is also provided:
```ts
export type TPredicate = (value: T, index: number) => boolean;
```We can also use the `not-predicate` in [RxJS](https://rxjs.dev/):
```ts
import {not} from 'not-predicate';
import {from} from 'rxjs';
import {filter} from 'rxjs/operators';const evenValueOnEvenIndex = (val: number, index: number) =>
val % 2 === 0 && index % 2 === 0;const src = from([1, 3, 5, 4, 6, 8]).pipe(filter(not(evenValueOnEvenIndex)));
const res: number[] = [];
src.subscribe(x => res.push(x));
console.log(res);
//=> [ 1, 3, 5, 4, 8 ]
```