UNPKG

diesel-core

Version:

Web framework built on Web Standards

2 lines (1 loc) 1.64 kB
var G=Object.create;var{getPrototypeOf:H,defineProperty:C,getOwnPropertyNames:J}=Object;var K=Object.prototype.hasOwnProperty;var L=(f,j,b)=>{b=f!=null?G(H(f)):{};let v=j||!f||!f.__esModule?C(b,"default",{value:f,enumerable:!0}):b;for(let q of J(f))if(!K.call(v,q))C(v,q,{get:()=>f[q],enumerable:!0});return v};var O=((f)=>typeof require!=="undefined"?require:typeof Proxy!=="undefined"?new Proxy(f,{get:(j,b)=>(typeof require!=="undefined"?require:j)[b]}):f)(function(f){if(typeof require!=="undefined")return require.apply(this,arguments);throw Error('Dynamic require of "'+f+'" is not supported')});class A{children;isEndOfWord;handler;isDynamic;pattern;path;method;constructor(){this.children={},this.isEndOfWord=!1,this.handler=[],this.isDynamic=!1,this.pattern="",this.path="",this.method=[]}}class E{root;constructor(){this.root=new A}insert(f,j){let b=this.root,v=f.split("/").filter(Boolean);if(f==="/"){b.isEndOfWord=!0,b.handler.push(j.handler),b.path=f,b.method.push(j.method);return}for(let q of v){let z=!1,w=q;if(q.startsWith(":"))z=!0,w=":";if(!b.children[w])b.children[w]=new A;b=b.children[w],b.isDynamic=z,b.pattern=q}b.isEndOfWord=!0,b.path=f,b.method.push(j.method),b.handler.push(j.handler)}search(f,j){let b=this.root,v=f.split("/").filter(Boolean),q=v.length;for(let F of v){let B=F;if(!b.children[B])if(b.children[":"])b=b.children[":"];else return null;else b=b.children[B]}let z=b.path.split("/").filter(Boolean);if(q!==z.length)return null;let w=b.method.indexOf(j);if(w!==-1)return{path:b.path,handler:b.handler[w],isDynamic:b.isDynamic,pattern:b.pattern,method:b.method[w]};return null}}export{E as default};