doc-path is a JavaScript/TypeScript library designed for navigating and manipulating properties within JSON-like documents using simple, dot-separated path strings. It provides core functionalities like `evaluatePath` to retrieve values and `setPath` to assign values, even creating intermediate objects if the path doesn't exist. The current stable version is 4.1.3, indicating active maintenance and incremental feature enhancements. The library is primarily used in Node.js environments (requiring Node.js >=16) and ships with TypeScript types, ensuring robust usage in modern development workflows. A key differentiator is its ability to implicitly traverse arrays when a path segment matches a property name within array elements, returning an array of matched values, a feature often requiring explicit mapping in other object path libraries like Lodash. It also supports escaping dots within property names.
npm install doc-pathVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `evaluatePath` to retrieve values from a nested document, including array traversal and escaping dots in keys. It also shows `setPath` to modify existing properties and create new nested structures.
Escape dots in object keys with a double backslash in string literals, e.g., `path.evaluatePath(document, 'my\.key')` for a key named 'my.key'.
For documents with excessively deep nesting, consider flattening the data structure or implementing custom iteration logic that avoids deep recursion if performance or stack limits become an issue. No direct configuration option exists within `doc-path` to mitigate this.
For ESM, `import path from 'doc-path';` and then use `path.evaluatePath(...)`. For CommonJS, `const path = require('doc-path');` and then `path.evaluatePath(...)`.Escape the dot in the key name with a double backslash in string literals: `path.evaluatePath(document, 'product\\.code');`.
Refactor the data structure to reduce its maximum nesting depth, or for extremely large and deep documents, consider alternative flatter storage or partial loading strategies.
No dependency data recorded yet.