From 55c00956f935244724d3cb3d243691a1cfc32e17 Mon Sep 17 00:00:00 2001 From: Sam Verschueren Date: Thu, 3 May 2018 05:22:32 +0200 Subject: [PATCH] Add `is.observable` (#50) --- package.json | 8 +++++++- readme.md | 12 +++++++++++- source/index.ts | 9 ++++++++- source/tests/test.ts | 10 ++++++++++ tsconfig.json | 1 + 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 00b01ba..2e112ef 100644 --- a/package.json +++ b/package.json @@ -43,17 +43,23 @@ "verify", "compare" ], + "dependencies": { + "symbol-observable": "^1.2.0" + }, "devDependencies": { "@types/jsdom": "^11.0.4", "@types/node": "^9.6.0", "@types/tempy": "^0.1.0", + "@types/zen-observable": "^0.5.3", "ava": "*", "del-cli": "^1.1.0", "jsdom": "^11.6.2", + "rxjs": "^6.0.0", "tempy": "^0.2.1", "tslint": "^5.9.1", "tslint-xo": "^0.7.0", - "typescript": "^2.8.1" + "typescript": "^2.8.1", + "zen-observable": "^0.8.8" }, "types": "dist/index.d.ts" } diff --git a/readme.md b/readme.md index 3e6e33f..ac7876e 100644 --- a/readme.md +++ b/readme.md @@ -261,6 +261,16 @@ is.nodeStream(fs.createReadStream('unicorn.png')); //=> true ``` +##### .observable(value) + +Returns `true` if `value` is an `Observable`. + +```js +const {Observable} = require('rxjs'); +is.observable(new Observable()); +//=> true +``` + ##### .infinite(value) Check if `value` is `Infinity` or `-Infinity`. @@ -281,7 +291,6 @@ Returns `true` if `value` is falsy or an empty string, array, object, map, or se Returns `true` if `is.empty(value)` or a string that is all whitespace. - ##### .any(predicate, ...values) Returns `true` if **any** of the input `values` returns true in the `predicate`: @@ -306,6 +315,7 @@ is.all(is.string, '🦄', [], 'unicorns'); //=> false ``` + ## FAQ ### Why yet another type checking module? diff --git a/source/index.ts b/source/index.ts index 64f5600..18ce4e8 100644 --- a/source/index.ts +++ b/source/index.ts @@ -1,4 +1,5 @@ import * as util from 'util'; +import symbolObservable from 'symbol-observable'; type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array; type Primitive = null | undefined | string | number | boolean | Symbol; @@ -17,6 +18,7 @@ export const enum TypeName { Function = 'Function', GeneratorFunction = 'GeneratorFunction', AsyncFunction = 'AsyncFunction', + Observable = 'Observable', Array = 'Array', Buffer = 'Buffer', Object = 'Object', @@ -88,6 +90,10 @@ function is(value: any): TypeName { // tslint:disable-line:only-arrow-functions return TypeName.Function; } + if (is.observable(value)) { + return TypeName.Observable; + } + if (Array.isArray(value)) { return TypeName.Array; } @@ -244,7 +250,8 @@ namespace is { // tslint:disable-line:no-namespace export const domElement = (value: any) => object(value) && value.nodeType === NODE_TYPE_ELEMENT && string(value.nodeName) && !plainObject(value) && DOM_PROPERTIES_TO_CHECK.every(property => property in value); - export const nodeStream = (value: any) => !nullOrUndefined(value) && isObject(value) && function_(value.pipe); + export const observable = (value: any) => Boolean(value && value[symbolObservable] && value === value[symbolObservable]()); + export const nodeStream = (value: any) => !nullOrUndefined(value) && isObject(value) && function_(value.pipe) && !observable(value); export const infinite = (value: any) => value === Infinity || value === -Infinity; diff --git a/source/tests/test.ts b/source/tests/test.ts index 64edaed..737fd2e 100644 --- a/source/tests/test.ts +++ b/source/tests/test.ts @@ -5,6 +5,8 @@ import * as util from 'util'; import * as tempy from 'tempy'; import test, {TestContext, Context} from 'ava'; import {JSDOM} from 'jsdom'; +import {Subject, Observable} from 'rxjs'; +import ZenObservable from 'zen-observable'; import m from '..'; const isNode8orHigher = Number(process.versions.node.split('.')[0]) >= 8; @@ -321,6 +323,14 @@ const types = new Map([ new Stream.Writable() ] }], + ['observable', { + is: m.observable, + fixtures: [ + new Observable(), + new Subject(), + new ZenObservable(() => {}) // tslint:disable-line:no-empty + ] + }], ['infinite', { is: m.infinite, fixtures: [ diff --git a/tsconfig.json b/tsconfig.json index fd0ae77..161f88e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "module": "commonjs", "moduleResolution": "node", "declaration": true, + "skipLibCheck": true, "pretty": true, "newLine": "lf", "lib": [