Installation
npm install extract-params --save
Then, in your app:
var extractParams = require('extract-params').extractParams;
var extractParamsInFirstMatch = require('extract-params').extractParamsInFirstMatch;
API
Tests whether str
matches the given parameterized pattern
, and returns a key-value object of parameters and their values in case of a successful match.
pattern
parameters must be in the following format: :camelCase
Match must occur from the first character of str
in order to be considered successful (see examples below).
If match is not successful, extractParams
returns null
.
Example 1
var params = extractParams(
'/users/123/friends/456/photo',
'/users/:userId/friends/:friendId/photo'
);
Example 2
var params = extractParams(
'/home/users/123/friends/456/photo',
'/users/:userId/friends/:friendId/photo'
);
Tests whether str
matches one of the parameterized patterns
. If none of the patterns
match, extractParamsInFirstMatch
returns null
. Otherwise, it returns the matching pattern and its parameters.
Example 1
var params = extractParamsInFirstMatch(
'/users/123',
[
'/users/:userId/friends/:friendId/photo',
'/users/:userId/friends/:friendId',
'/users/:userId/friends',
'/users/:userId',
'/users'
]
);
Example 2
var params = extractParamsInFirstMatch(
'/users/123/subscriptions',
[
'/users/:userId/friends/:friendId/photo',
'/users/:userId/friends/:friendId',
'/users/:userId/friends',
'/users/:userId',
'/users'
]
);
Running Tests
npm test
License
MIT