npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

interpolate-params

v2.0.1

Published

Interpolate parameters in a string pattern

Downloads

3

Readme

Interpolate Params

Installation

npm install interpolate-params --save

Then, in your app:

var interpolateParams = require('interpolate-params').interpolateParams;
// or
var interpolateParamsInFirstMatch = require('interpolate-params').interpolateParamsInFirstMatch;

API

interpolateParams(pattern, params, [map])

Interpolates params in the given parameterized pattern, using an optional map function (see its uses in the examples below).

If all pattern parameters appear in params, and map doesn't return null, the interpolation is considered successful. In this case, the interpolated pattern is returned along with the remaining params.

Otherwise, interpolateParams returns null.

pattern parameters must have letters only.

Example 1

var result = interpolateParams(
  '/users/:userId/friends/:friendId/photo',
  {
    friendId: '123',
    userId: '456',
    mood: 'Awesome'
  }
);

/* 
  Returns:
    {
      interpolatedPattern: '/users/456/friends/123/photo',
      remainingParams: {
        mood: 'Awesome'
      }
    }
*/

Example 2

var result = interpolateParams(
  '/users/:userId/friends/:friendId/photo',
  {
    friendId: '123',
    userId: '456',
    mood: 'Awesome'
  },
  function(param, value) {
    switch (param) {
      case 'userId':
        return value === '456' ? 'Misha' : null;

      case 'friendId':
        return value === '123' ? 'David' : null;

      default:
        return null;
    }
  }
);

/* 
  Returns:
    {
      interpolatedPattern: '/users/Misha/friends/David/photo',
      remainingParams: {
        mood: 'Awesome'
      }
    }
*/

Example 3

var result = interpolateParams(
  '/users/:userId/friends/:friendId/photo',
  {
    friendId: '999',
    userId: '456',
    mood: 'Awesome'
  },
  function(param, value) {
    switch (param) {
      case 'userId':
        return value === '456' ? 'Misha' : null;

      case 'friendId':
        return value === '123' ? 'David' : null;

      default:
        return null;
    }
  }
);

/* 
  Returns:
    null
    
  because friendId !== '123'
*/

Example 4

var result = interpolateParams(
  '/users/:userId/friends/:friendId/photo',
  {
    friendId: '123',
    mood: 'Awesome'
  }
);

/* 
  Returns:
    null
    
  because `userId` is not in `params`.
*/

interpolateParamsInFirstMatch(patterns, params)

Interpolates params in one of the parameterized patterns. Every pattern can have and optional map function. If none of the patterns match, interpolateParamsInFirstMatch returns null. Otherwise, it returns the first successful interpolation.

Example 1

function paramsMap(param, value) {
  switch (param) {
    case 'userId':
      return value === '456' ? 'Misha' : null;

    case 'friendId':
      return value === '123' ? 'David' : null;

    default:
      return null;
  }
}
  
var result = interpolateParamsInFirstMatch(
  [
    { pattern: '/users/:userId/friends/:friendId/photo', map: paramsMap },
    { pattern: '/users/:userId/friends/:friendId' },
    { pattern: '/users/:userId/friends' },
    { pattern: '/users/:userId' },
    { pattern: '/users' }
  ],
  {
    userId: '456',
    friendId: '999',
    mood: 'Awesome'
  }
);

/* 
  Returns:
    {
      interpolatedPattern: '/users/456/friends/999',
      remainingParams: {
        mood: 'Awesome'
      }
    }
    
  because the first pattern requires friendId === '123'
*/

Example 2

var result = interpolateParamsInFirstMatch(
  [
    { pattern: '/users/:userId/friends/:friendId/photo' },
    { pattern: '/users/:userId/friends/:friendId' },
    { pattern: '/users/:userId/friends' },
    { pattern: '/users/:userId' },
    { pattern: '/users' }
  ],
  {
    friendId: '123',
    mood: 'Awesome'
  }
);

/* 
  Returns:
    {
      interpolatedPattern: '/users',
      remainingParams: {
        friendId: '123',
        mood: 'Awesome'
      }
    }
    
  because every other pattern requires `userId` to be in params.
*/

Example 3

var result = interpolateParamsInFirstMatch(
  [
    { pattern: '/users/:userId/friends/:friendId/photo' },
    { pattern: '/users/:userId/friends/:friendId' },
    { pattern: '/users/:userId/friends' },
    { pattern: '/users/:userId' }
  ],
  {
    friendId: '123'
  }
);

/* 
  Returns:
    null
    
  because all the patterns require `userId` to be in params.
*/

Running Tests

npm test

License

MIT