https://github.com/nandovejer/js-subtype
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.).
https://github.com/nandovejer/js-subtype
javascript subtypes type-detection types utility
Last synced: 4 months ago
JSON representation
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.).
- Host: GitHub
- URL: https://github.com/nandovejer/js-subtype
- Owner: nandovejer
- Created: 2024-10-22T11:40:33.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2025-01-21T15:32:27.000Z (5 months ago)
- Last Synced: 2025-01-21T16:33:55.190Z (5 months ago)
- Topics: javascript, subtypes, type-detection, types, utility
- Language: JavaScript
- Homepage:
- Size: 7.81 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# js-subtype
[](https://badge.fury.io/js/js-subtype)
[](LICENSE)`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:
```bash
npm install js-subtype
```Or with yarn:
```bash
yarn add js-subtype
```## Usage
Here’s a quick example of how to use `js-subtype`:
```javascript
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:
```javascript
function validateInput(input) {
if (getType(input) !== "Array") {
throw new Error("Input must be an array.");
}
// Additional logic...
}
```| Type | Subtype | Description |
|------------|---------------|-----------------------------------------------------------------------------|
| `undefined`| `undefined` | Undefined value. |
| `boolean` | `boolean` | Boolean value (`true` or `false`). |
| `number` | `integer` | Integer number. |
| `number` | `float` | Floating-point number. |
| `string` | `string` | Text string. |
| `object` | `null` | Null value. |
| `object` | `array` | Array (list of elements). |
| `object` | `date` | `Date` object. |
| `object` | `regexp` | Regular expression. |
| `object` | `map` | `Map` object. |
| `object` | `set` | `Set` object. |
| `object` | `weakmap` | `WeakMap` object. |
| `object` | `weakset` | `WeakSet` object. |
| `object` | `error` | `Error` object. |
| `object` | `promise` | `Promise` object. |
| `object` | `object` | Generic object. |
| `function` | `function` | Function. |
| `symbol` | `symbol` | Symbol (`Symbol`). |
| `bigint` | `bigint` | Large integer (`BigInt`). |### Identifying Native JavaScript Objects
Determine the exact native type of an object, such as `Map` or `Set`:
```javascript
console.log(getType(new Map())); // Output: "Map"
console.log(getType(new Set())); // Output: "Set"
```## License
This project is licensed under the [MIT License](LICENSE).