@swan-io/chicane
Advanced tools
Comparing version 2.0.0-rc.1 to 2.0.0-rc.2
@@ -1,2 +0,2 @@ | ||
var e=require("react"),t=require("@emotion/hash"),n=require("use-sync-external-store/shim/with-selector.js");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var o=/*#__PURE__*/r(t);const s=e=>e[0],c=e=>e[e.length-1],l=e=>e,a=()=>{},i=e=>""!==e,u=e=>e.startsWith(":"),p=e=>"/"===e[0]?e:"/"+e,h=(e,t)=>e.length===t.length&&e.every((e,n)=>e===t[n]),f=e=>{const t=Object.keys(e);return t.length>0?JSON.stringify(t.sort().map(t=>[t,e[t]])):""},d=(e,t,n)=>{const r=e+f(t),s=f(n);return o.default(r)+"-"+o.default(s)},g=(e,t)=>(null==e?void 0:e.key)===(null==t?void 0:t.key),m=e=>{const t=e.indexOf("{");return t>-1&&e.endsWith("}")?{name:e.substring(0,t),union:e.substring(t+1,e.length-1).split("|").filter(i)}:{name:e}},y=e=>{const t=e.split("&").filter(i),n={};for(const e of t){const[t="",r=""]=e.split("="),o=decodeURIComponent(t),s=decodeURIComponent(r),c=n[o];n[o]=null!=c?"string"==typeof c?[c,s]:c.concat(s):s}return n},b=(e,t,n)=>{const r=e+(""!==e?"&":"")+encodeURIComponent(t);return""!==n?r+"="+encodeURIComponent(n):r},w=e=>{const t=Object.keys(e);if(0===t.length)return"";let n="";t.sort();for(const r of t){const t=e[r];if(null!=t)if("string"==typeof t)n=b(n,r,t);else for(const e of t)n=b(n,r,e)}return""===n?"":"?"+n};let v=!1;const x=e=>{const t=e.indexOf("#"),n=p(t<0?e:e.substring(0,t)),r=n.indexOf("?");return r<0?{path:n,search:""}:{path:n.substring(0,r),search:n.substring(r+1)}},k=e=>{const t=x(e),n=t.path.substring(1),r=""!==n?v?n.split("/").map(decodeURIComponent):n.split("/").filter(i).map(decodeURIComponent):[],o=""!==t.search?y(t.search):{},s="/"+r.map(encodeURIComponent).join("/"),c=w(o),l=s+c;return{path:r,search:o,raw:{path:s,search:c},toString:()=>l}},S=e=>{e.preventDefault(),e.returnValue=""},C="undefined"!=typeof window?(()=>{const e=new Set;let t=[];const n=window.history,r=window.location;let o=k(r.pathname+r.search);const s=()=>{const t=k(r.pathname+r.search);if(t.toString()===o.toString())return;v=!0;const n=t.raw.search!==o.raw.search,s=n?{}:o.search;if(n)for(const e in t.search)if(Object.prototype.hasOwnProperty.call(t.search,e)){const n=t.search[e];if(null==n)continue;const r=o.search[e];s[e]=null!=r&&"string"!=typeof r&&"string"!=typeof n&&h(n,r)?r:n}o={path:t.raw.path!==o.raw.path?t.path:o.path,search:s,raw:t.raw,toString:t.toString},e.forEach(e=>e(o))};return window.addEventListener("popstate",()=>{s()}),{get location(){return o},subscribe:t=>(e.add(t),()=>{e.delete(t)}),push:e=>{const o=c(t);if(null==o||window.confirm(o.message)){try{n.pushState(null,"",e)}catch{r.assign(e)}s()}},replace:e=>{const r=c(t);(null==r||window.confirm(r.message))&&(n.replaceState(null,"",e),s())},block:e=>(t.push(e),1===t.length&&window.addEventListener("beforeunload",S,{capture:!0}),()=>{t=t.filter(t=>{let{id:n}=t;return n!==e.id}),0===t.length&&window.removeEventListener("beforeunload",S,{capture:!0})})}})():{location:k("/"),subscribe:()=>a,push:a,replace:a,block:()=>a},R=()=>C.location,E=C.subscribe,O=C.push,I=C.replace,U=C.block,P=e.createContext(R),j=P.Provider,L=()=>e.useContext(P),A=t=>{let{href:n,replace:r=!1,target:o}=t;const s=e.useMemo(()=>x(n).path,[n]),c=L(),l=()=>s===c().raw.path,a=e.useSyncExternalStore(E,l,l),i=r||a,u=null==o||"_self"===o;return{active:a,onClick:e.useCallback(e=>{e.defaultPrevented||!u||0!==e.button||e.metaKey||e.altKey||e.ctrlKey||e.shiftKey||(e.preventDefault(),i?I(n):O(n))},[i,u,n])}},N=e.forwardRef((t,n)=>{let{onClick:r,className:o,replace:s,style:c,target:l,to:a,activeClassName:i,activeStyle:u,...p}=t;const{active:h,onClick:f}=A({href:a,replace:s,target:l});return e.createElement("a",{...p,ref:n,href:a,onClick:e=>{null==r||r(e),f(e)},target:l,className:h&&null!=i?null==o?i:o+" "+i:o,style:h&&null!=u?null==c?u:{...c,...u}:c})});N.displayName="Link";const T=(e,t)=>""===e?e:t+e,W=(e,t)=>{const n=p(e.path),r=p(t.path),o="/"===n?r:"/"===r?n:n+r,s=""===e.search?t.search:e.search+T(t.search,"&");return o+T(s,"?")},K=(e,t)=>{const n=x(t),r=n.path.endsWith("/*"),{ranking:o,path:s}=(e=>{const t=e.split("/").filter(i),n=[];let r=t.length>0?5*t.length:6;for(const e of t)if(u(e)){const t=m(e.substring(1));r+=null==t.union?2:3,n.push(t)}else r+=4,n.push(encodeURIComponent(e));return{ranking:r,path:n}})(r?n.path.slice(0,-2):n.path),c={isArea:r,name:e,ranking:r?o-1:o,path:s,search:void 0};if(""!==n.search){c.search={};const e=y(n.search);for(const t in e)if(u(t)){const e=t.endsWith("[]"),{name:n,union:r}=m(t.substring(1,t.length-(e?2:0)));c.search[n]=null==r?{multiple:e}:{multiple:e,union:r}}}return c},q=(e,t)=>{const{path:n}=e,{isArea:r,path:o}=t;if(!r&&n.length!==o.length||r&&n.length<o.length)return;const s={};for(let e=0;e<o.length;e++){const t=n[e],r=o[e];if(null==r)continue;if("string"==typeof r){if(t===r)continue;return}if(null==t)return;const{name:c,union:l}=r;if(null!=l&&!l.includes(t))return;s[c]=t}const c={};for(const n in t.search)if(Object.prototype.hasOwnProperty.call(t.search,n)){const r=t.search[n],o=e.search[n];if(null==r||null==o)continue;const{multiple:s,union:l}=r,a="string"==typeof o?[o]:o,i=null==l?a:a.filter(e=>l.includes(e));if(s){c[n]=i;continue}const u=i[0];null==u||null!=l&&!l.includes(u)||(c[n]=u)}return{key:d(t.name,s,c),name:t.name,params:{...s,...c}}},D=(e,t)=>{for(const n of t){const t=q(e,n);if(null!=t)return t}},J=function(e,t){void 0===t&&(t={});const n="/"+e.path.map(e=>encodeURIComponent("string"==typeof e?e:String(t[e.name]))).join("/");let r="";if(null!=e.search){const n={};for(const r in t){const o=e.search[r],s=t[r];if(null!=o&&null!=s){const{union:e}=o;"string"==typeof s?(null==e||e.includes(s))&&(n[r]=s):n[r]=null==e?s:s.filter(t=>e.includes(t))}}r=w(n)}return n+r},M={A:!0,INPUT:!0,SELECT:!0,TEXTAREA:!0};exports.Link=N,exports.ServerUrlProvider=t=>{let{children:n,value:r}=t;const o=k(r);return e.createElement(j,{children:n,value:()=>o})},exports.createGroup=(e,t,n)=>{const r=x(t),o={};for(const t in n)Object.prototype.hasOwnProperty.call(n,t)&&(o[e+t]=W(r,x(n[t])));return o},exports.createRouter=function(t,r){void 0===r&&(r={});const{basePath:o=""}=r,c={path:x(o).path,search:""},a={},i=[];for(const e in t)if(Object.prototype.hasOwnProperty.call(t,e)){const n=K(e,""!==o?W(c,x(t[e])):t[e]);a[e]=n,i.push(n)}i.sort((e,t)=>t.ranking-e.ranking);const u={},p={};for(const e of i){const t=e.name;if(u[t]=e=>({name:t,params:e}),!e.isArea){const e=t;p[e]=t=>J(a[e],t)}}return{useRoute:t=>{const r=JSON.stringify(t),o=e.useMemo(()=>i.filter(e=>{let{name:n}=e;return t.includes(n)}),[r]),s=L(),c=()=>D(s(),o);return n.useSyncExternalStoreWithSelector(E,c,c,l,g)},getRoute:e=>{const t=R(),n=i.filter(t=>{let{name:n}=t;return e.includes(n)});return D(t,n)},push:function(e){return O(J(a[e],s([].slice.call(arguments,1))))},replace:function(e){return I(J(a[e],s([].slice.call(arguments,1))))},P:u,...p}},exports.decodeSearch=e=>y("?"===e[0]?e.substring(1):e),exports.encodeSearch=w,exports.getLocation=R,exports.pushUnsafe=O,exports.replaceUnsafe=I,exports.subscribeToLocation=E,exports.useBlocker=(t,n)=>{const r=e.useId();e.useEffect(()=>{if(t)return U({id:r,message:n})})},exports.useFocusReset=t=>{let{route:n,containerRef:r}=t;const o=null==n?void 0:n.key.split("-")[0];e.useEffect(()=>{const e=r.current;if(null!=e&&v)try{null!=e.getAttribute("tabIndex")||M[e.nodeName]||e.setAttribute("tabIndex","-1"),e.focus()}catch{}},[r,o])},exports.useLinkProps=A,exports.useLocation=()=>{const t=L();return e.useSyncExternalStore(E,t,t)}; | ||
var e=require("react"),t=require("@emotion/hash"),n=require("use-sync-external-store/shim/with-selector.js");function r(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=/*#__PURE__*/r(t);const o=e=>e[0],c=e=>e[e.length-1],a=e=>e,l=()=>{},i=e=>""!==e,u=e=>e.startsWith(":"),p=e=>"/"===e[0]?e:"/"+e,h=(e,t)=>e.length===t.length&&e.every((e,n)=>e===t[n]),f=e=>{const t=Object.keys(e);return t.length>0?JSON.stringify(t.sort().map(t=>[t,e[t]])):""},d=(e,t,n)=>{const r=e+f(t),o=f(n);return s.default(r)+"-"+s.default(o)},g=(e,t)=>(null==e?void 0:e.key)===(null==t?void 0:t.key),m=e=>{const t=e.indexOf("{");return t>-1&&e.endsWith("}")?{name:e.substring(0,t),union:e.substring(t+1,e.length-1).split("|").filter(i)}:{name:e}},y=e=>{const t=new URLSearchParams(e),n={};for(const[e,r]of t){const t=n[e];n[e]=null!=t?"string"==typeof t?[t,r]:t.concat(r):r}return n},b=/=&/g,w=/=$/g,S=e=>{const t=Object.keys(e);if(0===t.length)return"";const n=new URLSearchParams;t.sort();for(const r of t){const t=e[r];if(null!=t)if("string"==typeof t)n.append(r,t);else for(const e of t)n.append(r,e)}const r=n.toString().replace(b,"&").replace(w,"");return""===r?"":"?"+r};let v=!1;const x=e=>{const t=e.indexOf("#"),n=p(t<0?e:e.substring(0,t)),r=n.indexOf("?");return r<0?{path:n,search:""}:{path:n.substring(0,r),search:n.substring(r)}},k=e=>{const t=x(e),n=t.path.substring(1),r=""!==n?v?n.split("/").map(decodeURIComponent):n.split("/").filter(i).map(decodeURIComponent):[],s=""!==t.search?y(t.search):{},o="/"+r.map(encodeURIComponent).join("/"),c=S(s),a=o+c;return{path:r,search:s,raw:{path:o,search:c},toString:()=>a}},E=e=>{e.preventDefault(),e.returnValue=""},O="undefined"!=typeof window?(()=>{const e=new Set;let t=[];const n=window.history,r=window.location;let s=k(r.pathname+r.search);const o=()=>{const t=k(r.pathname+r.search);if(t.toString()===s.toString())return;v=!0;const n=t.raw.search!==s.raw.search,o=n?{}:s.search;if(n)for(const e in t.search)if(Object.prototype.hasOwnProperty.call(t.search,e)){const n=t.search[e];if(null==n)continue;const r=s.search[e];o[e]=null!=r&&"string"!=typeof r&&"string"!=typeof n&&h(n,r)?r:n}s={path:t.raw.path!==s.raw.path?t.path:s.path,search:o,raw:t.raw,toString:t.toString},e.forEach(e=>e(s))};return window.addEventListener("popstate",()=>{o()}),{get location(){return s},subscribe:t=>(e.add(t),()=>{e.delete(t)}),push:e=>{const s=c(t);if(null==s||window.confirm(s.message)){try{n.pushState(null,"",e)}catch{r.assign(e)}o()}},replace:e=>{const r=c(t);(null==r||window.confirm(r.message))&&(n.replaceState(null,"",e),o())},block:e=>(t.push(e),1===t.length&&window.addEventListener("beforeunload",E,{capture:!0}),()=>{t=t.filter(t=>{let{id:n}=t;return n!==e.id}),0===t.length&&window.removeEventListener("beforeunload",E,{capture:!0})})}})():{location:k("/"),subscribe:()=>l,push:l,replace:l,block:()=>l},R=()=>O.location,C=O.subscribe,P=O.push,L=O.replace,U=O.block,j=e.createContext(R),I=j.Provider,A=()=>e.useContext(j),N=t=>{let{href:n,replace:r=!1,target:s}=t;const o=e.useMemo(()=>x(n).path,[n]),c=A(),a=()=>o===c().raw.path,l=e.useSyncExternalStore(C,a,a),i=r||l,u=null==s||"_self"===s;return{active:l,onClick:e.useCallback(e=>{e.defaultPrevented||!u||0!==e.button||e.metaKey||e.altKey||e.ctrlKey||e.shiftKey||(e.preventDefault(),i?L(n):P(n))},[i,u,n])}},T=e.forwardRef((t,n)=>{let{onClick:r,className:s,replace:o,style:c,target:a,to:l,activeClassName:i,activeStyle:u,...p}=t;const{active:h,onClick:f}=N({href:l,replace:o,target:a});return e.createElement("a",{...p,ref:n,href:l,onClick:e=>{null==r||r(e),f(e)},target:a,className:h&&null!=i?null==s?i:s+" "+i:s,style:h&&null!=u?null==c?u:{...c,...u}:c})});T.displayName="Link";const W=(e,t)=>""===e?e:t+e,K=(e,t)=>{const n=p(e.path),r=p(t.path),s=e.search.substring(1),o=t.search.substring(1),c="/"===n?r:"/"===r?n:n+r,a=""===s?o:s+W(o,"&");return c+W(a,"?")},q=(e,t)=>{const n=x(t),r=n.path.endsWith("/*"),{ranking:s,path:o}=(e=>{const t=e.split("/").filter(i),n=[];let r=t.length>0?5*t.length:6;for(const e of t)if(u(e)){const t=m(e.substring(1));r+=null==t.union?2:3,n.push(t)}else r+=4,n.push(encodeURIComponent(e));return{ranking:r,path:n}})(r?n.path.slice(0,-2):n.path),c={isArea:r,name:e,ranking:r?s-1:s,path:o,search:void 0};if(""!==n.search){c.search={};const e=new URLSearchParams(n.search);for(const[t]of e)if(u(t)){const e=t.endsWith("[]"),{name:n,union:r}=m(t.substring(1,t.length-(e?2:0)));c.search[n]=null==r?{multiple:e}:{multiple:e,union:r}}}return c},D=(e,t)=>{const{path:n}=e,{isArea:r,path:s}=t;if(!r&&n.length!==s.length||r&&n.length<s.length)return;const o={};for(let e=0;e<s.length;e++){const t=n[e],r=s[e];if(null==r)continue;if("string"==typeof r){if(t===r)continue;return}if(null==t)return;const{name:c,union:a}=r;if(null!=a&&!a.includes(t))return;o[c]=t}const c={};for(const n in t.search)if(Object.prototype.hasOwnProperty.call(t.search,n)){const r=t.search[n],s=e.search[n];if(null==r||null==s)continue;const{multiple:o,union:a}=r,l="string"==typeof s?[s]:s,i=null==a?l:l.filter(e=>a.includes(e));if(o){c[n]=i;continue}const u=i[0];null==u||null!=a&&!a.includes(u)||(c[n]=u)}return{key:d(t.name,o,c),name:t.name,params:{...o,...c}}},J=(e,t)=>{for(const n of t){const t=D(e,n);if(null!=t)return t}},M=function(e,t){void 0===t&&(t={});const n="/"+e.path.map(e=>encodeURIComponent("string"==typeof e?e:String(t[e.name]))).join("/");let r="";if(null!=e.search){const n={};for(const r in t){const s=e.search[r],o=t[r];if(null!=s&&null!=o){const{union:e}=s;"string"==typeof o?(null==e||e.includes(o))&&(n[r]=o):n[r]=null==e?o:o.filter(t=>e.includes(t))}}r=S(n)}return n+r},B={A:!0,INPUT:!0,SELECT:!0,TEXTAREA:!0};exports.Link=T,exports.ServerUrlProvider=t=>{let{children:n,value:r}=t;const s=k(r);return e.createElement(I,{children:n,value:()=>s})},exports.createGroup=(e,t,n)=>{const r=x(t),s={};for(const t in n)Object.prototype.hasOwnProperty.call(n,t)&&(s[e+t]=K(r,x(n[t])));return s},exports.createRouter=function(t,r){void 0===r&&(r={});const{basePath:s=""}=r,c={path:x(s).path,search:""},l={},i=[];for(const e in t)if(Object.prototype.hasOwnProperty.call(t,e)){const n=q(e,""!==s?K(c,x(t[e])):t[e]);l[e]=n,i.push(n)}i.sort((e,t)=>t.ranking-e.ranking);const u={},p={};for(const e of i){const t=e.name;if(u[t]=e=>({name:t,params:e}),!e.isArea){const e=t;p[e]=t=>M(l[e],t)}}return{useRoute:t=>{const r=JSON.stringify(t),s=e.useMemo(()=>i.filter(e=>{let{name:n}=e;return t.includes(n)}),[r]),o=A(),c=()=>J(o(),s);return n.useSyncExternalStoreWithSelector(C,c,c,a,g)},getRoute:e=>{const t=R(),n=i.filter(t=>{let{name:n}=t;return e.includes(n)});return J(t,n)},push:function(e){return P(M(l[e],o([].slice.call(arguments,1))))},replace:function(e){return L(M(l[e],o([].slice.call(arguments,1))))},P:u,...p}},exports.decodeSearch=y,exports.encodeSearch=S,exports.getLocation=R,exports.pushUnsafe=P,exports.replaceUnsafe=L,exports.subscribeToLocation=C,exports.useBlocker=(t,n)=>{const r=e.useId();e.useEffect(()=>{if(t)return U({id:r,message:n})})},exports.useFocusReset=t=>{let{route:n,containerRef:r}=t;const s=null==n?void 0:n.key.split("-")[0];e.useEffect(()=>{const e=r.current;if(null!=e&&v)try{null!=e.getAttribute("tabIndex")||B[e.nodeName]||e.setAttribute("tabIndex","-1"),e.focus()}catch{}},[r,s])},exports.useLinkProps=N,exports.useLocation=()=>{const t=A();return e.useSyncExternalStore(C,t,t)}; | ||
//# sourceMappingURL=index.js.map |
import { Search } from "./types"; | ||
export declare const decodeUnprefixedSearch: (search: string) => Search; | ||
export declare const decodeSearch: (search: string) => Search; | ||
export declare const appendParam: (acc: string, key: string, value: string) => string; | ||
export declare const encodeSearch: (search: Search) => string; |
{ | ||
"name": "@swan-io/chicane", | ||
"version": "2.0.0-rc.1", | ||
"version": "2.0.0-rc.2", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "A simple and safe router for React and TypeScript", |
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
116792
319