@bemoje/assert-constructor

Throws TypeError if 'value' is not a constructor.

Usage no npm install needed!

<script type="module">
  import bemojeAssertConstructor from 'https://cdn.skypack.dev/@bemoje/assert-constructor';
</script>

README

@bemoje/assert-constructor

Throws TypeError if 'value' is not a constructor.

Version

NPM version

Travis CI

dependencies

Dependencies

dependencies

Stats

NPM downloads Forks

Donate

Buy Me A Beer donate button PayPal donate button

Installation

npm install @bemoje/assert-constructor
npm install --save @bemoje/assert-constructor
npm install --save-dev @bemoje/assert-constructor

Usage

import assertConstructor from '@bemoje/assert-constructor'

function constructorName(ctor) {
    assertConstructor(ctor)
    return ctor.name
}

constructorName(class MyClass {})
//=> 'MyClass

constructorName(function MyClass() {})
//=> 'MyClass

try {
    constructorName(() => {})
    //=> throw TypeError('Expected class constructor, got Function')
} catch (e) {
    console.log(e)
}

try {
    constructorName('asd')
    //=> throw TypeError('Expected class constructor, got String')
} catch (e) {
    console.log(e)
}

Tests

Uses Jest to test module functionality. Run tests to get coverage details.

npm run test

API

assertConstructor

Throws TypeError if 'value' is not a constructor.

Parameters
  • value any The value to evaluate
Returns

void