Registry / devops / mpath
library0.9.0jsnpmunverified

Get and set nested JavaScript object properties using MongoDB-like dot notation, supporting array indexing and wildcard array paths. Version 0.9.0 is the latest stable release (last updated 2020). Lightweight, zero-dependency utility for manipulating deep object structures. Key differentiators: supports array property notation (e.g., 'comments.title' returns array of titles), optional map transform on get, and combined array indexing/wildcard for complex nested queries. Minimal API with only `get` and `set` methods.

npm install mpath
INSTALL
IMPORT
SIG · MPATH
M
mpath
devopsjavascriptv0.9.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mpath
const mpath = require('mpath');
import mpath from 'mpath';
The package uses CommonJS; no default ESM export. Use require().
get
const { get } = require('mpath');
Named export available as of v0.9.0. Also available as mpath.get.
set
const { set } = require('mpath');
Named export available as of v0.9.0. Also available as mpath.set.

Demonstrates basic get/set operations with array indexing and array property notation on nested objects.

const mpath = require('mpath'); const obj = { comments: [ { title: 'funny' }, { title: 'exciting!' } ] }; // Get single value by index const single = mpath.get('comments.1.title', obj); console.log(single); // 'exciting!' // Get array of values using array property notation const titles = mpath.get('comments.title', obj); console.log(titles); // ['funny', 'exciting!'] // Set value at path mpath.set('comments.0.title', 'amusing', obj); console.log(obj.comments[0].title); // 'amusing' // Set array of values to match array property notation mpath.set('comments.title', ['hilarious', 'fruity'], obj); console.log(obj.comments[1].title); // 'fruity'
Debug
Known issues
gotchaArray property notation returns an array of all matching values, but if a path segment is numeric, it indexes into the array instead of mapping.
fix
Use mpath.get('array.0.field', obj) for a specific index; use mpath.get('array.field', obj) to get an array of field values across all array elements.
affects: >=0.0.0
gotchaSetting via array property notation requires the provided value to be an array of the same length as the target array elements; otherwise behavior is undefined.
fix
Ensure the value array length matches the number of elements in the target array. For example, mpath.set('comments.title', ['a', 'b'], obj) when comments has two items.
affects: >=0.0.0
gotchampath.set does not create intermediate objects if they do not exist; it will throw or silently fail on undefined parent.
fix
Ensure the path exists up to the parent before setting. Consider initializing nested objects beforehand.
affects: >=0.0.0
deprecatedThe package has not been updated since 2020; no ESM support, no TypeScript types.
fix
Consider using lodash.get/set or native optional chaining for modern projects requiring ESM or TypeScript.
affects: 0.9.0
Errors
Common errors & fixes
TypeError: Cannot read property 'title' of undefined
mpath.get or mpath.set called with a path that references a non-existent property, and intermediate paths are undefined.
fix
Check that the path exists or use a safe-get pattern: mpath.get(path, obj) || defaultValue. For setting, ensure parent is defined.
mpath.set is not a function
Using destructured import incorrectly or ESM import with default export on a CJS-only package.
fix
Use const mpath = require('mpath'); then mpath.set(...). Do not use import mpath from 'mpath'.
RangeError: Invalid array length
Passing an array value to mpath.set with array property notation where the array length does not match the target number of elements.
fix
Ensure the value array length equals the number of items in the target array path. For example, if obj.items has 3 elements, value must be an array of length 3.
Upgrade
Version history
0.9.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
Resources
packagempath
mpath — npm install mpath · libregistry