├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.js ├── license ├── package.json ├── readme.md └── test ├── main.js └── strict.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | custom: https://sindresorhus.com/donate 4 | tidelift: npm/is-observable 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 18 14 | - 16 15 | - 14 16 | steps: 17 | - uses: actions/checkout@v3 18 | - uses: actions/setup-node@v3 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | export default function isObservable(value) { 2 | if (!value) { 3 | return false; 4 | } 5 | 6 | // eslint-disable-next-line no-use-extend-native/no-use-extend-native 7 | if (typeof Symbol.observable === 'symbol' && typeof value[Symbol.observable] === 'function') { 8 | // eslint-disable-next-line no-use-extend-native/no-use-extend-native 9 | return value === value[Symbol.observable](); 10 | } 11 | 12 | if (typeof value['@@observable'] === 'function') { 13 | return value === value['@@observable'](); 14 | } 15 | 16 | return false; 17 | } 18 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-observable", 3 | "version": "3.0.0", 4 | "description": "Check if a value is an Observable", 5 | "license": "MIT", 6 | "repository": "sindresorhus/is-observable", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "types": "./index.d.ts", 16 | "engines": { 17 | "node": ">=14.16" 18 | }, 19 | "scripts": { 20 | "test": "xo && ava" 21 | }, 22 | "files": [ 23 | "index.js" 24 | ], 25 | "keywords": [ 26 | "observable", 27 | "observables", 28 | "is", 29 | "check", 30 | "detect", 31 | "type" 32 | ], 33 | "devDependencies": { 34 | "ava": "^4.3.0", 35 | "indefinite-observable": "^1.0.1", 36 | "most": "^1.9.0", 37 | "rxjs": "^7.5.5", 38 | "xo": "^0.49.0", 39 | "xstream": "^11.14.0", 40 | "zen-observable": "^0.8.15" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # is-observable 2 | 3 | > Check if a value is an [Observable](https://github.com/zenparsing/es-observable) 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install is-observable 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import isObservable from 'is-observable'; 15 | 16 | isObservable(Observable.of(1, 2)); 17 | //=> true 18 | ``` 19 | 20 | ## Related 21 | 22 | - [observable-to-promise](https://github.com/sindresorhus/observable-to-promise) - Convert an Observable to a Promise 23 | - [is](https://github.com/sindresorhus/is) - Type check values 24 | 25 | --- 26 | 27 |
28 | 29 | Get professional support for this package with a Tidelift subscription 30 | 31 |
32 | 33 | Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies. 34 |
35 |
36 | -------------------------------------------------------------------------------- /test/main.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import zenObservable from 'zen-observable'; 3 | import {of as rxOf} from 'rxjs'; 4 | import * as most from 'most'; 5 | import {Stream as xstream} from 'xstream'; 6 | import {IndefiniteObservable} from 'indefinite-observable'; 7 | import isObservable from '../index.js'; 8 | 9 | test('main', t => { 10 | t.true(isObservable(zenObservable.of(1))); 11 | t.true(isObservable(rxOf(1))); 12 | t.true(isObservable(most.of(1))); 13 | t.true(isObservable(xstream.of(1))); 14 | t.true(isObservable(new IndefiniteObservable(() => {}))); 15 | 16 | t.false(isObservable(undefined)); 17 | t.false(isObservable({foo: true})); 18 | t.false(isObservable(() => {})); 19 | }); 20 | -------------------------------------------------------------------------------- /test/strict.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import isObservable from '../index.js'; 3 | 4 | const fake1 = {'@@observable': true}; 5 | const fake2 = {undefined: true}; 6 | const fake3 = { 7 | undefined() { 8 | return this; 9 | }, 10 | }; 11 | 12 | test('strict', t => { 13 | // Symbol.observable is defined via AVA 14 | delete Symbol.observable; 15 | // eslint-disable-next-line no-use-extend-native/no-use-extend-native 16 | t.is(Symbol.observable, undefined); 17 | t.false(isObservable(fake1)); 18 | t.false(isObservable(fake2)); 19 | t.false(isObservable(fake3)); 20 | }); 21 | --------------------------------------------------------------------------------