Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

extract-params

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extract-params - npm Package Compare versions

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 @@

13

package.json
{
"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"

@@ -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 @@ ];

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc