GithubHelp home page GithubHelp logo

jsdevtom / distinct-until-changed-immutable Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 2.0 504 KB

Like distinctUntilChanged(), but with immutable values

Home Page: https://toms.blog/distinct-until-changed-immutable/

License: MIT License

TypeScript 100.00%
rxjs rxjs-operators distinctuntilchanged deep-copy deep-clone

distinct-until-changed-immutable's Introduction

Distinct Until Changed Immutable

tl;dr

Install:

npm i -S distinct-until-changed-immutable

then import:

import { distinctUntilChangedImmutable } from 'distinct-until-changed-immutable';

then use:

of({a: 'a'}, {b: 'b'}, {b: 'b'})
  .pipe(
    distinctUntilChangedImmutable(),
  )

Returns an Observable that emits all items emitted by the source Observable that are deeply distinct by comparison from the previous item.

If a comparator function is provided, then it will be called for each item to test for whether or not that value should be emitted.

If a comparator function is not provided, a deep equality check is used by default.

Example

A simple example with numbers

import { of } from 'rxjs';
import { distinctUntilChangedImmutable } from 'distinct-until-changed-immutable';

of({a: 'a'}, {b: 'b'}, {b: 'b'}).pipe(
    distinctUntilChangedImmutable(),
  )
  .subscribe(x => console.log(x)); // {a: 'a'}, {b: 'b'}

An example using a compare function

import { of } from 'rxjs';
import { distinctUntilChangedImmutable } from 'distinct-until-changed-immutable';

interface Person {
   age: number,
   name: string
}

of<Person>(
    { age: 4, name: 'Foo'},
    { age: 7, name: 'Bar'},
    { age: 4, name: 'Foo'},
    { age: 6, name: 'Foo'},
  ).pipe(
    distinctUntilChangedImmutable((p: Person /*I have been cloned*/, q: Person) => p === q),
  )
  .subscribe(x => console.log(x));

// displays:
// { age: 4, name: 'Foo' }
// { age: 7, name: 'Bar' }
// { age: 4, name: 'Foo' } // Emitted because the parameters are immutable and we are doing a simple === check in the comparator function above
// { age: 5, name: 'Foo' }

distinct-until-changed-immutable's People

Contributors

jsdevtom avatar vesapupovci avatar

Watchers

 avatar  avatar

distinct-until-changed-immutable's Issues

ERROR TypeError: Class constructor Subscriber cannot be invoked without 'new'

Getting this error when trying to use the library in an Angular 8 project.

ERROR TypeError: Class constructor Subscriber cannot be invoked without 'new'

This happens only when the target in tsconfig.ts is set to es2015.

A workaround is to set the target to es5 but then this disables differential loading in Angular.

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.