Lodash toPath Internal Utility

3.8.1 · maintenance · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to use `lodash._topath` to parse various string and array representations of object paths into an array of segments, showcasing its handling of dot and bracket notation for robust property access.

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)' ]

view raw JSON →