getPath
Problem
You need to safely read a deeply nested property from an object using a string path — without writing chained optional chaining or try-catch blocks.
Solution
Use getPath(item, path) for safe dot-notation access. getPathOr supplies a fallback. requirePath throws for missing values. Numeric bracket notation is supported.
ts
import { getPath, getPathOr, requirePath } from '@vielzeug/arsenal/object';
const config = { api: { host: 'localhost', port: 3000 }, items: [10, 20] };
getPath(config, 'api.port'); // 3000
getPath(config, 'items[1]'); // 20
getPath(config, 'api.timeout'); // undefined
getPathOr(config, 'api.timeout', 5_000); // 5000
requirePath(config, 'api.port'); // 3000
requirePath(config, 'api.timeout'); // throws TypeErrorPitfalls
- Unsafe segments (
__proto__,constructor,prototype) never resolve. getPathOrhandles missing andundefinedvalues.- Use
requirePathonly when a missing value is a programmer error.