Small, predictable utilities for working with plain JavaScript objects
npm install @pfeiferio/object-utilsSmall, 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.
---
``bash``
npm install @pfeiferio/object-utils
---
`ts`
import {
isObject,
isPlainObject,
mergeObjects
} from '@pfeiferio/object-utils'
---
Checks whether a value is a non-null object, excluding arrays.
`ts`
isObject({}) // true
isObject([]) // false
isObject(null) // false
isObject('string') // false
This is a safe alternative to typeof value === 'object'.
---
Checks whether a value is a plain object.
A plain object is defined as:
* created via object literal ({})Object.create(null)
* or
`ts
isPlainObject({}) // true
isPlainObject(Object.create(null)) // true
isPlainObject([]) // false
isPlainObject(new Date()) // false
isPlainObject(null) // false
`
Useful for merge logic and configuration handling.
---
Deeply merges two values.
* Only plain objects are merged recursively
* All other values overwrite
* Inputs are never mutated
`ts`
mergeObjects(
{ server: { host: 'example.com' } },
{ server: { port: 80 } }
)
// → { server: { host: 'example.com', port: 80 } }
`ts`
mergeObjects(
{ a: { b: 1 } },
{ a: 2 }
)
// → { a: 2 }
`ts`
mergeObjects(
{ list: [1, 2] },
{ list: [3] }
)
// → { list: [3] }
`ts``
mergeObjects(
{ a: { b: 1 } },
null
)
// → null
---
* 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
---
MIT