Skip to content
Simplifies creating path arrays for functions in the Ramda.js library, using ES6 tagged template literal syntax.
JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci
src
test
.gitignore
.npmignore
LICENSE
README.md
package-lock.json
package.json

README.md

CircleCI

ramda-pathify

When working with Ramda path* or lens functions a lot, hand-building path arrays gets tiresome.

Pathify builds path arrays for Ramda, so you don't have to!

Step 1. Install it!

> npm install ramda-pathify

Step 2. Use it!

Simply pathify a string that looks exactly how you'd reference the object property's path in normal JavaScript code.

Modern ES6 codebases can use pathify with tag syntax:

const { lensPath, view } = require('ramda')
const pathify = require('ramda-pathify')

const mcHammer = pathify`too.legit[0].tooLegit['to-quit']`
// ------------> ['too', 'legit', 0, 'tooLegit', 'to-quit']

const retro = lensPath(mcHammer)

const musicVideo = {
  too: {
    legit: [
      {
        tooLegit: { 'to-quit': 'https://youtu.be/wiyYozeOoKs' }
      }
    ]
  }
}

view(retro, musicVideo) // --> https://youtu.be/wiyYozeOoKs

Legacy ES5 codebases can use pathify as a function instead:

const R = require('ramda')
const pathify = require('ramda-pathify')

const mcHammer = pathify('too.legit[0].tooLegit["to-quit"]')
// ------------> ['too', 'legit', 0, 'tooLegit', 'to-quit']

const retro = R.lensPath(mcHammer)

const musicVideo = {
  too: {
    legit: [
      {
        tooLegit: { 'to-quit': 'https://youtu.be/wiyYozeOoKs' }
      }
    ]
  }
}

R.view(retro, musicVideo) // --> https://youtu.be/wiyYozeOoKs

Step 3. Profit !!!

Useful when refactoring code that has unsafe references to deeply nested properties.

- const unsafe = (band) => band.too.legit[0].tooLegit['to-quit']
- unsafe(vanillaIce) // --> Error! Cannot reference property "legit" of undefined.
+ const safe = pathOr(`N/A`, pathify`too.legit[0].tooLegit['to-quit']`)
+ safe(vanillaIce)  // --> 'N/A'
You can’t perform that action at this time.