GithubHelp home page GithubHelp logo

tomaskraus / not-predicate Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 911 KB

Negation of predicate. Typed. Suitable for RxJS, Array.filter and others.

License: MIT License

JavaScript 6.34% TypeScript 93.66%
js negation not predicate rxjs ts filter functional ixjs typescript

not-predicate's Introduction

build Code Style: Google

not-predicate

Predicate negation. A predicate that negates a result of its predicate argument:

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:

// You cannot do this:
[3, 2, 8, 5].filter(!isEven);

not acts as a predicate, so we can use it:

[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

$ npm install not-predicate

Usage

Typescript / ES module:

import {not} from 'not-predicate';

Javascript / CommonJS:

const {not} = require('not-predicate');

More

Correctly negates a predicate that uses an index argument:

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:

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:

export type TPredicate<T> = (value: T, index: number) => boolean;

We can also use the not-predicate in RxJS:

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 ]

not-predicate's People

Contributors

dependabot[bot] avatar tomaskraus avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.