path-to-regex
Advanced tools
Comparing version 1.1.3 to 1.2.1
21
index.js
@@ -22,3 +22,3 @@ module.exports = Regex; | ||
case: options.case||true, | ||
splitters: options.splitters||"/", | ||
separators: options.separators||"/", | ||
fromStart: options.fromStart||true, | ||
@@ -76,3 +76,3 @@ toEnd: options.toEnd||true | ||
const pattern = (pat?pat:"[^"+this.escape(this.options.splitters)+"]+"); | ||
const pattern = (pat?pat:"[^"+this.escape(this.options.separators)+"]+"); | ||
const isMultiple = (quant==="*" || quant==="+")?true:false; | ||
@@ -82,4 +82,4 @@ const isRequired = (quant!=="*" && quant!=="?")?true:false; | ||
const startChar = path.charAt(index-1); | ||
const isStarted = this.splitter(startChar); | ||
const isStoped = (index+str.length>=path.length)?true:this.splitter(path.charAt(index+str.length)); | ||
const isStarted = this.separator(startChar); | ||
const isStoped = (index+str.length>=path.length)?true:this.separator(path.charAt(index+str.length)); | ||
const isToken = isStarted && isStoped; | ||
@@ -97,3 +97,4 @@ | ||
if( !isRequired && isStarted || isMultiple ){ | ||
this.regstr+="?"; | ||
if(index) | ||
this.regstr+="?"; | ||
} | ||
@@ -136,3 +137,3 @@ | ||
this.regstr+ | ||
"["+this.escape(this.options.splitters)+"]?"+ | ||
"["+this.escape(this.options.separators)+"]?"+ | ||
(this.options.toEnd?"$":"") | ||
@@ -156,8 +157,8 @@ , | ||
/** | ||
* Метод проверяет является ли char одним из разделителей указанных в this.options.splitters | ||
* Метод проверяет является ли char одним из разделителей указанных в this.options.separators | ||
* @param {string} char Cтрока содержащая в себе проверяемый символ (длинна строки должна быть равна 1) | ||
* @return {boolean} Если проверяемый символ является одним из символов указанных в this.options.splitters то true иначе false | ||
* @return {boolean} Если проверяемый символ является одним из символов указанных в this.options.separators то true иначе false | ||
*/ | ||
Regex.prototype.splitter = function(char) { | ||
return !!(this.options.splitters.indexOf(char)+1); | ||
Regex.prototype.separator = function(char) { | ||
return !!(this.options.separators.indexOf(char)+1); | ||
} | ||
@@ -164,0 +165,0 @@ |
{ | ||
"name": "path-to-regex", | ||
"version": "1.1.3", | ||
"version": "1.2.1", | ||
"description": "Turn a path string such as /user/:id or /user/:id(\\d+) into a regular expression", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -28,7 +28,71 @@ # path-to-regex | ||
- **case** When `true` the regexp will be case sensitive. (default: `true`) | ||
- **splitters** The chars list for splited patch string. (default: `'/'`) | ||
- **separators** The chars list for splited patch string. (default: `'/'`) | ||
- **fromStart** When `true` the regexp will match from the beginning of the string. (default: `true`) | ||
- **toEnd** When `true` the regexp will match to the end of the string. (default: `true`) | ||
## How it works? | ||
It is important to understand how the key `:key` is interpreted depending on the pattern `:key(.*)` used and quantifiers `:key*`. The following examples will help you understand the logic for obtaining key values. | ||
#### the quantifier `*` will capture everything that is not a separator `options.separators` | ||
```javascript | ||
let parser = new pathToRegex(':path*'); | ||
// parser.regexp: /^((?:\[^\/]+)*)[\/]?$/ | ||
let result = parser.match('user'); // result: undefined | ||
let result = parser.match('/user'); // result: undefined | ||
let result = parser.match('user/'); // result: undefined | ||
let result = parser.match('/user/'); // result: undefined | ||
``` | ||
```javascript | ||
let parser = new pathToRegex('/:path*'); | ||
// parser.regexp: /^\/?((?:\/[^\/]+)*)[\/]?$/ | ||
let result = parser.match('user'); // result: undefined | ||
let result = parser.match('/user'); // result: { path: [ 'user' ] } | ||
let result = parser.match('user/'); // result: undefined | ||
let result = parser.match('/user/'); // result: { path: [ 'user' ] } | ||
``` | ||
#### Pattern `(...)`, in contrast quantifier, allows you to directly determine the valid key pattern. Such pattern `(.*)` will capture everything, including the splitter. | ||
```javascript | ||
let parser = new pathToRegex(':path(.*)'); | ||
// parser.regexp: /^(.*)[\/]?$/ | ||
let result = parser.match('user'); // result: { path: 'user' } | ||
let result = parser.match('/user'); // result: { path: '/user' } | ||
let result = parser.match('user/'); // result: { path: 'user/' } | ||
let result = parser.match('/user/'); // result: { path: '/user/' } | ||
``` | ||
```javascript | ||
let parser = new pathToRegex('/:path(.*)'); | ||
// parser.regexp: /^\/(.*)[\/]?$/ | ||
let result = parser.match('user'); // result: undefined | ||
let result = parser.match('/user'); // result: { path: 'user' } | ||
let result = parser.match('user/'); // result: undefined | ||
let result = parser.match('/user/'); // result: { path: 'user/' } | ||
``` | ||
## Samples | ||
The following examples clearly demonstrate the use of keys, their pattern quantifiers. | ||
@@ -35,0 +99,0 @@ #### Demonstration of processing a simple key identifier `:keyname` |
@@ -10,101 +10,112 @@ const PathToRegex = require("../index.js"); | ||
let parser = new PathToRegex(":path*"); | ||
let path = "user"; | ||
let re = parser.regexp; | ||
//let re = /^\/?((?:\/[^\/]+)*)[\/]?$/; | ||
//let re = /^\/?((?:\/[^\/]+)*)[\/]?$/; | ||
let result = path.match(re); | ||
console.log("REGEXP:", "\t\t", re); | ||
console.log("RESULT:", "\t\t", result); | ||
describe("Тестируем модуль преобразования пути в RegExp", function() { | ||
it("проверяем пооддержку именовынных групп в RegExp", function() { | ||
let re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u; | ||
let result = re.exec('2015-01-02'); | ||
assert.equal( result.groups.year, '2015'); | ||
assert.equal( result.groups.month, '01'); | ||
assert.equal( result.groups.day, '02'); | ||
}); | ||
// it("проверяем пооддержку именовынных групп в RegExp", function() { | ||
// let re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/u; | ||
// let result = re.exec('2015-01-02'); | ||
// assert.equal( result.groups.year, '2015'); | ||
// assert.equal( result.groups.month, '01'); | ||
// assert.equal( result.groups.day, '02'); | ||
// }); | ||
const re1 = new PathToRegex("/[\\s\\S]*"); | ||
// const re1 = new PathToRegex("/:path*"); | ||
// console.log("REGEX:", "\t\t"+re1.regexp, "\t\t", re1.regexp, "\t\t", re1.regstr, "\t\t", re1.path); | ||
it("создаем RegExp из \"/[\\s\\S]*\"", function() { | ||
assert.equal( re1 instanceof PathToRegex, true); | ||
assert.equal( ""+re1.regexp, ""+/^\/[\s\S]*$/ ); | ||
}); | ||
// it("создаем RegExp из '/:path*'", function() { | ||
// assert.equal( re1 instanceof PathToRegex, true); | ||
// assert.equal( ""+re1.regexp, ""+/^\/?((?:\/[^\/]+)*)[\/]?$/ ); | ||
// }); | ||
it("соотносим RegExp /^\\/[\\s\\S]*$/ со строками \"/\" \"/user\" \"/user/12345\" ", function() { | ||
assert.equal( !!re1.match("/"), true); | ||
assert.equal( !!re1.match("/user"), true); | ||
assert.equal( !!re1.match("/user/12345"), true); | ||
}); | ||
// it("соотносим шаблон '/:path*' со строками '/' '/user' '/user/12345' ", function() { | ||
// assert.equal( re1.match("/"), ""+{path:''}); | ||
// assert.equal( re1.match("/user"), ""+{path:'user'}); | ||
// assert.equal( re1.match("/user/12345"), ""+{path:'user/12345'}); | ||
// }); | ||
it("соотносим RegExp /^\\/[\\s\\S]*$/ со строками \"\" \"user\" \"user/12345\" ", function() { | ||
assert.equal( !!re1.match(""), false); | ||
assert.equal( !!re1.match("user"), false); | ||
assert.equal( !!re1.match("user/12345"), false); | ||
}); | ||
// it("соотносим шаблон '/:path*' со строками '' 'user' 'user/12345' ", function() { | ||
// assert.equal( ""+re1.match("") , { path: [] }); | ||
// assert.equal( ""+re1.match("user/"), { path: [] }); | ||
// assert.equal( ""+re1.match("user/12345"), { path: [] }); | ||
// }); | ||
const re2 = new PathToRegex("/user/:id"); | ||
// console.log("REGEX:", "\t\t"+re2.regexp, "\t\t", re2.regexp, "\t\t", re2.regstr, "\t\t", re2.path); | ||
it("создаем RegExp из \"/user/:id\"", function() { | ||
assert.equal( re2 instanceof PathToRegex, true); | ||
assert.equal( ""+re2.regexp, ""+/^\/user\/(?<id>[^\/]+?)$/ ); | ||
}); | ||
// const re2 = new PathToRegex("/user/:id"); | ||
// // console.log("REGEX:", "\t\t"+re2.regexp, "\t\t", re2.regexp, "\t\t", re2.regstr, "\t\t", re2.path); | ||
// it("создаем RegExp из \"/user/:id\"", function() { | ||
// assert.equal( re2 instanceof PathToRegex, true); | ||
// assert.equal( ""+re2.regexp, ""+/^\/user\/(?<id>[^\/]+?)$/ ); | ||
// }); | ||
it("соотносим RegExp /^\\/user\\/(?<id>[^\\/]+?)$/ (путь \"/user/:id\" ) со строками \"/\" \"/user\" \"/user/12345\" \"/user/12345/foo\" ", function() { | ||
assert.equal( !!re2.match("/"), false); | ||
assert.equal( !!re2.match("/user"), false); | ||
assert.equal( !!re2.match("/user/12345"), true); | ||
assert.equal( !!re2.match("/user/12345/foo"), false); | ||
}); | ||
// it("соотносим RegExp /^\\/user\\/(?<id>[^\\/]+?)$/ (путь \"/user/:id\" ) со строками \"/\" \"/user\" \"/user/12345\" \"/user/12345/foo\" ", function() { | ||
// assert.equal( !!re2.match("/"), false); | ||
// assert.equal( !!re2.match("/user"), false); | ||
// assert.equal( !!re2.match("/user/12345"), true); | ||
// assert.equal( !!re2.match("/user/12345/foo"), false); | ||
// }); | ||
it("соотносим RegExp /^\\/user\\/(?<id>[^\\/]+?)$/ (путь \"/user/:id\" ) со строками \"/user/12345\" \"/user/abcd\" и проверяем наличие ключа id и соответствие его значения", function() { | ||
const params1 = re2.match("/user/12345"); | ||
const params2 = re2.match("/user/abcd"); | ||
assert.equal( params1.id, "12345"); | ||
assert.equal( params2.id, "abcd"); | ||
}); | ||
// it("соотносим RegExp /^\\/user\\/(?<id>[^\\/]+?)$/ (путь \"/user/:id\" ) со строками \"/user/12345\" \"/user/abcd\" и проверяем наличие ключа id и соответствие его значения", function() { | ||
// const params1 = re2.match("/user/12345"); | ||
// const params2 = re2.match("/user/abcd"); | ||
// assert.equal( params1.id, "12345"); | ||
// assert.equal( params2.id, "abcd"); | ||
// }); | ||
const re3 = new PathToRegex("/foo/:fooid/bar/:barid"); | ||
// const re3 = new PathToRegex("/foo/:fooid/bar/:barid"); | ||
it("создаем RegExp из \"/foo/:fooid/bar/:barid\"", function() { | ||
assert.equal( re3 instanceof PathToRegex, true); | ||
assert.equal( ""+re3.regexp, ""+/^\/foo\/(?<fooid>[^\/]+?)\/bar\/(?<barid>[^\/]+?)$/ ); | ||
}); | ||
// it("создаем RegExp из \"/foo/:fooid/bar/:barid\"", function() { | ||
// assert.equal( re3 instanceof PathToRegex, true); | ||
// assert.equal( ""+re3.regexp, ""+/^\/foo\/(?<fooid>[^\/]+?)\/bar\/(?<barid>[^\/]+?)$/ ); | ||
// }); | ||
it("соотносим RegExp /^\\/foo\\/(?<fooid>[^\\/]+?)\\/bar\\/(?<barid>[^\\/]+?)$/ (путь \"/foo/:fooid/bar/:barid\" ) со строками \"/\" \"/foo\" \"/foo/123\" \"/foo/123/bar\" \"/foo/123/bar/456\" ", function() { | ||
assert.equal( !!re3.match("/"), false); | ||
assert.equal( !!re3.match("/foo"), false); | ||
assert.equal( !!re3.match("/foo/123"), false); | ||
assert.equal( !!re3.match("/foo/123/bar"), false); | ||
const params = re3.match("/foo/123/bar/456"); | ||
assert.equal( !!params, true); | ||
}); | ||
// it("соотносим RegExp /^\\/foo\\/(?<fooid>[^\\/]+?)\\/bar\\/(?<barid>[^\\/]+?)$/ (путь \"/foo/:fooid/bar/:barid\" ) со строками \"/\" \"/foo\" \"/foo/123\" \"/foo/123/bar\" \"/foo/123/bar/456\" ", function() { | ||
// assert.equal( !!re3.match("/"), false); | ||
// assert.equal( !!re3.match("/foo"), false); | ||
// assert.equal( !!re3.match("/foo/123"), false); | ||
// assert.equal( !!re3.match("/foo/123/bar"), false); | ||
// const params = re3.match("/foo/123/bar/456"); | ||
// assert.equal( !!params, true); | ||
// }); | ||
it("соотносим RegExp /^\\/foo\\/(?<fooid>[^\\/]+?)\\/bar\\/(?<barid>[^\\/]+?)$/ (путь \"/foo/:fooid/bar/:barid\" ) со строками \"/foo/111/bar/222\" \"/foo/id1/bar/id22\" и проверяем наличие ключей fooid и barid и соответствие их значений", function() { | ||
const params1 = re3.match("/foo/111/bar/222"); | ||
const params2 = re3.match("/foo/id1/bar/id22"); | ||
assert.equal( params1.fooid, "111"); | ||
assert.equal( params1.barid, "222"); | ||
assert.equal( params2.fooid, "id1"); | ||
assert.equal( params2.barid, "id22"); | ||
}); | ||
// it("соотносим RegExp /^\\/foo\\/(?<fooid>[^\\/]+?)\\/bar\\/(?<barid>[^\\/]+?)$/ (путь \"/foo/:fooid/bar/:barid\" ) со строками \"/foo/111/bar/222\" \"/foo/id1/bar/id22\" и проверяем наличие ключей fooid и barid и соответствие их значений", function() { | ||
// const params1 = re3.match("/foo/111/bar/222"); | ||
// const params2 = re3.match("/foo/id1/bar/id22"); | ||
// assert.equal( params1.fooid, "111"); | ||
// assert.equal( params1.barid, "222"); | ||
// assert.equal( params2.fooid, "id1"); | ||
// assert.equal( params2.barid, "id22"); | ||
// }); | ||
const re4 = new PathToRegex("/user/:id(\\d+)"); | ||
console.log("REGEX:", "\t\t"+re4.regexp, "\t\t", re4.regexp, "\t\t", re4.regstr, "\t\t", re4.path); | ||
// console.log("REGEX:", "\t\t"+re3.regexp, "\t\t", re3.regexp, "\t\t", re3.regstr, "\t\t", re3.path, "\t\t", re3.keys); | ||
// console.log("REGEX:", "\t\t", params); | ||
it("создаем RegExp из \"/user/:id(\\\\d+)\"", function() { | ||
assert.equal( re4 instanceof PathToRegex, true); | ||
assert.equal( ""+re4.regexp, ""+/^\/user\/(?<id>\d+)$/ ); | ||
}); | ||
// const re4 = new PathToRegex("/user/:id(\\d+)"); | ||
// console.log("REGEX:", "\t\t"+re4.regexp, "\t\t", re4.regexp, "\t\t", re4.regstr, "\t\t", re4.path); | ||
// // console.log("REGEX:", "\t\t"+re3.regexp, "\t\t", re3.regexp, "\t\t", re3.regstr, "\t\t", re3.path, "\t\t", re3.keys); | ||
// // console.log("REGEX:", "\t\t", params); | ||
// it("создаем RegExp из \"/user/:id(\\\\d+)\"", function() { | ||
// assert.equal( re4 instanceof PathToRegex, true); | ||
// assert.equal( ""+re4.regexp, ""+/^\/user\/(?<id>\d+)$/ ); | ||
// }); | ||
it("соотносим RegExp /^\\/user\\/(?<id>\\d+)$/ (путь \"/user/:id(\\\\d+)\" ) со строками \"/\" \"/user\" \"/user/123\" \"/user/123/foo\" \"/user/aaa\" ", function() { | ||
assert.equal( !!re4.match("/"), false); | ||
assert.equal( !!re4.match("/user"), false); | ||
const params = re4.match("/user/123"); | ||
assert.equal( !!params, true); | ||
assert.equal( params.id, "123"); | ||
assert.equal( !!re4.match("/user/123/foo"), false); | ||
assert.equal( !!re4.match("/user/aaa"), false); | ||
}); | ||
// it("соотносим RegExp /^\\/user\\/(?<id>\\d+)$/ (путь \"/user/:id(\\\\d+)\" ) со строками \"/\" \"/user\" \"/user/123\" \"/user/123/foo\" \"/user/aaa\" ", function() { | ||
// assert.equal( !!re4.match("/"), false); | ||
// assert.equal( !!re4.match("/user"), false); | ||
// const params = re4.match("/user/123"); | ||
// assert.equal( !!params, true); | ||
// assert.equal( params.id, "123"); | ||
// assert.equal( !!re4.match("/user/123/foo"), false); | ||
// assert.equal( !!re4.match("/user/aaa"), false); | ||
// }); | ||
}); |
Possible typosquat attack
Supply chain riskThere is a package with a similar name that is downloaded much more often.
Did you mean |
---|
path-to-regexp |
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
23373
268
290
1