5.0.0-next.1 • Published 4 years ago

@nbsh/to-type v5.0.0-next.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

to-type

Fixes JavaScript typeof operator using Symbol.toStringTag

Installation

$ npm i -S @nbsh/to-type
$ yarn add @nbsh/to-type

Usage

import { strictEqual } from 'assert';
import { toType, Types } from '@nbsh/to-type';

toType(4); // "number"
toType('abc'); // "string"
toType(true); // "boolean"
toType({ a: 4 }); // "object"
toType([1, 2, 3]); // "array"
toType(new Date()); // "date"
toType(/a-z/); // "regexp"
toType(undefined); // "undefined"
toType(null); // "null"
toType(new TypeError()); // "error"
toType(() => {}); // "function"

strictEqual(toType(4), Types.Number); // true
strictEqual(toType('abc'), Types.String); // true

API

toType(object: unknown): string

The toType() method return the default string description of the object it receive as argument. toType() do not affect the object itself. See Symbol.toStringTag.

Types: enum

The Types enum represent the well-known string descriptions that toType() method return. Note, implementing a class that overrides the toStringTab symbol will not make its value available within this enum.