Type-safe dot-notation access and mutation for nested objects
npm install @philiprehberger/dotpath-tsType-safe dot-notation access and mutation for nested objects
npm install @philiprehberger/dotpath-ts
import { get } from '@philiprehberger/dotpath-ts';
const obj = { a: { b: { c: 42 } } };
get(obj, 'a.b.c'); // 42
get(obj, 'a.b.missing'); // undefined
get(obj, 'a.b.missing', 0); // 0 (default value)
import { set } from '@philiprehberger/dotpath-ts';
const obj = { a: { b: { c: 42 } } };
const updated = set(obj, 'a.b.c', 100);
// updated.a.b.c === 100
// obj.a.b.c === 42 (original unchanged)
import { has } from '@philiprehberger/dotpath-ts';
const obj = { a: { b: { c: 42 } } };
has(obj, 'a.b.c'); // true
has(obj, 'a.b.missing'); // false
import { del } from '@philiprehberger/dotpath-ts';
const obj = { a: { b: { c: 42, d: 'hello' } } };
const result = del(obj, 'a.b.c');
// result.a.b === { d: 'hello' }
// obj.a.b.c === 42 (original unchanged)
import { update } from '@philiprehberger/dotpath-ts';
const obj = { a: { b: { count: 5 } } };
const result = update(obj, 'a.b.count', (v) => (v as number) * 2);
// result.a.b.count === 10
// obj.a.b.count === 5 (original unchanged)
| Function | Signature | Description |
|---|---|---|
get | get(obj, path, defaultValue?) | Retrieve a value at a dot-notation path |
set | set(obj, path, value) | Immutably set a value at a dot-notation path |
has | has(obj, path) | Check whether a path exists in an object |
del | del(obj, path) | Immutably remove a property at a dot-notation path |
update | update(obj, path, fn) | Get, transform, and immutably set a value at a path |
| Type | Description |
|---|---|
Paths<T> | All valid dot-path strings for an object type (depth limit 5) |
PathValue<T, P> | The type at a given dot path within an object type |
npm install
npm run build
npm test
If you find this project useful: