extract-params
Advanced tools
Comparing version 0.3.0 to 0.4.0
36
index.js
@@ -1,33 +0,7 @@ | ||
var paramRegex = /:[a-zA-Z]+/g; | ||
var extractParams = require('./lib/extractParams'); | ||
var extractParamsInFirstMatch = require('./lib/extractParamsInFirstMatch'); | ||
// https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters | ||
function escapeRegexCharacters(str) { | ||
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
} | ||
module.exports = function extractParams(str, pattern) { | ||
var paramsMatch = pattern.match(paramRegex); | ||
if (paramsMatch === null) { | ||
return {}; | ||
} | ||
var params = paramsMatch.map(function(param) { | ||
return param.slice(1); // remove leading ':' | ||
}); | ||
var valuesRegex = new RegExp('^' + pattern.split(paramRegex).map(function(patternPart) { | ||
return escapeRegexCharacters(patternPart); | ||
}).join('(.+)')); | ||
var valuesMatch = str.match(valuesRegex); | ||
if (valuesMatch === null) { | ||
return {}; | ||
} | ||
return valuesMatch.slice(1).reduce(function(result, value, index) { | ||
result[params[index]] = value; | ||
return result; | ||
}, {}); | ||
module.exports = { | ||
extractParams: extractParams, | ||
extractParamsInFirstMatch: extractParamsInFirstMatch | ||
}; |
{ | ||
"name": "extract-params", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Extract parameters from a string based on a pattern", | ||
"main": "index.js", | ||
"scripts": { | ||
"lint": "eslint index.js tests.js", | ||
"test": "mocha tests", | ||
"test-dev": "mocha tests --watch --reporter min", | ||
"lint": "eslint lib test", | ||
"test": "mocha test", | ||
"test-dev": "mocha test --watch --reporter min", | ||
"build": "npm run lint && npm test" | ||
@@ -22,3 +22,7 @@ }, | ||
"keywords": [ | ||
"regex" | ||
"regex", | ||
"pattern", | ||
"matching", | ||
"pattern matching", | ||
"pattern-matching" | ||
], | ||
@@ -25,0 +29,0 @@ "devDependencies": { |
@@ -25,3 +25,5 @@ <a href="https://codeship.com/projects/119982" target="_blank"> | ||
```js | ||
var extractParams = require('extract-params'); | ||
var extractParams = require('extract-params').extractParams; | ||
// or | ||
var extractParamsInFirstMatch = require('extract-params').extractParamsInFirstMatch; | ||
``` | ||
@@ -31,12 +33,18 @@ | ||
* [`extractParams(str, pattern)`](#extractParams) | ||
* [`extractParamsInFirstMatch(str, patterns)`](#extractParamsInFirstMatch) | ||
<a name="extractParams"></a> | ||
### extractParams(str, pattern) | ||
This function extracts `pattern` parameters by matching `str` **at the start** (see examples below). | ||
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` | ||
If `str` doesn't match `pattern` at the start, returns `{}`. | ||
Match must occur from the first character of `str` in order to be considered successful (see examples below). | ||
Example 1: | ||
If match is not successful, `extractParams` returns `null`. | ||
#### Example 1 | ||
```js | ||
@@ -57,3 +65,3 @@ var params = extractParams( | ||
Example 2: | ||
#### Example 2 | ||
@@ -68,8 +76,62 @@ ```js | ||
Returns: | ||
{} | ||
null | ||
because `str` matches `pattern`, but not at the start. | ||
because `str` matches `pattern`, | ||
but not from the first character of `str`. | ||
*/ | ||
``` | ||
<a name="extractParamsInFirstMatch"></a> | ||
### extractParamsInFirstMatch(str, patterns) | ||
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 | ||
```js | ||
var params = extractParamsInFirstMatch( | ||
'/users/123', | ||
[ | ||
'/users/:userId/friends/:friendId/photo', | ||
'/users/:userId/friends/:friendId', | ||
'/users/:userId/friends', | ||
'/users/:userId', | ||
'/users' | ||
] | ||
); | ||
/* | ||
Returns: | ||
{ | ||
pattern: '/users/:userId', | ||
params: { | ||
userId: '123' | ||
} | ||
} | ||
*/ | ||
``` | ||
#### Example 2 | ||
```js | ||
var params = extractParamsInFirstMatch( | ||
'/users/123/subscriptions', | ||
[ | ||
'/users/:userId/friends/:friendId/photo', | ||
'/users/:userId/friends/:friendId', | ||
'/users/:userId/friends', | ||
'/users/:userId', | ||
'/users' | ||
] | ||
); | ||
/* | ||
Returns: | ||
null | ||
because none of the patterns match. | ||
*/ | ||
``` | ||
## Running Tests | ||
@@ -76,0 +138,0 @@ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
8807
9
144
142
1