Interpolate Params
Installation
npm install interpolate-params --save
Then, in your app:
var interpolateParams = interpolateParams;// orvar interpolateParamsInFirstMatch = 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 = ; /* Returns: { interpolatedPattern: '/users/456/friends/123/photo', remainingParams: { mood: 'Awesome' } }*/
Example 2
var result = ; /* Returns: { interpolatedPattern: '/users/Misha/friends/David/photo', remainingParams: { mood: 'Awesome' } }*/
Example 3
var result = ; /* Returns: null because friendId !== '123'*/
Example 4
var result = ; /* 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
{ } var result = ; /* Returns: { interpolatedPattern: '/users/456/friends/999', remainingParams: { mood: 'Awesome' } } because the first pattern requires friendId === '123'*/
Example 2
var result = ; /* Returns: { interpolatedPattern: '/users', remainingParams: { friendId: '123', mood: 'Awesome' } } because every other pattern requires `userId` to be in params.*/
Example 3
var result = ; /* Returns: null because all the patterns require `userId` to be in params.*/
Running Tests
npm test