Lodash toPath Internal Utility
This package, `lodash._topath`, provides the internal `toPath` utility function from Lodash, specifically from its v3 branch. It is designed to convert various input values into a normalized array of path segments, which is crucial for higher-order Lodash functions like `_.get`, `_.set`, or `_.has` that operate on deeply nested object properties using string paths. While historically published as a standalone module for granular consumption, its direct use is generally discouraged in modern applications in favor of accessing equivalent path resolution functionality directly from the main `lodash` package or `lodash-es` (v4+), where `toPath` serves as an internal helper. The current stable version provided by this specific package is `3.8.1`. The parent Lodash project, from which this utility originates, follows semantic versioning and has an active, though somewhat infrequent for major versions, release cadence. Key differentiators of `toPath` include its robust handling of complex path syntax, such as dot notation, bracket notation for array indices or properties with special characters, and direct array inputs, providing a consistent and reliable way to abstract property access.
Common errors
-
SyntaxError: Unexpected token 'export' (or similar 'require is not defined')
cause Attempting to use ES module `import` syntax for `lodash._topath` (a CommonJS package) or using `require` in an ES module without proper configuration.fixFor `lodash._topath` (v3.x), always use `const toPath = require('lodash._topath');`. Ensure your environment supports CommonJS or configure transpilation correctly if mixing module types. -
SyntaxError: The requested module 'lodash' does not provide an export named 'toPath' (or similar for CommonJS)
cause The `toPath` function is an internal utility in modern Lodash (v4+) and is not directly exported from the main `lodash` or `lodash-es` packages.fixRely on public Lodash functions like `_.get`, `_.set`, `_.has` which internally handle path parsing. If you specifically need the `toPath` logic and are using Lodash v3.x, use `require('lodash._topath')`.
Warnings
- breaking This `lodash._topath` package is based on Lodash v3.x. When migrating projects to Lodash v4.0.0 or later, be aware that many core Lodash APIs and internal behaviors changed. Relying on `lodash._topath` directly in a Lodash v4+ environment may lead to unexpected behavior or redundancy, as `toPath` is primarily an internal utility in modern Lodash.
- gotcha The `lodash._topath` package (v3.x) is distributed as CommonJS. Attempting to use ES module `import` syntax (`import toPath from 'lodash._topath';`) will result in a runtime error.
- gotcha `lodash._topath` is an internal utility, originally published separately for modularity in Lodash v3. Direct reliance on internal modules is generally discouraged as their API surface or existence may change without adhering to semantic versioning for external consumers. In modern Lodash (v4+), `toPath` is strictly an internal helper.
- gotcha While this specific package (`lodash._topath` v3.x) does not directly manipulate objects, users should be aware of a prototype pollution vulnerability (GHSA-f23m-r3pf-42rh) in `lodash` v4.18.0 concerning `_.unset` and `_.omit`. If paths generated by this (or any) `toPath` utility are subsequently used with vulnerable versions of `_.unset` or `_.omit` from the main `lodash` library, it could lead to security issues.
Install
-
npm install lodash._topath -
yarn add lodash._topath -
pnpm add lodash._topath
Imports
- toPath
import toPath from 'lodash._topath';
const toPath = require('lodash._topath'); - toPath (via main Lodash)
import { toPath } from 'lodash';import { get } from 'lodash'; - toPath (TypeScript types)
type Path = string | Array<string | number | symbol>;
Quickstart
const toPath = require('lodash._topath');
// Example 1: Basic string path
const path1 = 'user.address.street';
console.log(`Path "${path1}" parsed:`, toPath(path1));
// Expected: [ 'user', 'address', 'street' ]
// Example 2: Path with array index
const path2 = 'data.items[0].name';
console.log(`Path "${path2}" parsed:`, toPath(path2));
// Expected: [ 'data', 'items', '0', 'name' ]
// Example 3: Path with mixed notation and special characters
const path3 = 'config["api-keys"][1].secret';
console.log(`Path "${path3}" parsed:`, toPath(path3));
// Expected: [ 'config', 'api-keys', '1', 'secret' ]
// Example 4: Already an array (should return itself)
const path4 = ['config', 'version'];
console.log(`Path ${JSON.stringify(path4)} parsed:`, toPath(path4));
// Expected: [ 'config', 'version' ]
// Example 5: Non-string, non-array input (should convert to string then path)
const path5 = Symbol('id');
console.log(`Path ${String(path5)} parsed:`, toPath(path5));
// Expected: [ 'Symbol(id)' ]