@pfeiferio/object-utils
v1.1.1
Published
Small, predictable utilities for working with plain JavaScript objects
Maintainers
Readme
@pfeiferio/object-utils
Small, predictable utility functions for working with plain JavaScript objects.
This package provides a minimal set of helpers for object inspection and deep merging with strict, explicit behavior and zero dependencies.
Installation
npm install @pfeiferio/object-utilsUsage
import {
isObject,
isPlainObject,
mergeObjects
} from '@pfeiferio/object-utils'isObject(value)
Checks whether a value is a non-null object, excluding arrays.
isObject({}) // true
isObject([]) // false
isObject(null) // false
isObject('string') // falseThis is a safe alternative to typeof value === 'object'.
isPlainObject(value)
Checks whether a value is a plain object.
A plain object is defined as:
- created via object literal (
{}) - or
Object.create(null)
isPlainObject({}) // true
isPlainObject(Object.create(null)) // true
isPlainObject([]) // false
isPlainObject(new Date()) // false
isPlainObject(null) // falseUseful for merge logic and configuration handling.
mergeObjects(a, b)
Deeply merges two values.
- Only plain objects are merged recursively
- All other values overwrite
- Inputs are never mutated
mergeObjects(
{ server: { host: 'example.com' } },
{ server: { port: 80 } }
)
// → { server: { host: 'example.com', port: 80 } }Overwrite behavior
mergeObjects(
{ a: { b: 1 } },
{ a: 2 }
)
// → { a: 2 }mergeObjects(
{ list: [1, 2] },
{ list: [3] }
)
// → { list: [3] }Non-plain values always win
mergeObjects(
{ a: { b: 1 } },
null
)
// → nullDesign Principles
- Explicit semantics
- No array merging
- No mutation
- Predictable results
- Suitable for configuration and business logic
This package intentionally avoids:
- implicit deep cloning
- class instance merging
- array merging heuristics
License
MIT
