Skip to content

Commit

Permalink
feat: distinctWithInitial operator
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed Nov 13, 2022
1 parent b6abf3f commit 6611c38
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 30 deletions.
30 changes: 0 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions projects/rxutils/operators/distinctWithInitial.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {expect} from 'chai';
import {lastValueFrom, of} from 'rxjs';
import {toArray} from 'rxjs/operators';
import {distinctWithInitial} from './distinctWithInitial';

/* eslint-disable @typescript-eslint/no-magic-numbers */

describe('operators/distinctWIthInitialPrimitive', () => {
it('Should filter without args (1)', async () => {
const src$ = of(0, 1, 1, 0).pipe(
distinctWithInitial(0),
toArray()
);
expect(await lastValueFrom(src$)).to.deep.eq([0, 1, 0]);
});

it('Should filter with args', async () => {
type V = {v: number};
const val = (v: number): V => ({v});

const src$ = of(val(3), val(2), val(4), val(3)).pipe(
distinctWithInitial<V, number>(val(1), (a, b) => a % 2 === b % 2, v => v.v),
toArray()
);

expect(await lastValueFrom(src$)).to.deep.eq([
val(1),
val(2),
val(3)
]);
});
});
43 changes: 43 additions & 0 deletions projects/rxutils/operators/distinctWithInitial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type {MonoTypeOperatorFunction} from 'rxjs';
import {pipe} from 'rxjs';
import {distinctUntilChanged, startWith} from 'rxjs/operators';

/**
* Shortcut alias for `pipe(startWith(initial), distinctUntilChanged())`
* @param initial The initial value
* @param comparator `distinctUntilChanged` comparator
* @kind Operator
* @since 2.3.0
* @see https://rxjs.dev/api/operators/startWith
* @see https://rxjs.dev/api/operators/distinctUntilChanged
*/
export function distinctWithInitial<T>(
initial: T,
comparator?: (previous: T, current: T) => boolean
): MonoTypeOperatorFunction<T>;

/**
* Shortcut alias for `pipe(startWith(initial), distinctUntilChanged())`
* @param initial The initial value
* @param comparator `distinctUntilChanged` comparator
* @param keySelector `distinctUntilChanged` key selector
* @kind Operator
* @since 2.3.0
* @see https://rxjs.dev/api/operators/startWith
* @see https://rxjs.dev/api/operators/distinctUntilChanged
*/
export function distinctWithInitial<T, K>(
initial: T,
comparator: (previous: K, current: K) => boolean,
keySelector: (value: T) => K
): MonoTypeOperatorFunction<T>;

export function distinctWithInitial<T>(
initial: T,
...untilChangedArgs: Parameters<typeof distinctUntilChanged<T>>
): MonoTypeOperatorFunction<T> {
return pipe(
startWith(initial),
distinctUntilChanged(...untilChangedArgs)
);
}
1 change: 1 addition & 0 deletions projects/rxutils/operators/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export {distinctUntilDeepChanged} from './distinctUntilDeepChanged';
export {innerMap} from './innerMap';
export {countEmissions} from './countEmissions';
export {distinctWithInitial} from './distinctWithInitial';
export {filterUntilPasses} from './filterUntilPasses';
export {startWithIfAsynchronous} from './startWithIfAsynchronous';
export {takeTruthy} from './takeTruthy';
Expand Down

0 comments on commit 6611c38

Please sign in to comment.