piral-core
Advanced tools
Comparing version 1.6.2-beta.7367 to 1.6.2-beta.7393
@@ -1,5 +0,103 @@ | ||
import ptr from 'path-to-regexp'; | ||
const defaultDelimiter = escapeString('/'); | ||
const pathExpr = new RegExp([ | ||
'(\\\\.)', | ||
'([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))', | ||
].join('|'), 'g'); | ||
function escapeString(str) { | ||
return str.replace(/([.+*?=^!:${}()[\]|\/\\])/g, '\\$1'); | ||
} | ||
function escapeGroup(group) { | ||
return group.replace(/([=!:$\/()])/g, '\\$1'); | ||
} | ||
function parse(str) { | ||
const tokens = []; | ||
let key = 0; | ||
let index = 0; | ||
let path = ''; | ||
let res; | ||
while ((res = pathExpr.exec(str)) !== null) { | ||
const m = res[0]; | ||
const escaped = res[1]; | ||
const offset = res.index; | ||
path += str.slice(index, offset); | ||
index = offset + m.length; | ||
// Ignore already escaped sequences. | ||
if (escaped) { | ||
path += escaped[1]; | ||
continue; | ||
} | ||
const next = str[index]; | ||
const prefix = res[2]; | ||
const name = res[3]; | ||
const capture = res[4]; | ||
const group = res[5]; | ||
const modifier = res[6]; | ||
const asterisk = res[7]; | ||
// Push the current path onto the tokens. | ||
if (path) { | ||
tokens.push(path); | ||
path = ''; | ||
} | ||
const partial = prefix != null && next != null && next !== prefix; | ||
const repeat = modifier === '+' || modifier === '*'; | ||
const optional = modifier === '?' || modifier === '*'; | ||
const delimiter = res[2] || '/'; | ||
const pattern = capture || group; | ||
tokens.push({ | ||
name: name || `${key++}`, | ||
prefix: prefix || '', | ||
delimiter, | ||
optional, | ||
repeat, | ||
partial, | ||
asterisk: !!asterisk, | ||
pattern: pattern ? escapeGroup(pattern) : asterisk ? '.*' : '[^' + escapeString(delimiter) + ']+?', | ||
}); | ||
} | ||
// Match any characters still remaining. | ||
if (index < str.length) { | ||
path += str.substring(index); | ||
} | ||
// If the path exists, push it onto the end. | ||
if (path) { | ||
tokens.push(path); | ||
} | ||
return tokens; | ||
} | ||
function tokensToRegExp(tokens) { | ||
let route = ''; | ||
for (const token of tokens) { | ||
if (typeof token === 'string') { | ||
route += escapeString(token); | ||
} | ||
else { | ||
const prefix = escapeString(token.prefix); | ||
let capture = '(?:' + token.pattern + ')'; | ||
if (token.repeat) { | ||
capture += '(?:' + prefix + capture + ')*'; | ||
} | ||
if (token.optional) { | ||
if (!token.partial) { | ||
capture = '(?:' + prefix + '(' + capture + '))?'; | ||
} | ||
else { | ||
capture = prefix + '(' + capture + ')?'; | ||
} | ||
} | ||
else { | ||
capture = prefix + '(' + capture + ')'; | ||
} | ||
route += capture; | ||
} | ||
} | ||
const endsWithDelimiter = route.slice(-defaultDelimiter.length) === defaultDelimiter; | ||
const path = endsWithDelimiter ? route.slice(0, -defaultDelimiter.length) : route; | ||
return new RegExp(`^${path}(?:${defaultDelimiter}(?=$))?$`, 'i'); | ||
} | ||
function stringToRegexp(path) { | ||
return tokensToRegExp(parse(path)); | ||
} | ||
export function createRouteMatcher(path) { | ||
return ptr(path); | ||
return stringToRegexp(path); | ||
} | ||
//# sourceMappingURL=routes.js.map |
{ | ||
"name": "piral-core", | ||
"version": "1.6.2-beta.7367", | ||
"version": "1.6.2-beta.7393", | ||
"description": "The core library for creating a Piral instance.", | ||
@@ -64,5 +64,4 @@ "keywords": [ | ||
"dependencies": { | ||
"path-to-regexp": "^1.8.0", | ||
"piral-base": "1.6.2-beta.7367", | ||
"piral-debug-utils": "1.6.2-beta.7367", | ||
"piral-base": "1.6.2-beta.7393", | ||
"piral-debug-utils": "1.6.2-beta.7393", | ||
"zustand": "^3.0.0" | ||
@@ -87,3 +86,3 @@ }, | ||
], | ||
"gitHead": "8d7ec61c81bdc200aad2233e8b01f41f9f63264b" | ||
"gitHead": "75d596d714a582f54071e1799e0ce183dbacbb09" | ||
} |
@@ -1,5 +0,135 @@ | ||
import ptr from 'path-to-regexp'; | ||
const defaultDelimiter = escapeString('/'); | ||
const pathExpr = new RegExp( | ||
[ | ||
'(\\\\.)', | ||
'([\\/.])?(?:(?:\\:(\\w+)(?:\\(((?:\\\\.|[^\\\\()])+)\\))?|\\(((?:\\\\.|[^\\\\()])+)\\))([+*?])?|(\\*))', | ||
].join('|'), | ||
'g', | ||
); | ||
function escapeString(str: string) { | ||
return str.replace(/([.+*?=^!:${}()[\]|\/\\])/g, '\\$1'); | ||
} | ||
function escapeGroup(group: string) { | ||
return group.replace(/([=!:$\/()])/g, '\\$1'); | ||
} | ||
type Token = | ||
| string | ||
| { | ||
name: string; | ||
prefix: string; | ||
delimiter: string; | ||
optional: boolean; | ||
repeat: boolean; | ||
partial: boolean; | ||
asterisk: boolean; | ||
pattern: string; | ||
}; | ||
function parse(str: string) { | ||
const tokens: Array<Token> = []; | ||
let key = 0; | ||
let index = 0; | ||
let path = ''; | ||
let res: RegExpExecArray; | ||
while ((res = pathExpr.exec(str)) !== null) { | ||
const m = res[0]; | ||
const escaped = res[1]; | ||
const offset = res.index; | ||
path += str.slice(index, offset); | ||
index = offset + m.length; | ||
// Ignore already escaped sequences. | ||
if (escaped) { | ||
path += escaped[1]; | ||
continue; | ||
} | ||
const next = str[index]; | ||
const prefix = res[2]; | ||
const name = res[3]; | ||
const capture = res[4]; | ||
const group = res[5]; | ||
const modifier = res[6]; | ||
const asterisk = res[7]; | ||
// Push the current path onto the tokens. | ||
if (path) { | ||
tokens.push(path); | ||
path = ''; | ||
} | ||
const partial = prefix != null && next != null && next !== prefix; | ||
const repeat = modifier === '+' || modifier === '*'; | ||
const optional = modifier === '?' || modifier === '*'; | ||
const delimiter = res[2] || '/'; | ||
const pattern = capture || group; | ||
tokens.push({ | ||
name: name || `${key++}`, | ||
prefix: prefix || '', | ||
delimiter, | ||
optional, | ||
repeat, | ||
partial, | ||
asterisk: !!asterisk, | ||
pattern: pattern ? escapeGroup(pattern) : asterisk ? '.*' : '[^' + escapeString(delimiter) + ']+?', | ||
}); | ||
} | ||
// Match any characters still remaining. | ||
if (index < str.length) { | ||
path += str.substring(index); | ||
} | ||
// If the path exists, push it onto the end. | ||
if (path) { | ||
tokens.push(path); | ||
} | ||
return tokens; | ||
} | ||
function tokensToRegExp(tokens: Array<Token>) { | ||
let route = ''; | ||
for (const token of tokens) { | ||
if (typeof token === 'string') { | ||
route += escapeString(token); | ||
} else { | ||
const prefix = escapeString(token.prefix); | ||
let capture = '(?:' + token.pattern + ')'; | ||
if (token.repeat) { | ||
capture += '(?:' + prefix + capture + ')*'; | ||
} | ||
if (token.optional) { | ||
if (!token.partial) { | ||
capture = '(?:' + prefix + '(' + capture + '))?'; | ||
} else { | ||
capture = prefix + '(' + capture + ')?'; | ||
} | ||
} else { | ||
capture = prefix + '(' + capture + ')'; | ||
} | ||
route += capture; | ||
} | ||
} | ||
const endsWithDelimiter = route.slice(-defaultDelimiter.length) === defaultDelimiter; | ||
const path = endsWithDelimiter ? route.slice(0, -defaultDelimiter.length) : route; | ||
return new RegExp(`^${path}(?:${defaultDelimiter}(?=$))?$`, 'i'); | ||
} | ||
function stringToRegexp(path: string) { | ||
return tokensToRegExp(parse(path)); | ||
} | ||
export function createRouteMatcher(path: string): RegExp { | ||
return ptr(path); | ||
return stringToRegexp(path); | ||
} |
Sorry, the diff of this file is not supported yet
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
708494
3
17906
+ Addedpiral-base@1.6.2-beta.7393(transitive)
+ Addedpiral-debug-utils@1.6.2-beta.7393(transitive)
- Removedpath-to-regexp@^1.8.0
- Removedisarray@0.0.1(transitive)
- Removedpath-to-regexp@1.9.0(transitive)
- Removedpiral-base@1.6.2-beta.7367(transitive)
- Removedpiral-debug-utils@1.6.2-beta.7367(transitive)
Updatedpiral-base@1.6.2-beta.7393