1 | "use strict";exports.__esModule=true;exports.getSortedRoutes=getSortedRoutes;class UrlNode{constructor(){this.placeholder=true;this.children=new Map();this.slugName=null;this.restSlugName=null;this.optionalRestSlugName=null;}insert(urlPath){this._insert(urlPath.split('/').filter(Boolean),[],false);}smoosh(){return this._smoosh();}_smoosh(prefix='/'){const childrenPaths=[...this.children.keys()].sort();if(this.slugName!==null){childrenPaths.splice(childrenPaths.indexOf('[]'),1);}if(this.restSlugName!==null){childrenPaths.splice(childrenPaths.indexOf('[...]'),1);}if(this.optionalRestSlugName!==null){childrenPaths.splice(childrenPaths.indexOf('[[...]]'),1);}const routes=childrenPaths.map(c=>this.children.get(c)._smoosh(`${prefix}${c}/`)).reduce((prev,curr)=>[...prev,...curr],[]);if(this.slugName!==null){routes.push(...this.children.get('[]')._smoosh(`${prefix}[${this.slugName}]/`));}if(!this.placeholder){const r=prefix==='/'?'/':prefix.slice(0,-1);if(this.optionalRestSlugName!=null){throw new Error(`You cannot define a route with the same specificity as a optional catch-all route ("${r}" and "${r}[[...${this.optionalRestSlugName}]]").`);}routes.unshift(r);}if(this.restSlugName!==null){routes.push(...this.children.get('[...]')._smoosh(`${prefix}[...${this.restSlugName}]/`));}if(this.optionalRestSlugName!==null){routes.push(...this.children.get('[[...]]')._smoosh(`${prefix}[[...${this.optionalRestSlugName}]]/`));}return routes;}_insert(urlPaths,slugNames,isCatchAll){if(urlPaths.length===0){this.placeholder=false;return;}if(isCatchAll){throw new Error(`Catch-all must be the last part of the URL.`);}
|
2 | let nextSegment=urlPaths[0];
|
3 | if(nextSegment.startsWith('[')&&nextSegment.endsWith(']')){
|
4 | let segmentName=nextSegment.slice(1,-1);let isOptional=false;if(segmentName.startsWith('[')&&segmentName.endsWith(']')){
|
5 | segmentName=segmentName.slice(1,-1);isOptional=true;}if(segmentName.startsWith('...')){
|
6 | segmentName=segmentName.substring(3);isCatchAll=true;}if(segmentName.startsWith('[')||segmentName.endsWith(']')){throw new Error(`Segment names may not start or end with extra brackets ('${segmentName}').`);}if(segmentName.startsWith('.')){throw new Error(`Segment names may not start with erroneous periods ('${segmentName}').`);}function handleSlug(previousSlug,nextSlug){if(previousSlug!==null){
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | if(previousSlug!==nextSlug){
|
12 | throw new Error(`You cannot use different slug names for the same dynamic path ('${previousSlug}' !== '${nextSlug}').`);}}slugNames.forEach(slug=>{if(slug===nextSlug){throw new Error(`You cannot have the same slug name "${nextSlug}" repeat within a single dynamic path`);}if(slug.replace(/\W/g,'')===nextSegment.replace(/\W/g,'')){throw new Error(`You cannot have the slug names "${slug}" and "${nextSlug}" differ only by non-word symbols within a single dynamic path`);}});slugNames.push(nextSlug);}if(isCatchAll){if(isOptional){if(this.restSlugName!=null){throw new Error(`You cannot use both an required and optional catch-all route at the same level ("[...${this.restSlugName}]" and "${urlPaths[0]}" ).`);}handleSlug(this.optionalRestSlugName,segmentName);
|
13 | this.optionalRestSlugName=segmentName;
|
14 | nextSegment='[[...]]';}else{if(this.optionalRestSlugName!=null){throw new Error(`You cannot use both an optional and required catch-all route at the same level ("[[...${this.optionalRestSlugName}]]" and "${urlPaths[0]}").`);}handleSlug(this.restSlugName,segmentName);
|
15 | this.restSlugName=segmentName;
|
16 | nextSegment='[...]';}}else{if(isOptional){throw new Error(`Optional route parameters are not yet supported ("${urlPaths[0]}").`);}handleSlug(this.slugName,segmentName);
|
17 | this.slugName=segmentName;
|
18 | nextSegment='[]';}}
|
19 | if(!this.children.has(nextSegment)){this.children.set(nextSegment,new UrlNode());}this.children.get(nextSegment)._insert(urlPaths.slice(1),slugNames,isCatchAll);}}function getSortedRoutes(normalizedPages){
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | const root=new UrlNode();
|
30 | normalizedPages.forEach(pagePath=>root.insert(pagePath));
|
31 | return root.smoosh();}
|
32 |
|
\ | No newline at end of file |