Skip to content

js-subtype is a lightweight library that allows you to determine JavaScript subtypes, making it easy to identify the exact type of an object (like Array, Date, etc.).

Notifications You must be signed in to change notification settings

nandovejer/js-subtype

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

js-subtype

npm version
License: MIT

js-subtype is a lightweight library that allows you to determine JavaScript subtypes, making it easy to identify the exact type of an object (like Array, Date, etc.).

Features

  • Supports multiple subtypes (Array, Date, Map, RegExp, etc.).
  • Lightweight and easy to use.
  • Zero dependencies.
  • Works in both Node.js and modern browsers.

Installation

Install using npm:

npm install js-subtype

Or with yarn:

yarn add js-subtype

Usage

Here’s a quick example of how to use js-subtype:

const { getType } = require("js-subtype");

console.log(getType([1, 2, 3])); // Output: "Array"
console.log(getType(new Date())); // Output: "Date"
console.log(getType(/regex/)); // Output: "RegExp"

API

getType(value)

Returns a string representing the subtype of the given value.

  • Parameters:
    • value: Any JavaScript value.
  • Returns: The string subtype (Array, Date, Map, etc.).

Use Cases

Advanced Type Validation

Use js-subtype to validate inputs or types precisely in your applications:

function validateInput(input) {
  if (getType(input) !== "Array") {
    throw new Error("Input must be an array.");
  }
  // Additional logic...
}

Identifying Native JavaScript Objects

Determine the exact native type of an object, such as Map or Set:

console.log(getType(new Map())); // Output: "Map"
console.log(getType(new Set())); // Output: "Set"

License

This project is licensed under the MIT License.

About

js-subtype is a lightweight library that allows you to determine JavaScript subtypes, making it easy to identify the exact type of an object (like Array, Date, etc.).

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published