UNPKG

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.

48 lines (32 loc) โ€ข 1.55 kB
# ๐Ÿง  objChecker.mjs ## ๐Ÿ“˜ Object & Type Utilities This document introduces utility functions designed to help you safely and efficiently work with JavaScript objects and types. These helpers are particularly useful when dealing with dynamic values โ€” especially in situations where data might be coming from APIs, user input, or JSON files. These tools are built to reduce boilerplate code and prevent common mistakes when handling unknown or loosely-typed values in JavaScript. --- ### ๐Ÿงฎ `countObj(obj)` Returns the number of elements in an array or the number of keys in an object. ```js countObj([1, 2, 3]); // 3 countObj({ a: 1, b: 2 }); // 2 countObj('hi'); // 0 ``` --- ### ๐Ÿงผ `isJsonObject(value)` Check if a value is a **plain JSON-compatible object** โ€” meaning it's created via `{}` or `new Object()`, with a prototype of `Object.prototype`, and **not** a special object like `Date`, `Map`, `Array`, etc. ```js isJsonObject({}); // true isJsonObject(Object.create({})); // true isJsonObject(Object.create(Object.prototype)); // true isJsonObject(Object.assign({}, { a: 1 })); // true ``` ```js isJsonObject([]); // false isJsonObject(new Date()); // false isJsonObject(new Map()); // false isJsonObject(Object.create(null)); // false ``` ๐Ÿ”’ This function ensures the object: * is not `null` * has `typeof === 'object'` * is **directly** inherited from `Object.prototype` Use this when you need to strictly validate a raw JSON object (like the output of `JSON.parse()` or manual object literals).