Skip to content

Commit

Permalink
feat(hasCompleted): Add operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Jun 3, 2018
1 parent 25907f1 commit dfeab10
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
33 changes: 33 additions & 0 deletions source/operators/hasCompleted-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc
*/
/*tslint:disable:no-unused-expression*/

import { marbles } from "rxjs-marbles";
import { hasCompleted } from "./hasCompleted";

describe("hasCompleted", () => {

it("should emit nothing for a source that does not complete", marbles(m => {

const source = m.cold("ab-cd-ef--");
const sourceSubs = "^---------";
const expected = "----------";

const destination = source.pipe(hasCompleted());
m.expect(destination).toBeObservable(expected);
m.expect(source).toHaveSubscriptions(sourceSubs);
}));

it("should emit 'true' when a source completes", marbles(m => {

const source = m.cold("ab-cd-ef-|");
const sourceSubs = "^--------!";
const expected = m.cold("---------(t|)", { t: true });

const destination = source.pipe(hasCompleted());
m.expect(destination).toBeObservable(expected);
m.expect(source).toHaveSubscriptions(sourceSubs);
}));
});
11 changes: 11 additions & 0 deletions source/operators/hasCompleted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc
*/

import { concat, of, OperatorFunction } from "rxjs";
import { ignoreElements } from "rxjs/operators";

export function hasCompleted<T>(): OperatorFunction<T, boolean> {
return source => concat(source.pipe(ignoreElements()), of(true));
}

0 comments on commit dfeab10

Please sign in to comment.