router-trie
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -1,2 +0,2 @@ | ||
const e=["children"],t=Symbol("index"),l=Symbol("dynamic"),r=Symbol("optional"),n=Symbol("catch-all"),i=Symbol("route"),o=Symbol("root");function c(e,t,l={}){let r=f(e,((t?t.replace(/^\//,"").replace(/\/$/,""):null)||"").split("/").filter(Boolean),0,[],l.onVisit);return r.length?function(e){let t,l=Number.MIN_SAFE_INTEGER;for(let r of e){let e=0;for(let t of r)e+=h(t);e>l&&(l=e,t=r)}return t}(r):null}function f(e,c,u,h,s){if(!e)return h;s&&s(e);let a=c.length;if(u>=a)switch(e.key){case t:f(e.children[0],c,u,h,s);break;case l:case n:break;case i:e.route&&h.push(function(e){if(!e.route)return null;let t=[],l=e;for(;l;)l.route&&t.push(l.route),l=l.parent;return t.reverse()}(e));case o:case r:for(let t of e.children)f(t,c,u,h,s)}else if("string"==typeof e.key){if(e.key===c[u])for(let t of e.children)f(t,c,u+1,h,s)}else switch(e.key){case t:break;case n:f(e.children[0],c,a,h,s);break;case l:case r:u++;case o:case i:for(let t of e.children)f(t,c,u,h,s)}return h}let u=e=>"*"===e;function h(e){let t=(e.path||"").split("/").filter(Boolean),l=t.length*t.length;return t.some(u)&&(l+=-1),e.index&&(l+=2),t.filter(e=>!u(e)).reduce((e,t,l)=>e+(t.startsWith(":")?t.endsWith("?")?3*(l+1):4*(l+1):""===t?1*(l+1):10*(l+1)),l)}function s(e){let t={key:o,parent:null,children:[],route:null};for(let l of e)a(t,l);return t}function a(e,o){let c=function(e,o,c){let f=o.split("/"),u=f.length,h=e;for(let e=0;e<u;e++){let t=f[e];if(!t)continue;if(t.startsWith("*")){if(h.children.find(e=>e.key===n))throw new Error("Only one catch all route is allowed per branch of the tree");let e=d(n,h);h.children.push(e),h=e;break}if(t.startsWith(":")){if(t.endsWith("?")){let e=h.children.find(e=>e.key===r);if(e)h=e;else{let e=d(r,h);h.children.push(e),h=e}}else{let e=h.children.find(e=>e.key===l);if(e)h=e;else{let e=d(l,h);h.children.push(e),h=e}}continue}let i=h.children.find(e=>e.key===t);if(i)h=i;else{let e=d(t,h);h.children.push(e),h=e}}if(c.index){let e=d(t,h);h.children.push(e),h=e}let s=d(i,h,c);return h.children.push(s),h=s,h}(e,o.path?o.path.replace(/^\//,"").replace(/\/$/,""):"",o);if(!o.index&&o.children)for(let e of o.children)a(c,e);return c}function d(t,l,r=null){if(r){let t=function(e,t){if(null==e)return{};var l,r,n={},i=Object.keys(e);for(r=0;r<i.length;r++)t.indexOf(l=i[r])>=0||(n[l]=e[l]);return n}(r,e);r=t}return{key:t,route:r,parent:l,children:[]}}export{n as CATCH_ALL_SYMBOL,l as DYNAMIC_SYMBOL,t as INDEX_SYMBOL,r as OPTIONAL_SYMBOL,o as ROOT_SYMBOL,i as ROUTE_SYMBOL,s as createTrie,c as matchTrie}; | ||
const e=["children"],t=Symbol("index"),n=Symbol("dynamic"),r=Symbol("optional"),l=Symbol("catch-all"),i=Symbol("route"),o=Symbol("root");function c(e,t,n={}){let r=f(e,p(d(t)),0,[],n.onVisit);return r.length?function(e){let t,n=Number.MIN_SAFE_INTEGER;for(let r of e){let e=0;for(let t of r)e+=h(t);e>n&&(n=e,t=r)}return t}(r):null}function f(e,c,u,h,s){if(!e)return h;s&&s(e);let a=c.length;if(u>=a)switch(e.key){case t:f(e.children[0],c,u,h,s);break;case n:case l:break;case i:e.route&&h.push(function(e){if(!e.route)return null;let t=[],n=e;for(;n;)n.route&&t.push(n.route),n=n.parent;return t.reverse()}(e));case o:case r:for(let t of e.children)f(t,c,u,h,s)}else if("string"==typeof e.key){if(e.key===c[u])for(let t of e.children)f(t,c,u+1,h,s)}else switch(e.key){case t:break;case l:f(e.children[0],c,a,h,s);break;case n:case r:u++;case o:case i:for(let t of e.children)f(t,c,u,h,s)}return h}const u=e=>"*"===e;function h(e){let t=p(e.path||""),n=t.length*t.length;return t.some(u)&&(n+=-1),e.index&&(n+=2),t.filter(e=>!u(e)).reduce((e,t,n)=>e+(t.startsWith(":")?t.endsWith("?")?3*(n+1):4*(n+1):""===t?1*(n+1):10*(n+1)),n)}function s(e){let t={key:o,parent:null,children:[],route:null};for(let n of e)a(t,n);return t}function a(e,o){let c=function(e,o,c){let f=p(o),u=f.length,h=e;for(let e=0;e<u;e++){let t=f[e];if(!t)continue;if(t.startsWith("*")){if(h.children.find(e=>e.key===l))throw new Error("Only one catch all route is allowed per branch of the tree");let e=y(l,h);h.children.push(e),h=e;break}if(t.startsWith(":")){if(t.endsWith("?")){let e=h.children.find(e=>e.key===r);if(e)h=e;else{let e=y(r,h);h.children.push(e),h=e}}else{let e=h.children.find(e=>e.key===n);if(e)h=e;else{let e=y(n,h);h.children.push(e),h=e}}continue}let i=h.children.find(e=>e.key===t);if(i)h=i;else{let e=y(t,h);h.children.push(e),h=e}}if(c.index){let e=y(t,h);h.children.push(e),h=e}let s=y(i,h,c);return h.children.push(s),h=s,h}(e,d(o.path),o);if(!o.index&&o.children)for(let e of o.children)a(c,e);return c}function d(e){return e?e.replace(/^\//,"").replace(/\/$/,""):""}function p(e){return e.split("/").filter(Boolean)}function y(t,n,r=null){if(r){let t=function(e,t){if(null==e)return{};var n,r,l={},i=Object.keys(e);for(r=0;r<i.length;r++)t.indexOf(n=i[r])>=0||(l[n]=e[n]);return l}(r,e);r=t}return{key:t,route:r,parent:n,children:[]}}export{l as CATCH_ALL_SYMBOL,n as DYNAMIC_SYMBOL,t as INDEX_SYMBOL,r as OPTIONAL_SYMBOL,o as ROOT_SYMBOL,i as ROUTE_SYMBOL,s as createTrie,c as matchTrie}; | ||
//# sourceMappingURL=index.modern.js.map |
@@ -1,2 +0,2 @@ | ||
function e(e,r){(null==r||r>e.length)&&(r=e.length);for(var n=0,t=new Array(r);n<r;n++)t[n]=e[n];return t}function r(r,n){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,n){if(r){if("string"==typeof r)return e(r,n);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?e(r,n):void 0}}(r))||n&&r&&"number"==typeof r.length){t&&(r=t);var i=0;return function(){return i>=r.length?{done:!0}:{done:!1,value:r[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var n=["children"],t=Symbol("index"),i=Symbol("dynamic"),o=Symbol("optional"),a=Symbol("catch-all"),l=Symbol("route"),u=Symbol("root");function c(e,n,t){void 0===t&&(t={});var i=f(e,((n?n.replace(/^\//,"").replace(/\/$/,""):null)||"").split("/").filter(Boolean),0,[],t.onVisit);return i.length?function(e){for(var n,t,i=Number.MIN_SAFE_INTEGER,o=r(e);!(t=o()).done;){for(var a,l=t.value,u=0,c=r(l);!(a=c()).done;)u+=(d=void 0,s=void 0,s=(d=((f=a.value).path||"").split("/").filter(Boolean)).length*d.length,d.some(h)&&(s+=-1),f.index&&(s+=2),d.filter(function(e){return!h(e)}).reduce(function(e,r,n){return e+(r.startsWith(":")?r.endsWith("?")?3*(n+1):4*(n+1):""===r?1*(n+1):10*(n+1))},s));u>i&&(i=u,n=l)}var f,d,s;return n}(i):null}function f(e,n,c,h,d){if(!e)return h;d&&d(e);var s=n.length;if(c>=s)switch(e.key){case t:f(e.children[0],n,c,h,d);break;case i:case a:break;case l:e.route&&h.push(function(e){if(!e.route)return null;for(var r=[],n=e;n;)n.route&&r.push(n.route),n=n.parent;return r.reverse()}(e));case u:case o:for(var v,p=r(e.children);!(v=p()).done;)f(v.value,n,c,h,d)}else if("string"==typeof e.key){if(e.key===n[c])for(var y,b=r(e.children);!(y=b()).done;)f(y.value,n,c+1,h,d)}else switch(e.key){case t:break;case a:f(e.children[0],n,s,h,d);break;case i:case o:c++;case u:case l:for(var m,k=r(e.children);!(m=k()).done;)f(m.value,n,c,h,d)}return h}var h=function(e){return"*"===e};function d(e){for(var n,t={key:u,parent:null,children:[],route:null},i=r(e);!(n=i()).done;)s(t,n.value);return t}function s(e,n){var u=function(e,r,n){for(var u=r.split("/"),c=u.length,f=e,h=function(e){var r=u[e];if(!r)return"continue";if(r.startsWith("*")){if(f.children.find(function(e){return e.key===a}))throw new Error("Only one catch all route is allowed per branch of the tree");var n=v(a,f);return f.children.push(n),f=n,"break"}if(r.startsWith(":")){if(r.endsWith("?")){var t=f.children.find(function(e){return e.key===o});if(t)f=t;else{var l=v(o,f);f.children.push(l),f=l}}else{var c=f.children.find(function(e){return e.key===i});if(c)f=c;else{var h=v(i,f);f.children.push(h),f=h}}return"continue"}var d=f.children.find(function(e){return e.key===r});if(d)f=d;else{var s=v(r,f);f.children.push(s),f=s}},d=0;d<c;d++){var s=h(d);if("continue"!==s&&"break"===s)break}if(n.index){var p=v(t,f);f.children.push(p),f=p}var y=v(l,f,n);return f.children.push(y),f=y}(e,n.path?n.path.replace(/^\//,"").replace(/\/$/,""):"",n);if(!n.index&&n.children)for(var c,f=r(n.children);!(c=f()).done;)s(u,c.value);return u}function v(e,r,t){if(void 0===t&&(t=null),t){var i=function(e,r){if(null==e)return{};var n,t,i={},o=Object.keys(e);for(t=0;t<o.length;t++)r.indexOf(n=o[t])>=0||(i[n]=e[n]);return i}(t,n);t=i}return{key:e,route:t,parent:r,children:[]}}export{a as CATCH_ALL_SYMBOL,i as DYNAMIC_SYMBOL,t as INDEX_SYMBOL,o as OPTIONAL_SYMBOL,u as ROOT_SYMBOL,l as ROUTE_SYMBOL,d as createTrie,c as matchTrie}; | ||
function e(e,r){(null==r||r>e.length)&&(r=e.length);for(var n=0,t=new Array(r);n<r;n++)t[n]=e[n];return t}function r(r,n){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,n){if(r){if("string"==typeof r)return e(r,n);var t=Object.prototype.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?e(r,n):void 0}}(r))||n&&r&&"number"==typeof r.length){t&&(r=t);var i=0;return function(){return i>=r.length?{done:!0}:{done:!1,value:r[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var n=["children"],t=Symbol("index"),i=Symbol("dynamic"),o=Symbol("optional"),a=Symbol("catch-all"),u=Symbol("route"),l=Symbol("root");function c(e,n,t){void 0===t&&(t={});var i=f(e,y(v(n)),0,[],t.onVisit);return i.length?function(e){for(var n,t,i=Number.MIN_SAFE_INTEGER,o=r(e);!(t=o()).done;){for(var a,u=t.value,l=0,c=r(u);!(a=c()).done;)l+=(h=void 0,s=void 0,s=(h=y((f=a.value).path||"")).length*h.length,h.some(d)&&(s+=-1),f.index&&(s+=2),h.filter(function(e){return!d(e)}).reduce(function(e,r,n){return e+(r.startsWith(":")?r.endsWith("?")?3*(n+1):4*(n+1):""===r?1*(n+1):10*(n+1))},s));l>i&&(i=l,n=u)}var f,h,s;return n}(i):null}function f(e,n,c,d,h){if(!e)return d;h&&h(e);var s=n.length;if(c>=s)switch(e.key){case t:f(e.children[0],n,c,d,h);break;case i:case a:break;case u:e.route&&d.push(function(e){if(!e.route)return null;for(var r=[],n=e;n;)n.route&&r.push(n.route),n=n.parent;return r.reverse()}(e));case l:case o:for(var v,y=r(e.children);!(v=y()).done;)f(v.value,n,c,d,h)}else if("string"==typeof e.key){if(e.key===n[c])for(var p,b=r(e.children);!(p=b()).done;)f(p.value,n,c+1,d,h)}else switch(e.key){case t:break;case a:f(e.children[0],n,s,d,h);break;case i:case o:c++;case l:case u:for(var m,k=r(e.children);!(m=k()).done;)f(m.value,n,c,d,h)}return d}var d=function(e){return"*"===e};function h(e){for(var n,t={key:l,parent:null,children:[],route:null},i=r(e);!(n=i()).done;)s(t,n.value);return t}function s(e,n){var l=function(e,r,n){for(var l=y(r),c=l.length,f=e,d=function(e){var r=l[e];if(!r)return"continue";if(r.startsWith("*")){if(f.children.find(function(e){return e.key===a}))throw new Error("Only one catch all route is allowed per branch of the tree");var n=p(a,f);return f.children.push(n),f=n,"break"}if(r.startsWith(":")){if(r.endsWith("?")){var t=f.children.find(function(e){return e.key===o});if(t)f=t;else{var u=p(o,f);f.children.push(u),f=u}}else{var c=f.children.find(function(e){return e.key===i});if(c)f=c;else{var d=p(i,f);f.children.push(d),f=d}}return"continue"}var h=f.children.find(function(e){return e.key===r});if(h)f=h;else{var s=p(r,f);f.children.push(s),f=s}},h=0;h<c;h++){var s=d(h);if("continue"!==s&&"break"===s)break}if(n.index){var v=p(t,f);f.children.push(v),f=v}var b=p(u,f,n);return f.children.push(b),f=b}(e,v(n.path),n);if(!n.index&&n.children)for(var c,f=r(n.children);!(c=f()).done;)s(l,c.value);return l}function v(e){return e?e.replace(/^\//,"").replace(/\/$/,""):""}function y(e){return e.split("/").filter(Boolean)}function p(e,r,t){if(void 0===t&&(t=null),t){var i=function(e,r){if(null==e)return{};var n,t,i={},o=Object.keys(e);for(t=0;t<o.length;t++)r.indexOf(n=o[t])>=0||(i[n]=e[n]);return i}(t,n);t=i}return{key:e,route:t,parent:r,children:[]}}export{a as CATCH_ALL_SYMBOL,i as DYNAMIC_SYMBOL,t as INDEX_SYMBOL,o as OPTIONAL_SYMBOL,l as ROOT_SYMBOL,u as ROUTE_SYMBOL,h as createTrie,c as matchTrie}; | ||
//# sourceMappingURL=index.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e||self).routerTrie={})}(this,function(e){function r(e,r){(null==r||r>e.length)&&(r=e.length);for(var n=0,t=new Array(r);n<r;n++)t[n]=e[n];return t}function n(e,n){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,n){if(e){if("string"==typeof e)return r(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?r(e,n):void 0}}(e))||n&&e&&"number"==typeof e.length){t&&(e=t);var i=0;return function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var t=["children"],i=Symbol("index"),o=Symbol("dynamic"),a=Symbol("optional"),l=Symbol("catch-all"),u=Symbol("route"),c=Symbol("root");function f(e,r,t,d,s){if(!e)return d;s&&s(e);var h=r.length;if(t>=h)switch(e.key){case i:f(e.children[0],r,t,d,s);break;case o:case l:break;case u:e.route&&d.push(function(e){if(!e.route)return null;for(var r=[],n=e;n;)n.route&&r.push(n.route),n=n.parent;return r.reverse()}(e));case c:case a:for(var v,p=n(e.children);!(v=p()).done;)f(v.value,r,t,d,s)}else if("string"==typeof e.key){if(e.key===r[t])for(var y,b=n(e.children);!(y=b()).done;)f(y.value,r,t+1,d,s)}else switch(e.key){case i:break;case l:f(e.children[0],r,h,d,s);break;case o:case a:t++;case c:case u:for(var m,k=n(e.children);!(m=k()).done;)f(m.value,r,t,d,s)}return d}var d=function(e){return"*"===e};function s(e,r){var t=function(e,r,n){for(var t=r.split("/"),c=t.length,f=e,d=function(e){var r=t[e];if(!r)return"continue";if(r.startsWith("*")){if(f.children.find(function(e){return e.key===l}))throw new Error("Only one catch all route is allowed per branch of the tree");var n=h(l,f);return f.children.push(n),f=n,"break"}if(r.startsWith(":")){if(r.endsWith("?")){var i=f.children.find(function(e){return e.key===a});if(i)f=i;else{var u=h(a,f);f.children.push(u),f=u}}else{var c=f.children.find(function(e){return e.key===o});if(c)f=c;else{var d=h(o,f);f.children.push(d),f=d}}return"continue"}var s=f.children.find(function(e){return e.key===r});if(s)f=s;else{var v=h(r,f);f.children.push(v),f=v}},s=0;s<c;s++){var v=d(s);if("continue"!==v&&"break"===v)break}if(n.index){var p=h(i,f);f.children.push(p),f=p}var y=h(u,f,n);return f.children.push(y),f=y}(e,r.path?r.path.replace(/^\//,"").replace(/\/$/,""):"",r);if(!r.index&&r.children)for(var c,f=n(r.children);!(c=f()).done;)s(t,c.value);return t}function h(e,r,n){if(void 0===n&&(n=null),n){var i=function(e,r){if(null==e)return{};var n,t,i={},o=Object.keys(e);for(t=0;t<o.length;t++)r.indexOf(n=o[t])>=0||(i[n]=e[n]);return i}(n,t);n=i}return{key:e,route:n,parent:r,children:[]}}e.CATCH_ALL_SYMBOL=l,e.DYNAMIC_SYMBOL=o,e.INDEX_SYMBOL=i,e.OPTIONAL_SYMBOL=a,e.ROOT_SYMBOL=c,e.ROUTE_SYMBOL=u,e.createTrie=function(e){for(var r,t={key:c,parent:null,children:[],route:null},i=n(e);!(r=i()).done;)s(t,r.value);return t},e.matchTrie=function(e,r,t){void 0===t&&(t={});var i=f(e,((r?r.replace(/^\//,"").replace(/\/$/,""):null)||"").split("/").filter(Boolean),0,[],t.onVisit);return i.length?function(e){for(var r,t,i=Number.MIN_SAFE_INTEGER,o=n(e);!(t=o()).done;){for(var a,l=t.value,u=0,c=n(l);!(a=c()).done;)u+=(s=void 0,h=void 0,h=(s=((f=a.value).path||"").split("/").filter(Boolean)).length*s.length,s.some(d)&&(h+=-1),f.index&&(h+=2),s.filter(function(e){return!d(e)}).reduce(function(e,r,n){return e+(r.startsWith(":")?r.endsWith("?")?3*(n+1):4*(n+1):""===r?1*(n+1):10*(n+1))},h));u>i&&(i=u,r=l)}var f,s,h;return r}(i):null}}); | ||
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports):"function"==typeof define&&define.amd?define(["exports"],r):r((e||self).routerTrie={})}(this,function(e){function r(e,r){(null==r||r>e.length)&&(r=e.length);for(var n=0,t=new Array(r);n<r;n++)t[n]=e[n];return t}function n(e,n){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,n){if(e){if("string"==typeof e)return r(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?r(e,n):void 0}}(e))||n&&e&&"number"==typeof e.length){t&&(e=t);var i=0;return function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var t=["children"],i=Symbol("index"),o=Symbol("dynamic"),a=Symbol("optional"),u=Symbol("catch-all"),l=Symbol("route"),c=Symbol("root");function f(e,r,t,d,s){if(!e)return d;s&&s(e);var h=r.length;if(t>=h)switch(e.key){case i:f(e.children[0],r,t,d,s);break;case o:case u:break;case l:e.route&&d.push(function(e){if(!e.route)return null;for(var r=[],n=e;n;)n.route&&r.push(n.route),n=n.parent;return r.reverse()}(e));case c:case a:for(var v,y=n(e.children);!(v=y()).done;)f(v.value,r,t,d,s)}else if("string"==typeof e.key){if(e.key===r[t])for(var p,b=n(e.children);!(p=b()).done;)f(p.value,r,t+1,d,s)}else switch(e.key){case i:break;case u:f(e.children[0],r,h,d,s);break;case o:case a:t++;case c:case l:for(var m,k=n(e.children);!(m=k()).done;)f(m.value,r,t,d,s)}return d}var d=function(e){return"*"===e};function s(e,r){var t=function(e,r,n){for(var t=v(r),c=t.length,f=e,d=function(e){var r=t[e];if(!r)return"continue";if(r.startsWith("*")){if(f.children.find(function(e){return e.key===u}))throw new Error("Only one catch all route is allowed per branch of the tree");var n=y(u,f);return f.children.push(n),f=n,"break"}if(r.startsWith(":")){if(r.endsWith("?")){var i=f.children.find(function(e){return e.key===a});if(i)f=i;else{var l=y(a,f);f.children.push(l),f=l}}else{var c=f.children.find(function(e){return e.key===o});if(c)f=c;else{var d=y(o,f);f.children.push(d),f=d}}return"continue"}var s=f.children.find(function(e){return e.key===r});if(s)f=s;else{var h=y(r,f);f.children.push(h),f=h}},s=0;s<c;s++){var h=d(s);if("continue"!==h&&"break"===h)break}if(n.index){var p=y(i,f);f.children.push(p),f=p}var b=y(l,f,n);return f.children.push(b),f=b}(e,h(r.path),r);if(!r.index&&r.children)for(var c,f=n(r.children);!(c=f()).done;)s(t,c.value);return t}function h(e){return e?e.replace(/^\//,"").replace(/\/$/,""):""}function v(e){return e.split("/").filter(Boolean)}function y(e,r,n){if(void 0===n&&(n=null),n){var i=function(e,r){if(null==e)return{};var n,t,i={},o=Object.keys(e);for(t=0;t<o.length;t++)r.indexOf(n=o[t])>=0||(i[n]=e[n]);return i}(n,t);n=i}return{key:e,route:n,parent:r,children:[]}}e.CATCH_ALL_SYMBOL=u,e.DYNAMIC_SYMBOL=o,e.INDEX_SYMBOL=i,e.OPTIONAL_SYMBOL=a,e.ROOT_SYMBOL=c,e.ROUTE_SYMBOL=l,e.createTrie=function(e){for(var r,t={key:c,parent:null,children:[],route:null},i=n(e);!(r=i()).done;)s(t,r.value);return t},e.matchTrie=function(e,r,t){void 0===t&&(t={});var i=f(e,v(h(r)),0,[],t.onVisit);return i.length?function(e){for(var r,t,i=Number.MIN_SAFE_INTEGER,o=n(e);!(t=o()).done;){for(var a,u=t.value,l=0,c=n(u);!(a=c()).done;)l+=(s=void 0,h=void 0,h=(s=v((f=a.value).path||"")).length*s.length,s.some(d)&&(h+=-1),f.index&&(h+=2),s.filter(function(e){return!d(e)}).reduce(function(e,r,n){return e+(r.startsWith(":")?r.endsWith("?")?3*(n+1):4*(n+1):""===r?1*(n+1):10*(n+1))},h));l>i&&(i=l,r=u)}var f,s,h;return r}(i):null}}); | ||
//# sourceMappingURL=index.umd.js.map |
{ | ||
"name": "router-trie", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "source": "src/index.ts", |
@@ -17,9 +17,11 @@ export const INDEX_SYMBOL = Symbol("index"), | ||
) { | ||
let match = !pathname ? null : pathname.replace(/^\//, "").replace(/\/$/, ""), | ||
path = match || "", | ||
segments = path.split("/").filter(Boolean), | ||
matched = matchRecursive<Route>(root, segments, 0, [], options.onVisit); | ||
if (!matched.length) return null; | ||
let matched = matchRecursive<Route>( | ||
root, | ||
getSegments(sanitizePath(pathname)), | ||
0, | ||
[], | ||
options.onVisit | ||
); | ||
return rankMatched(matched); | ||
return matched.length ? rankMatched(matched) : null; | ||
} | ||
@@ -135,3 +137,3 @@ | ||
let staticSegmentValue = 10, | ||
const staticSegmentValue = 10, | ||
dynamicSegmentValue = 4, | ||
@@ -144,3 +146,3 @@ optionalSegmentValue = 3, | ||
function computeScore(match: Omit<RouteConfig, "children">): number { | ||
let segments = (match.path || "").split("/").filter(Boolean), | ||
let segments = getSegments(match.path || ""), | ||
initialScore = segments.length * segments.length; | ||
@@ -193,3 +195,3 @@ if (segments.some(isSplat)) { | ||
) { | ||
let path = route.path ? route.path.replace(/^\//, "").replace(/\/$/, "") : "", | ||
let path = sanitizePath(route.path), | ||
node = insertPath(root, path, route); | ||
@@ -206,2 +208,10 @@ | ||
function sanitizePath(path?: string) { | ||
return path ? path.replace(/^\//, "").replace(/\/$/, "") : ""; | ||
} | ||
function getSegments(path: string) { | ||
return path.split("/").filter(Boolean); | ||
} | ||
function insertPath<Route extends RouteConfig>( | ||
@@ -212,3 +222,3 @@ root: Node<Route>, | ||
) { | ||
let segments = path.split("/"), | ||
let segments = getSegments(path), | ||
segmentsLength = segments.length, | ||
@@ -215,0 +225,0 @@ currentNode = root; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
81720
428