path-to-prop

3.0.0 • Public • Published

Path to prop 🛤

Total Downloads Latest Stable Version

npm i path-to-prop

Retrieves a property from an object based on a 'path/to.that.prop'

  • Supports paths with both / and . to separate props
  • Safely typed (with TypeScript) - returns unknown

Usage

import { getProp } from 'path-to-prop'

getProp({ nested: { prop: 1 } }, 'nested.prop') // 1

getProp({ nested: { prop: 1 } }, 'nested') // { prop: 1 }

You can use / or . to access nested props:

const obj = {a: {b: {c: {d: {e: 1}}}}}
const path = 'a/b/c.d.e'

getProp(obj, path)
  // returns 1

getProp(obj, 'nonexistent.prop')
  // returns undefined

When you have / or . in your prop names, use an array:

const obj = {'a/b': {'c.d': 1}}

getProp(obj, ['a/b', 'c.d'])
  // returns 1

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 3.0.0
    0
    • latest

Version History

Package Sidebar

Install

npm i path-to-prop

Weekly Downloads

1,309

Version

3.0.0

License

MIT

Unpacked Size

5.18 kB

Total Files

5

Last publish

Collaborators

  • mesqueeb