1.0.1 • Published 8 months ago
@engr-lukman/is-object-checker v1.0.1
Is Object Checker
A lightweight, TypeScript-compatible utility for checking if a value is a pure object (not an array and not null).
Installation
npm install @engr-lukman/is-object-checker
# or
yarn add @engr-lukman/is-object-checker
# or
pnpm add @engr-lukman/is-object-checkerDemo
Check out the interactive Storybook demo to see the component in action.
Features
- Accurately detects pure JavaScript objects
- Distinguishes objects from arrays, null values, and primitive types
- Full TypeScript support with type definitions
- Zero dependencies
- ES6 module compatible
- TypeScript type guards for improved type inference
- Built for Node.js 20 LTS or higher and modern browsers
Usage
Basic Usage
// ES Modules / TypeScript
import isPureObject from '@engr-lukman/is-object-checker';
// CommonJS
const isPureObject = require('@engr-lukman/is-object-checker').default;
// Examples
isPureObject({}); // true
isPureObject({ name: 'John' }); // true
isPureObject(null); // false
isPureObject([]); // false
isPureObject(123); // false
isPureObject('string'); // false
isPureObject(new Date()); // true (Date is an object)
isPureObject(() => {}); // false (functions are not pure objects)With TypeScript Type Guards
import isPureObject from '@engr-lukman/is-object-checker';
function processValue(value: unknown): void {
if (isPureObject(value)) {
// TypeScript knows value is an object here
console.log('This is an object with keys:', Object.keys(value));
} else {
console.log('Not a pure object');
}
}
// Type safety example
const dataFromAPI: unknown = { userId: 1, name: 'John' };
if (isPureObject(dataFromAPI)) {
// TypeScript correctly narrows the type
const keys = Object.keys(dataFromAPI);
console.log(keys); // ['userId', 'name']
}API Reference
isPureObject(value: unknown): value is Record<string | symbol, unknown>
Checks if a value is a pure JavaScript object (not an array and not null).
| Parameter | Type | Description |
|---|---|---|
| value | unknown | The value to check |
Returns: A boolean indicating if the value is a pure object. Also acts as a TypeScript type guard.
Compatibility
- Node.js 20 LTS or higher
- Modern browsers supporting ES6 modules
- TypeScript 4.0+
Use Cases
- Validating API responses before processing
- Type-safe handling of unknown data structures
- Defensive programming patterns
- Configuration validation
License
MIT © Mohammad Lukman Hussain