@debkit/object
v1.2.0
Published
Deep clone, compare, and attach utilities for plain objects
Downloads
315
Maintainers
Readme
@debkit/object
Deep clone, compare, and attach utilities for plain objects. Zero dependencies.
Install
npm install @debkit/objectAPI
excludeFields<T>(data, excludes)
Deep clones an object and removes fields by path. Supports wildcard * for arrays.
import { excludeFields } from '@debkit/object';
const user = { name: 'John', password: 'secret', meta: { internal: true } };
const safe = excludeFields(user, ['password', 'meta.internal']);
// { name: 'John', meta: {} }
// Wildcard - remove 'id' from every item in an array
const data = { users: [{ id: 1, name: 'A' }, { id: 2, name: 'B' }] };
excludeFields(data, ['users[*].id' as any]);
// { users: [{ name: 'A' }, { name: 'B' }] }deepCompare(obj1, obj2, options?)
Recursively compares two objects for equality. Handles nested objects and arrays.
import { deepCompare } from '@debkit/object';
deepCompare({ a: 1, b: [2, 3] }, { a: 1, b: [2, 3] }); // true
deepCompare({ a: 1 }, { A: 1 }); // false
// Case-insensitive key comparison
deepCompare({ Name: 'John' }, { name: 'John' }, { sensitiveKey: true }); // trueattachField(baseObj, ...attaches)
Merges multiple objects into one (shallow).
import { attachField } from '@debkit/object';
const result = attachField({ a: 1 }, { b: 2 }, { c: 3 });
// { a: 1, b: 2, c: 3 }attachMethods<TData, TAttach>(data, attach)
Attaches methods to a data object with full type inference.
import { attachMethods } from '@debkit/object';
const user = attachMethods({ name: 'John', age: 30 }, ({ data }) => ({
greet: () => `Hello, ${data.name}!`,
isAdult: () => data.age >= 18,
}));
user.name; // 'John'
user.greet(); // 'Hello, John!'
user.isAdult(); // trueLicense
MIT
