UNPKG

yrv

Version:
2 lines (1 loc) 10.6 kB
var rr=Object.create;var E=Object.defineProperty;var er=Object.getOwnPropertyDescriptor;var tr=Object.getOwnPropertyNames;var nr=Object.getPrototypeOf,ar=Object.prototype.hasOwnProperty;var F=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports);var cr=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of tr(e))!ar.call(r,a)&&a!==t&&E(r,a,{get:()=>e[a],enumerable:!(n=er(e,a))||n.enumerable});return r};var ir=(r,e,t)=>(t=r!=null?rr(nr(r)):{},cr(e||!r||!r.__esModule?E(t,"default",{value:r,enumerable:!0}):t,r));var k=F((vr,A)=>{"use strict";A.exports=r=>encodeURIComponent(r).replace(/[!'()*]/g,e=>`%${e.charCodeAt(0).toString(16).toUpperCase()}`)});var U=F((br,R)=>{"use strict";var $="%[a-f0-9]{2}",C=new RegExp($,"gi"),N=new RegExp("("+$+")+","gi");function x(r,e){try{return decodeURIComponent(r.join(""))}catch{}if(r.length===1)return r;e=e||1;var t=r.slice(0,e),n=r.slice(e);return Array.prototype.concat.call([],x(t),x(n))}function sr(r){try{return decodeURIComponent(r)}catch{for(var e=r.match(C),t=1;t<e.length;t++)r=x(e,t).join(""),e=r.match(C);return r}}function fr(r){for(var e={"%FE%FF":"\uFFFD\uFFFD","%FF%FE":"\uFFFD\uFFFD"},t=N.exec(r);t;){try{e[t[0]]=decodeURIComponent(t[0])}catch{var n=sr(t[0]);n!==t[0]&&(e[t[0]]=n)}t=N.exec(r)}e["%C2"]="\uFFFD";for(var a=Object.keys(e),i=0;i<a.length;i++){var c=a[i];r=r.replace(new RegExp(c,"g"),e[c])}return r}R.exports=function(r){if(typeof r!="string")throw new TypeError("Expected `encodedURI` to be of type `string`, got `"+typeof r+"`");try{return r=r.replace(/\+/g," "),decodeURIComponent(r)}catch{return fr(r)}}});var T=F((jr,q)=>{"use strict";q.exports=(r,e)=>{if(!(typeof r=="string"&&typeof e=="string"))throw new TypeError("Expected the arguments to be of type `string`");if(e==="")return[r];let t=r.indexOf(e);return t===-1?[r]:[r.slice(0,t),r.slice(t+e.length)]}});var I=F((Sr,D)=>{"use strict";D.exports=function(r,e){for(var t={},n=Object.keys(r),a=Array.isArray(e),i=0;i<n.length;i++){var c=n[i],s=r[c];(a?e.indexOf(c)!==-1:e(c,s,r))&&(t[c]=s)}return t}});var Q=F(o=>{"use strict";var ur=k(),lr=U(),B=T(),or=I(),dr=r=>r==null;function hr(r){switch(r.arrayFormat){case"index":return e=>(t,n)=>{let a=t.length;return n===void 0||r.skipNull&&n===null||r.skipEmptyString&&n===""?t:n===null?[...t,[l(e,r),"[",a,"]"].join("")]:[...t,[l(e,r),"[",l(a,r),"]=",l(n,r)].join("")]};case"bracket":return e=>(t,n)=>n===void 0||r.skipNull&&n===null||r.skipEmptyString&&n===""?t:n===null?[...t,[l(e,r),"[]"].join("")]:[...t,[l(e,r),"[]=",l(n,r)].join("")];case"comma":case"separator":return e=>(t,n)=>n==null||n.length===0?t:t.length===0?[[l(e,r),"=",l(n,r)].join("")]:[[t,l(n,r)].join(r.arrayFormatSeparator)];default:return e=>(t,n)=>n===void 0||r.skipNull&&n===null||r.skipEmptyString&&n===""?t:n===null?[...t,l(e,r)]:[...t,[l(e,r),"=",l(n,r)].join("")]}}function gr(r){let e;switch(r.arrayFormat){case"index":return(t,n,a)=>{if(e=/\[(\d*)\]$/.exec(t),t=t.replace(/\[\d*\]$/,""),!e){a[t]=n;return}a[t]===void 0&&(a[t]={}),a[t][e[1]]=n};case"bracket":return(t,n,a)=>{if(e=/(\[\])$/.exec(t),t=t.replace(/\[\]$/,""),!e){a[t]=n;return}if(a[t]===void 0){a[t]=[n];return}a[t]=[].concat(a[t],n)};case"comma":case"separator":return(t,n,a)=>{let i=typeof n=="string"&&n.includes(r.arrayFormatSeparator),c=typeof n=="string"&&!i&&g(n,r).includes(r.arrayFormatSeparator);n=c?g(n,r):n;let s=i||c?n.split(r.arrayFormatSeparator).map(f=>g(f,r)):n===null?n:g(n,r);a[t]=s};default:return(t,n,a)=>{if(a[t]===void 0){a[t]=n;return}a[t]=[].concat(a[t],n)}}}function L(r){if(typeof r!="string"||r.length!==1)throw new TypeError("arrayFormatSeparator must be single character string")}function l(r,e){return e.encode?e.strict?ur(r):encodeURIComponent(r):r}function g(r,e){return e.decode?lr(r):r}function H(r){return Array.isArray(r)?r.sort():typeof r=="object"?H(Object.keys(r)).sort((e,t)=>Number(e)-Number(t)).map(e=>r[e]):r}function J(r){let e=r.indexOf("#");return e!==-1&&(r=r.slice(0,e)),r}function yr(r){let e="",t=r.indexOf("#");return t!==-1&&(e=r.slice(t)),e}function P(r){r=J(r);let e=r.indexOf("?");return e===-1?"":r.slice(e+1)}function M(r,e){return e.parseNumbers&&!Number.isNaN(Number(r))&&typeof r=="string"&&r.trim()!==""?r=Number(r):e.parseBooleans&&r!==null&&(r.toLowerCase()==="true"||r.toLowerCase()==="false")&&(r=r.toLowerCase()==="true"),r}function V(r,e){e=Object.assign({decode:!0,sort:!0,arrayFormat:"none",arrayFormatSeparator:",",parseNumbers:!1,parseBooleans:!1},e),L(e.arrayFormatSeparator);let t=gr(e),n=Object.create(null);if(typeof r!="string"||(r=r.trim().replace(/^[?#&]/,""),!r))return n;for(let a of r.split("&")){if(a==="")continue;let[i,c]=B(e.decode?a.replace(/\+/g," "):a,"=");c=c===void 0?null:["comma","separator"].includes(e.arrayFormat)?c:g(c,e),t(g(i,e),c,n)}for(let a of Object.keys(n)){let i=n[a];if(typeof i=="object"&&i!==null)for(let c of Object.keys(i))i[c]=M(i[c],e);else n[a]=M(i,e)}return e.sort===!1?n:(e.sort===!0?Object.keys(n).sort():Object.keys(n).sort(e.sort)).reduce((a,i)=>{let c=n[i];return Boolean(c)&&typeof c=="object"&&!Array.isArray(c)?a[i]=H(c):a[i]=c,a},Object.create(null))}o.extract=P;o.parse=V;o.stringify=(r,e)=>{if(!r)return"";e=Object.assign({encode:!0,strict:!0,arrayFormat:"none",arrayFormatSeparator:","},e),L(e.arrayFormatSeparator);let t=c=>e.skipNull&&dr(r[c])||e.skipEmptyString&&r[c]==="",n=hr(e),a={};for(let c of Object.keys(r))t(c)||(a[c]=r[c]);let i=Object.keys(a);return e.sort!==!1&&i.sort(e.sort),i.map(c=>{let s=r[c];return s===void 0?"":s===null?l(c,e):Array.isArray(s)?s.reduce(n(c),[]).join("&"):l(c,e)+"="+l(s,e)}).filter(c=>c.length>0).join("&")};o.parseUrl=(r,e)=>{e=Object.assign({decode:!0},e);let[t,n]=B(r,"#");return Object.assign({url:t.split("?")[0]||"",query:V(P(r),e)},e&&e.parseFragmentIdentifier&&n?{fragmentIdentifier:g(n,e)}:{})};o.stringifyUrl=(r,e)=>{e=Object.assign({encode:!0,strict:!0},e);let t=J(r.url).split("?")[0]||"",n=o.extract(r.url),a=o.parse(n,{sort:!1}),i=Object.assign(a,r.query),c=o.stringify(i,e);c&&(c=`?${c}`);let s=yr(r.url);return r.fragmentIdentifier&&(s=`#${l(r.fragmentIdentifier,e)}`),`${t}${c}${s}`};o.pick=(r,e,t)=>{t=Object.assign({parseFragmentIdentifier:!0},t);let{url:n,query:a,fragmentIdentifier:i}=o.parseUrl(r,t);return o.stringifyUrl({url:n,query:or(a,e),fragmentIdentifier:i},t)};o.exclude=(r,e,t)=>{let n=Array.isArray(e)?a=>!e.includes(a):(a,i)=>!e(a,i);return o.pick(r,n,t)}});var X=ir(Q());var w=function(r){function e(t,n){var a="Unreachable '"+(t!=="/"?t.replace(/\/$/,""):t)+"', segment '"+n+"' is not defined";r.call(this,a),this.message=a,this.route=t,this.path=n}return r&&(e.__proto__=r),e.prototype=Object.create(r&&r.prototype),e.prototype.constructor=e,e}(Error);function G(r,e){var t,n,a=-100,i=[];t=r.replace(/[-$.]/g,"\\$&").replace(/\(/g,"(?:").replace(/\)/g,")?").replace(/([:*]\w+)(?:<([^<>]+?)>)?/g,function(f,d,u){return i.push(d.substr(1)),d.charAt()===":"?(a+=100,"((?!#)"+(u||"[^#/]+?")+")"):(n=!0,a+=500,"((?!#)"+(u||"[^#]+?")+")")});try{t=new RegExp("^"+t+"$")}catch{throw new TypeError("Invalid route expression, given '"+e+"'")}var c=r.includes("#")?.5:1,s=r.length*a*c;return{keys:i,regex:t,_depth:s,_isSplat:n}}var m=function(e,t){var n=G(e,t),a=n.keys,i=n.regex,c=n._depth,s=n._isSplat;function f(d){var u=d.match(i);if(u)return a.reduce(function(y,O,j){return y[O]=typeof u[j+1]=="string"?decodeURIComponent(u[j+1]):null,y},{})}return f.regex=i,f.keys=a,{_isSplat:s,_depth:c,match:f}};m.push=function(e,t,n,a){var i=t[e]||(t[e]={});return i.pattern||(i.pattern=new m(e,a),i.route=(n||"").replace(/\/$/,"")||"/"),t.keys=t.keys||[],t.keys.includes(e)||(t.keys.push(e),m.sort(t)),i};m.sort=function(e){e.keys.sort(function(t,n){return e[t].pattern._depth-e[n].pattern._depth})};function K(r,e){return""+(e&&e!=="/"?e:"")+(r||"")}function b(r,e){var t=r.match(/<[^<>]*\/[^<>]*>/);if(t)throw new TypeError("RegExp cannot contain slashes, given '"+t+"'");var n=r.split(/(?=\/|#)/),a=[];n[0]!=="/"&&n.unshift("/"),n.some(function(i,c){var s=a.slice(1).concat(i).join("")||null,f=n.slice(c+1).join("")||null,d=e(i,s,f?""+(i!=="/"?i:"")+f:null);return a.push(i),d})}function mr(r,e){var t=e.refs,n={},a=[],i;return b(r,function(c,s,f){if(!e.keys)throw new w(r,c);var d;if(e.keys.some(function(u){var y=e[u].pattern,O=y.match,j=y._length,p=y._isSplat,_=O(p&&f||c);if(_){var Y=(t[e[u].route]||[]).concat(t[e[u].route+"/"]||[]).concat(t[e[u].route+"#"]||[]);return Object.assign(n,_),Y.forEach(function(v){if(!a.some(function(Z){return Z.key===v})){var h=Object.assign({},t[v]),S=!1;h.exact?S=f===null:S=!(c&&s===null)||c===s||p||!f,h.matches=S,h.params=Object.assign({},n),h.route=h.fullpath,h.depth+=O.keys.length,h.path=p&&f||s||c,delete h.fullpath,a.push(h)}}),f===null&&!e[u].keys?!0:!p&&!f&&e.keys.some(function(v){return v.includes("*")})?!1:(i=p,e=e[u],d=!0,!0)}return!1}),!(d||e.keys.some(function(u){return e[u].pattern.match(c)})))throw new w(r,c);return i||!d}),a.sort(function(c,s){return s.fallback&&!c.fallback?-1:c.fallback&&!s.fallback?1:s.route.includes("#")&&!c.route.includes("#")?-1:c.route.includes("#")&&!s.route.includes("#")?1:c.depth-s.depth})}function z(r,e,t){for(var n=mr.bind(null,r,e),a=[];t>0;){t-=1;try{return n(a)}catch(i){if(t>0)return n(a);throw i}}}function pr(r,e,t,n){var a=K(r,t),i=a.split(/(?=[#:/*.]\w)/g).length,c=Object.assign({},n,{fullpath:a,depth:i});if(!r||!"#/".includes(r.charAt()))throw new TypeError("Routes should have a valid path, given "+JSON.stringify(r));if(!c.key)throw new TypeError("Routes should have a key, given "+JSON.stringify(c));e.refs[c.key]=c,e.refs[a]=e.refs[a]?e.refs[a].concat(c.key):[c.key];var s=e;return b(a,function(f,d){s=m.push(f,s,d,a)}),a}function Fr(r,e,t){var n=K(r,t),a=e,i=null,c=null;if(b(n,function(f){if(!a)return i=null,!0;if(!a.keys)throw new w(r,f);c=f,i=a,a=a[c]}),!(i&&c))throw new w(r,c);if(i===e&&(i=e["/"]),i.route!==c){var s=i.keys.indexOf(c);if(s===-1)throw new w(r,c);i.keys.splice(s,1),m.sort(i),delete i[c]}i.route===a.route&&delete e.refs[n]}var W=function(){var e={refs:{}},t=[];return{routes:e,resolve:function(n,a){var i=n.split("?")[0],c=[];b(i,function(s,f,d){try{a(null,z(f,e,2).filter(function(u){return c.includes(u.route)?!1:(c.push(u.route),!0)}),f)}catch(u){a(u,[])}})},mount:function(n,a){n!=="/"&&t.push(n),a(),t.pop()},find:function(n,a){return z(n,e,a===!0?2:a||1)},add:function(n,a){return pr(n,e,t.join(""),a)},rm:function(n){return Fr(n,e,t.join(""))}}};W.matches=function(e,t){return G(e,t).regex.test(t)};var wr=W;var export_parse=X.parse;var export_stringify=X.stringify;export{wr as Router,export_parse as parse,export_stringify as stringify};