spread routes

This commit is contained in:
cudr
2019-03-11 17:37:20 +03:00
parent 7be7e1eb9f
commit 81f80e6215
6 changed files with 60 additions and 6 deletions

View File

@@ -246,13 +246,17 @@ type Part = {
content: string;
dynamic: boolean;
qualifier?: string;
spread?: boolean;
};
function comparator(
a: { basename: string, parts: Part[], file: string, is_index: boolean },
b: { basename: string, parts: Part[], file: string, is_index: boolean }
) {
if (a.is_index !== b.is_index) return a.is_index ? -1 : 1;
if (a.is_index !== b.is_index) {
const spread_pattern = /\[\.{3}/g;
return a.is_index && spread_pattern.test(a.file) ? 1 : -1;
}
const max = Math.max(a.parts.length, b.parts.length);
@@ -263,6 +267,14 @@ function comparator(
if (!a_sub_part) return 1; // b is more specific, so goes first
if (!b_sub_part) return -1;
// if spread && index, order later
if (a_sub_part.spread && b_sub_part.spread) {
return a.is_index ? 1 : -1
}
// If one is ...spread order it later
if (a_sub_part.spread !== b_sub_part.spread) return a_sub_part.spread ? 1 : -1;
if (a_sub_part.dynamic !== b_sub_part.dynamic) {
return a_sub_part.dynamic ? 1 : -1;
}
@@ -306,6 +318,7 @@ function get_parts(part: string): Part[] {
return {
content,
dynamic,
spread: /^\.{3}.+$/.test(content),
qualifier
};
})
@@ -333,7 +346,7 @@ function get_pattern(segments: Part[][], add_trailing_slash: boolean) {
segments.map(segment => {
return '\\/' + segment.map(part => {
return part.dynamic
? part.qualifier || '([^\\/]+?)'
? part.qualifier || part.spread ? '(.+)' : '([^\\/]+?)'
: encodeURI(part.content.normalize())
.replace(/\?/g, '%3F')
.replace(/#/g, '%23')