https://github.com/jqrony/typeh
A lightweight JS utility for advanced type detection, validation, and enforcement.
https://github.com/jqrony/typeh
datatype typehint
Last synced: 11 months ago
JSON representation
A lightweight JS utility for advanced type detection, validation, and enforcement.
- Host: GitHub
- URL: https://github.com/jqrony/typeh
- Owner: jqrony
- License: mit
- Created: 2025-07-17T18:28:58.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-07-17T19:59:21.000Z (11 months ago)
- Last Synced: 2025-07-17T20:56:52.244Z (11 months ago)
- Topics: datatype, typehint
- Language: JavaScript
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Typeh - JavaScript Type Helper Library [](https://www.npmjs.com/package/typeh)
> A lightweight JS utility for advanced type detection, validation, and enforcement.
## Features
- Determine the detailed type of any value (including distinguishing between integers and floats)
- Validate values against expected types with detailed error messages
- Check for common type categories like scalar, numeric, countable, infinite, etc.
- Supports multiple types (e.g. `int|float`) in validation
- Works in browser and Node.js environments
---
## Installation
Since TH is a self-contained script, you can include it directly in your project.
```bash
npm install typeh --save
```
### Browser
Include the script in your HTML:
```html
```
OR
```js
// Node.js
require("typeh");
```
**isA Types**
- isCountable()
- isInteger()
- isDouble()
- isNumeric()
- isLong()
- isScalar()
- isInfinite()
- isString()
- isBool()
- isArray()
- isObject()
- isCallable()
- isIterable()
- isNull()
- isFalse()
- isTrue()
**Type List**
- int
- float
- string
- bool/boolean
- array
- object
- callable
- iterable
- mixed
- null
- false
- true
Others... Like (HTMLElement, regexp, symbol etc.) But support only in `define` method.
### Usage
**Variable**
```js
// Required
const arr = array([]);
const str = string('Hello World');
const fn = callable(() => {});
const isBool = bool(true);
const num = int(12);
const float = float(12.22);
const values = iterable(new Set([1,2,3,4]));
const obj = object({});
const data = mixed("Foo"); // Allow all types
/* Optional/Required */
function myFun(username, email, number, isAdult) {
username = string(username); // [required]
email = _string(email); // [optional]
number = _int(number); // [optional]
isAdult = bool(isAdult); // [required]
}
myFun('Foo', null, null, true);
```
**How to use custom Type**
```js
const custom = define("string|array|bool", []); // Allow (string, array, boolean) only
// Optional
const optval = define("?string|array"); // Allow (string, array, null, undefined)
```
**Argument typeh**
```js
function select(selector, context, results, isSelf) {
setType({
string: selector, // [required]
HTMLElement: context, // [required]
array: results, // [required]
"?bool": isSelf // [optional]
});
}
```
**How To Check Primitive Type**
```js
toType(/^[a-z]/); // Output: regexp
toType(new Date); // Output: date
toType('Hello'); // Output: string
toType(123); // Output: number
toType(true); // Output: boolean
toType(() => {}); // Output: function
toType([]); // Output: array
toType({}); // Output: object
toType(new Error()); // Output: error
toType(Symbol('Foo')); // Output: symbol
```
**Check To Object Type**
```js
toType(12.21); // Output: float
toType(123); // Output: int
toType(/^[a-z]/); // Output: regexp
toType('Hello'); // Output: string
const body = document.body;
toType(body); // Output: HTMLBodyElement
```