tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
155 lines (107 loc) โข 3.4 kB
Markdown
# ๐ง objFilter.mjs
Type detection, extension, and analysis made easy โ simple and extensible type validation in pure JavaScript.
## Overview
`objFilter.mjs` is a utility module that provides a structured and extensible way to validate, infer, and count object types in JavaScript. Itโs designed to work in both Node.js and browser environments and is perfect for libraries that need consistent, extensible type-checking logic.
Whether youโre validating inputs, writing schema validators, or building tools that need to "understand" JavaScript data โ this module has your back!
## Features
- โ
Precise type detection (`undefined`, `null`, `array`, `buffer`, `date`, etc.)
- โ Custom type extensions with ordering
- ๐ Reorder type checking priority
- ๐ Safe and predictable type checks
- ๐งฎ Count values in arrays and objects
- ๐ซ No dependencies
## Usage
### ๐ `objType(obj, [type])`
Get the type of any value, or check it against a known type.
```js
objType([], 'array'); // true
objType('hello'); // "string"
objType(undefined); // null
```
Returns:
- `true` / `false` if a type is provided
- The detected type name as a string if no type is provided
- `null` if `undefined` is passed
### ๐ `checkObj(obj)`
Checks the type of a given object and returns the validation result if a known type is detected.
```js
checkObj('hello');
// { valid: true, type: "string" }
checkObj(123);
// { valid: true, type: "number" }
checkObj(undefined);
// { valid: true, type: "undefined" }
checkObj(Symbol('sym'));
// { valid: true, type: "symbol" }
checkObj(() => {});
// { valid: true, type: "function" }
checkObj(null);
// { valid: true, type: "null" }
checkObj(Object.create(null));
// { valid: true, type: "object" }
```
Returns:
- `{ valid: true, type: "<type>" }` if the type is recognized
- `{ valid: null, type: null }` if no matching type is found
### โ `extendObjType(newTypes, [index])`
Add your own custom types. You can optionally define where in the check order they go.
```js
extendObjType({
customElement: val => val && val.tagName === 'MY-ELEMENT'
});
```
```js
extendObjType([
[ 'alpha', val => typeof val === 'string' ],
[ 'beta', val => Array.isArray(val) ]
]);
```
This will insert `customElement` before the built-in `object` type unless a position is specified.
### ๐ `reorderObjTypeOrder(newOrder)`
Set a custom priority order for how types are checked (must include only known types).
```js
reorderObjTypeOrder([
'string',
'number',
'array',
'object'
]);
```
Returns `true` if successful, or `false` if the order includes unknown types.
### ๐ `cloneObjTypeOrder()`
Safely get a copy of the current type evaluation order.
```js
const currentOrder = cloneObjTypeOrder();
```
## Supported Types
Hereโs a full list of supported type names (in their default order):
- `undefined`
- `null`
- `boolean`
- `number`
- `bigint`
- `string`
- `symbol`
- `function`
- `array`
- `buffer`
- `date`
- `regexp`
- `map`
- `set`
- `weakmap`
- `weakset`
- `promise`
- `htmlElement`
- `object`
You can change this order or insert your own types with `extendObjType`.
### ๐ ๏ธ `getCheckObj()`
This function creates a clone of the functions from the `typeValidator` object. It returns a new object where the keys are the same and the values are the cloned functions.