Object Utility Library
raw JSON →object-lib is a utility library for JavaScript that provides a collection of functions for robust object manipulation, particularly focusing on recursive operations, alignment, cloning, deep comparison, and merging. The current stable version is 5.2.4, with frequent patch and minor releases, indicating active maintenance and continuous development. Key differentiators include its integration with `object-scan` syntax for declarative pathing in functions like `clone` and `Merge`, enabling powerful and flexible data transformations. It also offers unique features like `jsonSmartParse` for gracefully handling malformed JSON, often encountered from LLM outputs, and `SafeProxy` for creating proxy objects that enforce strict property access, throwing errors for non-existent keys instead of returning `undefined`. The library is designed for modern Node.js environments, requiring Node.js version 20 or higher since v5.0.0, ensuring compatibility with contemporary JavaScript features.
Common errors
error ERR_REQUIRE_ESM ↓
import statements and ensuring your package.json specifies "type": "module" or files end with .mjs. error TypeError: Cannot read properties of undefined (reading 'someProp') ↓
SafeProxy object, or wrap access in try...catch blocks, or use Reflect.has() to check for property existence before attempting to read it. error Error: Node.js version 18.x.x is not supported. Please upgrade to Node.js 20 or higher. ↓
Warnings
breaking Version 5.0.0 of object-lib bumped the minimum required Node.js version to 20. Projects running on older Node.js environments will encounter runtime errors or will be unable to install the package. ↓
gotcha The `SafeProxy` function fundamentally alters JavaScript's default behavior for property access. Instead of returning `undefined` for non-existent properties, it throws a `ReferenceError`. This can be unexpected and requires careful error handling or pre-flight checks if you frequently deal with optional properties. ↓
gotcha The `Merge` function's behavior is heavily configured by the `logic` object, which uses `object-scan` syntax. Misunderstanding or misconfiguring this logic can lead to unintended merging outcomes, data loss, or unexpected array concatenations instead of unique item merging. ↓
gotcha The `jsonSmartParse` function is designed to be lenient with invalid JSON, commonly found in LLM outputs. While useful, relying on it for strict JSON parsing could mask underlying data format issues or allow malformed data to be processed if strict validation is required. ↓
Install
npm install object-lib yarn add object-lib pnpm add object-lib Imports
- align wrong
const align = require('object-lib').align;correctimport { align } from 'object-lib'; - clone wrong
import clone from 'object-lib';correctimport { clone } from 'object-lib'; - Merge wrong
import { merge } from 'object-lib';correctimport { Merge } from 'object-lib'; - SafeProxy wrong
const SafeProxy = require('object-lib').SafeProxy;correctimport { SafeProxy } from 'object-lib'; - jsonSmartParse
import { jsonSmartParse } from 'object-lib';
Quickstart
import { align, clone, Merge, SafeProxy, jsonSmartParse } from 'object-lib';
// Align object keys to a reference object
const objToAlign = { k1: 1, k3: 3, k2: 2 };
const ref = { k2: null, k1: null, k4: 4 };
const aligned = align(objToAlign, ref);
console.log('Aligned object:', aligned);
// Expected: { k2: 2, k1: 1 }
// Deep clone with selective referencing/exclusion using object-scan syntax
const originalData = { a: { nested: 1 }, b: { nested: 2 }, c: { nested: 3 } };
const clonedData = clone(originalData, ['b', '!c']);
console.log('Cloned data:', clonedData);
console.log('Is `a` deep cloned?', clonedData.a !== originalData.a);
// Expected: true
console.log('Is `b` referenced?', clonedData.b === originalData.b);
// Expected: true
// Smartly merge objects based on a unique identifier (e.g., 'id')
const merged = Merge({ '**[*]': 'id' })(
{ items: [{ id: 1, val: 'a' }, { id: 2, val: 'b' }] },
{ items: [{ id: 2, val: 'c' }, { id: 3, val: 'd' }] }
);
console.log('Merged items:', merged);
// Expected: { items: [{ id: 1, val: 'a' }, { id: 2, val: 'c' }, { id: 3, val: 'd' }] }
// Create a SafeProxy for strict property access, throwing errors for non-existent keys
const config = SafeProxy({ db: { host: 'localhost' } });
try {
console.log('DB Host:', config.db.host);
// Attempting to access a non-existent property will throw
// console.log('Non-existent property:', config.db.port);
} catch (e) {
console.error('Caught error from SafeProxy:', e.message);
}
// Parse potentially invalid JSON strings (e.g., from LLMs)
const brokenJson = "{ name: 'Alice', age: 30, city: 'New York' }"; // Missing quotes on keys
const parsed = jsonSmartParse(brokenJson);
console.log('Smart parsed JSON:', parsed);
// Expected: { name: 'Alice', age: 30, city: 'New York' }