@rwaterman/deep-object-rename
v2.3.0
Published
A Javascript library for deep object renaming by key or value, written in TypeScript
Maintainers
Readme
@rwaterman/deep-object-rename
Zero-dependency helper to deeply rename either (1) object keys or (2) object values.
ESM only. Requires Node.js 24+.
npm install @rwaterman/deep-object-renamerenameKeys
Pass a function that returns the new key, or the SkipRename symbol to leave it as is. Plain objects and arrays are traversed; anything else (Date, Map, Set, Buffer, class instances, ...) is passed through untouched. Circular and shared references are preserved.
import { renameKeys, SkipRename } from '@rwaterman/deep-object-rename';
const obj = { x: { y: { z: 123 } } };
const changed = renameKeys(obj, (key) => (key.startsWith('y') ? 'y_changed' : SkipRename));
// { x: { y_changed: { z: 123 } } }renameValues
Pass a function that returns the new value, or the SkipRename symbol to leave it as is. Only primitive values (string | number | boolean | null | undefined, see DeepRenameValue) are passed to the callback; plain objects and arrays are traversed, anything else (Date, Map, Set, class instances, ...) is passed through untouched. Circular and shared references are preserved.
import { renameValues, SkipRename, type DeepRenameValue } from '@rwaterman/deep-object-rename';
const obj = { x: { y: { y: 123, z: '123' }, x2: '123' } };
const changed = renameValues(obj, (val: DeepRenameValue) => (val === '123' ? 123 : SkipRename));
// { x: { y: { y: 123, z: 123 }, x2: 123 } }Development
npm test # vitest
npm run lint # oxlint (type-aware)
npm run build # tsc -> lib/*.mjs