extract-params
Advanced tools
Comparing version 0.2.0 to 0.3.0
12
index.js
@@ -1,3 +0,8 @@ | ||
var paramRegex = /:[a-z]+/g; | ||
var paramRegex = /:[a-zA-Z]+/g; | ||
// 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) { | ||
@@ -14,3 +19,6 @@ var paramsMatch = pattern.match(paramRegex); | ||
var valuesRegex = new RegExp('^' + pattern.replace(paramRegex, '(.+)')); | ||
var valuesRegex = new RegExp('^' + pattern.split(paramRegex).map(function(patternPart) { | ||
return escapeRegexCharacters(patternPart); | ||
}).join('(.+)')); | ||
var valuesMatch = str.match(valuesRegex); | ||
@@ -17,0 +25,0 @@ |
{ | ||
"name": "extract-params", | ||
"version": "0.2.0", | ||
"version": "0.3.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", | ||
"build": "npm run lint && npm test" | ||
}, | ||
"repository": { | ||
@@ -15,7 +21,2 @@ "type": "git", | ||
"homepage": "https://github.com/moroshko/extract-params", | ||
"scripts": { | ||
"lint": "eslint index.js tests.js", | ||
"test": "mocha tests", | ||
"build": "npm run lint && npm test" | ||
}, | ||
"keywords": [ | ||
@@ -22,0 +23,0 @@ "regex" |
44
tests.js
@@ -6,2 +6,20 @@ var expect = require('chai').expect; | ||
{ | ||
should: 'not find any parameters if there is no match', | ||
str: 'everyone-knows-that-elm-is-awesome', | ||
pattern: 'he-said-that-:language-is-:description', | ||
result: {} | ||
}, | ||
{ | ||
should: 'not find any parameters if matches but not at the start', | ||
str: 'she-said-that-elm-is-awesome', | ||
pattern: 'he-said-that-:language-is-:description', | ||
result: {} | ||
}, | ||
{ | ||
should: 'not find any parameters if pattern has no parameters', | ||
str: 'react-is-awesome', | ||
pattern: 'awesome', | ||
result: {} | ||
}, | ||
{ | ||
should: 'find parameter', | ||
@@ -13,2 +31,28 @@ str: 'my-name-is-Misha', | ||
} | ||
}, | ||
{ | ||
should: 'find multiple parameters', | ||
str: '/users/123/friends/456/photo', | ||
pattern: '/users/:userId/friends/:friendId/photo', | ||
result: { | ||
userId: '123', | ||
friendId: '456' | ||
} | ||
}, | ||
{ | ||
should: 'handle special characters in the pattern', | ||
str: 'my+name-is-Misha', | ||
pattern: 'my+name-is-:name', | ||
result: { | ||
name: 'Misha' | ||
} | ||
}, | ||
{ | ||
should: 'handle many special characters in the pattern', | ||
str: 'my(name}-is-Misha-{${Moroshko[[', | ||
pattern: 'my(name}-is-:firstName-{${:lastName[[', | ||
result: { | ||
firstName: 'Misha', | ||
lastName: 'Moroshko' | ||
} | ||
} | ||
@@ -15,0 +59,0 @@ ]; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4764
88
80