Miss any of our Open RFC calls?Watch the recordings here! »

@bemoje/throw-type-error

1.0.1 • Public • Published

@bemoje/throw-type-error

Throws a TypeError with a generated error message with ANSI-colors for easier console output readability.

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/throw-type-error
npm install --save @bemoje/throw-type-error
npm install --save-dev @bemoje/throw-type-error

Usage

import throwTypeError from '@bemoje/throw-type-error'
 
import throwTypeError from '../src/throw-type-error'
 
/**
 * Adds a and b
 * @param {number} a - The first number
 * @param {number|string} b - The second number, or a number coercible-string
 */
function add(a, b) {
    // type check 'a'
    if (!isType(Number, a)) {
        throwTypeError(Number, a)
    }
 
    // type check 'b'
    if (!isType(Number, b) && !isType(String, b)) {
        throwTypeError([Number, String], b)
    }
 
    // coerce b
    b = Number(b)
 
    // add
    return a + b
}
 
add(1, 2)
//=> 3
 
add(1, '2')
//=> 3
 
add('one', 2)
//=> throws TypeError('Expected Number, got String) [IN ANSI COLORED FOR READABILITY]
 
add(1, /2/g)
//=> throws TypeError('Expected [Number, String], got RegExp) [IN ANSI COLORED FOR READABILITY]
 

Tests

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

npm run test

API

throwTypeError

Throws a TypeError with a generated error message with ANSI-colors for easier console output readability.

Parameters
  • expectedTypeConstructor function The expected type's constructor

  • value any The value to evaluate

Returns

void

Install

npm i @bemoje/throw-type-error

DownloadsWeekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

8.46 kB

Total Files

5

Last publish

Collaborators

  • avatar