Skip to content

jesdavpet/ramda-pathify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CircleCI

codecov

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'

About

Simplifies creating path arrays for functions in the Ramda.js library, using ES6 tagged template literal syntax.

Resources

License

Stars

Watchers

Forks

Packages

No packages published