Skip to content

Instantly share code, notes, and snippets.

@stukennedy
Last active May 24, 2017 08:28
Show Gist options
  • Save stukennedy/06c34385d749aa0b83118801f0e4e21d to your computer and use it in GitHub Desktop.
Save stukennedy/06c34385d749aa0b83118801f0e4e21d to your computer and use it in GitHub Desktop.
convert a path and resourcePath string into an object of parameters

take a path string and resourcePath string and parse into Object

import _ from 'lodash/fp'

const convertPath = (event) => {
  const path = _.flow(
    _.get('path'),
    _.split('/')
  )(event)
  const args = _.flow(
    _.get('resourcePath'),
    _.split('/'),
    _.map(_.replace(/{|}/g, ""))
  )(event)
  return _.flow(
    _.zip(args),
    _.map(_.uniq),
    _.fromPairs,
    _.pickBy(_.identity)
  )(path)
}

const event = { 
  path: '/api/customers/add',
  resourcePath: '/api/{model}/{action}'
}
console.log(convertPath(event))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment