mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-18 05:25:08 +00:00
Correct errors in param pattern and matching patterns
This commit is contained in:
@@ -90,13 +90,14 @@ export default function create_routes({ files } = { files: glob.sync('**/*.*', {
|
|||||||
) || '_';
|
) || '_';
|
||||||
|
|
||||||
const params: string[] = [];
|
const params: string[] = [];
|
||||||
const match_patterns: string[] = [];
|
const match_patterns: object = {};
|
||||||
const param_pattern = /\[([^\(]+)(?:\((.+?)\))?\]/g;
|
const param_pattern = /\[([^\(\]]+)(?:\((.+?)\))?\]/g;
|
||||||
let match;
|
let match;
|
||||||
while (match = param_pattern.exec(base)) {
|
while (match = param_pattern.exec(base)) {
|
||||||
params.push(match[1]);
|
params.push(match[1]);
|
||||||
if (typeof match[2] !== 'undefined') {
|
if (typeof match[2] !== 'undefined') {
|
||||||
match_patterns.push(`(${match[2]}?)`);
|
// Make a map of the regexp patterns
|
||||||
|
match_patterns[match[1]] = `(${match[2]}?)`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +111,13 @@ export default function create_routes({ files } = { files: glob.sync('**/*.*', {
|
|||||||
const dynamic = ~part.indexOf('[');
|
const dynamic = ~part.indexOf('[');
|
||||||
|
|
||||||
if (dynamic) {
|
if (dynamic) {
|
||||||
const matcher = part.replace(param_pattern, match_patterns[i] || `([^/]+?)`);
|
// Get keys from part and replace with stored match patterns
|
||||||
|
const keys = part.replace(/\(.*?\)/, '').split(/[\[\]]/).filter((x, i) => { if (i % 2) return x });
|
||||||
|
let matcher = part;
|
||||||
|
keys.forEach(k => {
|
||||||
|
const key_pattern = new RegExp('\\[' + k + '(?:\\((.+?)\\))?\\]');
|
||||||
|
matcher = matcher.replace(key_pattern, match_patterns[k] || `([^/]+?)`);
|
||||||
|
})
|
||||||
pattern_string = nested ? `(?:\\/${matcher}${pattern_string})?` : `\\/${matcher}${pattern_string}`;
|
pattern_string = nested ? `(?:\\/${matcher}${pattern_string})?` : `\\/${matcher}${pattern_string}`;
|
||||||
} else {
|
} else {
|
||||||
nested = false;
|
nested = false;
|
||||||
|
|||||||
Reference in New Issue
Block a user