🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

path-to-from-params

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

path-to-from-params

Friendlier wrapper for path-to-regexp that converts paths to/from plain objects.

1.0.3
latest
Source
npm
Version published
Weekly downloads
2
-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

Path to/from params

Build Status

Convert a path, e.g. places/france/paris/, that matches a pattern with Express-style placeholders, e.g. 'places/:country/:city' to/from an array of parameters, e.g. {country:'france',city:'paris'}

A a friendlier/easier wrapper for path-to-regexp. Built regexes are internally cached for performance and everything is fully unit tested with Jest.

Installation

npm install path-to-from-params

Usage

Get parameters from a path string

const { pathToParams } = require('path-to-from-params');
const params = pathToParams('places/:country/:city', 'places/france/paris');
console.log(params); // {country:'france',city:'paris'}

Build a path string from parameters

const { pathFromParams } = require('path-to-from-params');
const path = pathFromParams('places/:country/:city', {country:'france',city:'paris'});
console.log(path); // 'places/france/paris'

Get parameters from a path string (with optional parameters)

const { pathToParams } = require('path-to-from-params');
const params = pathToParams('places/:country/:city?', 'places/brazil');
console.log(params); // {country:'france',city:undefined}

Build a path string from parameters (with optional parameters)

const { pathFromParams } = require('path-to-from-params');
const path = pathFromParams('places/:country/:city?', {country:'brazil'});
console.log(path); // 'places/brazil'

API

pathToParams()

pathToParams(pattern, path)

Convert a path to a plain object of parameters. Returns an array any parameters named in the pattern that were found in the path, e.g. {country:'china',city:'shanghai'}

  • pattern (string)
    The pattern for the path, e.g. 'places/:country/:city?'

  • path (string)
    The path itself, e.g. 'places/china/shanghai'

pathFromParams()

pathFromParams(pattern, params)

Build a path from a pattern and an array of parameters. Returns the path string that was built, e.g. 'places/china/shanghai'

  • pattern (string)
    The pattern for the path, e.g. 'places/:country/:city?'

  • params (object)
    The object containing the parameters, e.g. {country:'china',city:'shanghai'}

FAQs

Package last updated on 14 Jul 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts