Skip to content

Commit

Permalink
feat(isNullish): Add is(Not)Nullish.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Jun 1, 2018
1 parent 5a2df9c commit 1d50d2e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
8 changes: 8 additions & 0 deletions source/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

import { Observable, SchedulerLike } from "rxjs";

export function isNotNullish<T>(value: T | null | undefined): value is T {
return (value !== null) && (value !== undefined);
}

export function isNullish<T>(value: T | null | undefined): value is null | undefined {
return (value === null) || (value === undefined);
}

export function isObservable(value: any): value is Observable<any> {
return value && (typeof value["subscribe"] === "function");
}
Expand Down
35 changes: 35 additions & 0 deletions source/utils-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @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 { expect } from "chai";
import { isNotNullish, isNullish } from "./util";

describe("util", () => {

describe("isNotNullish", () => {

it("should determine whether a value is not nullish", () => {

expect(isNotNullish(null)).to.be.false;
expect(isNotNullish(undefined)).to.be.false;
expect(isNotNullish(false)).to.be.true;
expect(isNotNullish(0)).to.be.true;
expect(isNotNullish("")).to.be.true;
});
});

describe("isNullish", () => {

it("should determine whether a value is nullish", () => {

expect(isNullish(null)).to.be.true;
expect(isNullish(undefined)).to.be.true;
expect(isNullish(false)).to.be.false;
expect(isNullish(0)).to.be.false;
expect(isNullish("")).to.be.false;
});
});
});

0 comments on commit 1d50d2e

Please sign in to comment.