0.1.1 • Published 6 years ago

@ganuz/is-instance-of v0.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

@ganuz/is-instance-of

Source Code Version MIT License Bundle Size TypeScript

Is Instance Of is package from Ganuz library

Install

$ yarn add @ganuz/is-instance-of

Or

$ npm install --save @ganuz/is-instance-of

Use

Module

import {
  default as isInstanceOf
} from '@ganuz/is-instance-of';

Browser

<script src="https://unpkg.com/@ganuz/is-instance-of/bundle.umd.min.js"></script>
let {
  isInstanceOf
} = G;

Examples

 isInstanceOf('foo', null); // throw TypeError
 isInstanceOf('foo', {}); // throw TypeError
 isInstanceOf('foo', class { static [Symbol.hasInstance] = 'foo'; }}); // throw TypeError
 isInstanceOf(NaN, Number); // => false
 isInstanceOf({}, Object); // => true
 isInstanceOf(Object.create(null), Object); // => false
 isInstanceOf('some', {[Symbol.hasInstance]: (v) => v === 'some'}); // => true
 let iframe = document.createElement('iframe');
 document.body.appendChild(iframe);
 let {Array: IFrameArray, Object: IFrameObject, Symbol: IFrameSymbol} = iframe.contentWindow;

 isInstanceOf([], IFrameArray); // => false

 // Note: In native `Symbol` ({Symbol.hasInstance]: 'S'})[IFrameSymbol.hasInstance] === 'S', but in most es6 `Symbol` polyfill ({Symbol.hasInstance]: 'S'})[IFrameSymbol.hasInstance] !== 'S' (in that case it will be `false`).
 isInstanceOf('some', Object.create(IFrameObject.prototype, {[IFrameSymbol.hasInstance]: {value: (v) => v === 'some'}})); // => true

@ganuz/is-instance-of/assert

Module

import {
  default as assertInstanceOf
} from '@ganuz/is-instance-of/assert';

Browser

<script src="https://unpkg.com/@ganuz/is-instance-of/assert/bundle.umd.min.js"></script>
let {
  assertInstanceOf
} = G;

Examples

 assertInstanceOf(null, Object); // throw TypeError
 assertInstanceOf([], Function); // throw TypeError
 assertInstanceOf({}, String); // throw TypeError
 assertInstanceOf(['a', 'b', 'c'], Array); // => ['a', 'b', 'c']
 assertInstanceOf(/^[a-z]+$/i, RegExp); // => /^[a-z]+$/i

License

Copyright © Yisrael Eliev, Licensed under the MIT license.