@swan-io/chicane
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,5 +0,16 @@ | ||
import { Location, Subscription } from "./types"; | ||
import { Location, Search, Subscription } from "./types"; | ||
export declare const history: import("history").BrowserHistory; | ||
export declare const subscribeToLocation: (subscription: Subscription) => (() => void); | ||
export declare const getLocation: () => import("./types").MutableLocation; | ||
export declare const getLocation: () => Readonly<{ | ||
key: string; | ||
path: readonly string[]; | ||
search: Readonly<Search>; | ||
hash?: string; | ||
raw: Readonly<{ | ||
path: string; | ||
search: string; | ||
hash: string; | ||
}>; | ||
toString(): string; | ||
}>; | ||
export declare const hasInitialLocationChanged: () => boolean; | ||
@@ -6,0 +17,0 @@ export declare const useLocation: () => Location; |
@@ -1,2 +0,2 @@ | ||
import{createBrowserHistory as n,createPath as e,parsePath as t}from"history";import*as r from"react";import{useSyncExternalStoreWithSelector as a}from"use-sync-external-store/shim/with-selector";import{useSyncExternalStore as o}from"use-sync-external-store/shim";import i from"@emotion/hash";var u=function(n){return n[0]},l=function(n){return n},c=function(n){return""!==n},s=function(n){return n.startsWith(":")},f=function(n,e){return n.length===e.length&&n.every(function(n,t){return n===e[t]})},h=function(n,e){return(null==n?void 0:n.key)===(null==e?void 0:e.key)},p=function(n){return"/"===n[0]?n:"/"+n},v=function(n,e){var t=p(n),r=p(e);return"/"===t?r:"/"===r?t:t+r},m=function(n,e,t){var r={};for(var a in t)Object.prototype.hasOwnProperty.call(t,a)&&(r[n+a]=v(e,t[a]));return r};function g(){return g=Object.assign||function(n){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(n[r]=t[r])}return n},g.apply(this,arguments)}function y(n,e){(null==e||e>n.length)&&(e=n.length);for(var t=0,r=new Array(e);t<e;t++)r[t]=n[t];return r}function d(n,e){var t="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(t)return(t=t.call(n)).next.bind(t);if(Array.isArray(n)||(t=function(n,e){if(n){if("string"==typeof n)return y(n,e);var t=Object.prototype.toString.call(n).slice(8,-1);return"Object"===t&&n.constructor&&(t=n.constructor.name),"Map"===t||"Set"===t?Array.from(n):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?y(n,e):void 0}}(n))||e&&n&&"number"==typeof n.length){t&&(n=t);var r=0;return function(){return r>=n.length?{done:!0}:{done:!1,value:n[r++]}}}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 b=function(n){for(var e,t={},r=d(new URLSearchParams(n));!(e=r()).done;){var a=e.value,o=a[0],i=a[1],u=t[o];t[o]=null!=u?"string"==typeof u?[u,i]:u.concat(i):i}return t},k=function(n,e,t){var r=n+(""!==n?"&":"")+encodeURIComponent(e);return""!==t?r+"="+encodeURIComponent(t):r},w=function(n){var e=Object.keys(n);if(0===e.length)return"";var t="";e.sort();for(var r=0,a=e;r<a.length;r++){var o=a[r],i=n[o];if(null!=i)if("string"==typeof i)t=k(t,o,i);else for(var u,l=d(i);!(u=l()).done;)t=k(t,o,u.value)}return""===t?"":"?"+t},O=function(n,e){var t=n.search,r=n.hash,a=n.pathname.substring(1),o=""!==a?e?a.split("/").filter(c).map(decodeURIComponent):a.split("/").map(decodeURIComponent):[],u=""!==t?b(t):{},l=""!==r?decodeURIComponent(r.substring(1)):null,s="/"+o.map(encodeURIComponent).join("/"),f=w(u),h=null!=l?"#"+encodeURIComponent(l):"",p=s+f+h;return g({key:i(s)+"-"+i(f+h),path:o,search:u},null!==l&&{hash:l},{raw:{path:s,search:f,hash:h},toString:function(){return p}})},C=new Set,S=n(),I=O(S.location,!0),R=!1;I.toString()!==e(S.location)&&S.replace(I.toString()),S.listen(function(n){var e=O(n.location,!1);if(e.toString()!==I.toString()){R=!0;var t=e.raw.search!==I.raw.search,r=t?{}:I.search;if(t)for(var a in e.search)if(Object.prototype.hasOwnProperty.call(e.search,a)){var o=e.search[a];if(null==o)continue;var i=I.search[a];r[a]=null!=i&&"string"!=typeof i&&"string"!=typeof o&&f(o,i)?i:o}I=g({key:e.key,path:e.raw.path!==I.raw.path?e.path:I.path,search:r},null!=e.hash&&{hash:e.hash},{raw:e.raw,toString:e.toString}),C.forEach(function(n){return n(I)})}});var j=function(n){return C.add(n),function(){C.delete(n)}},U=function(){return I},P=function(){return o(j,U)},A=function(n){var e=t(n),r=e.pathname,a=e.search,o=e.hash;S.push({pathname:void 0===r?"":r,search:void 0===a?"":a,hash:void 0===o?"":o})},E=function(n){var e=t(n),r=e.pathname,a=e.search,o=e.hash;S.replace({pathname:void 0===r?"":r,search:void 0===a?"":a,hash:void 0===o?"":o})},N=function(n){for(var e,t=n.split("/").filter(c),r=[],a=t.length>0?4*t.length:5,o=d(t);!(e=o()).done;){var i=e.value,u=s(i),l=u?i.substring(1):encodeURIComponent(i);a+=u?2:3,r.push({name:l,param:u})}return{ranking:a,segments:r}},x=function(n,e){if(e.endsWith("*")){var r=t(e.slice(0,-1)).pathname,a=N(void 0===r?"/":r);return{finite:!1,name:n,ranking:a.ranking-1,segments:a.segments,search:{}}}for(var o,i=t(e),u=i.pathname,l=i.search,c=void 0===l?"":l,f=i.hash,h=void 0===f?"":f,p=N(void 0===u?"/":u),v=p.ranking,m=p.segments,y={},b=d(new URLSearchParams(c.substring(1)));!(o=b()).done;){var k=o.value[0];(w=k).startsWith(":")&&w.endsWith("[]")?y[k.substring(1,k.length-2)]="multiple":s(k)&&(y[k.substring(1,k.length)]="unique")}return g({finite:!0,name:n,ranking:v,segments:m,search:y},s(h.substring(1))&&{hash:h.substring(2)});var w},T=function(n,e){var t=n.path,r=e.finite,a=e.segments;if(!(r&&t.length!==a.length||!r&&t.length<a.length)){for(var o={},i=0;i<a.length;i++){var u=t[i],l=a[i];if(null!=l)if(l.param){if(null==u)return;o[l.name]=u}else if(l.name!==u)return}if(!r)return o;for(var c in e.search)if(Object.prototype.hasOwnProperty.call(e.search,c)){var s=e.search[c],f=n.search[c];if(null==s||null==f)continue;if("multiple"===s){o[c]="string"==typeof f?[f]:f;continue}"string"==typeof f?o[c]=f:null!=f[0]&&(o[c]=f[0])}return null!=e.hash&&null!=n.hash&&(o[e.hash]=n.hash),o}},K=function(n,e){void 0===e&&(e={});var t="/"+n.segments.map(function(n){var t=n.name,r=e[t];return encodeURIComponent(n.param&&"string"==typeof r?r:t)}).join("/"),r="",a="";if(null!=n.search){var o={};for(var i in e){var u=e[i];Object.prototype.hasOwnProperty.call(e,i)&&Object.prototype.hasOwnProperty.call(n.search,i)&&null!=u&&(o[i]=u)}r=w(o)}if(null!=n.hash){var l=e[n.hash];"string"==typeof l&&(a="#"+encodeURIComponent(l))}return{pathname:t,search:r,hash:a}},L=function(n,t){void 0===t&&(t={});var o=t.basePath,i=void 0===o?"":o,c={},s=[];for(var f in n)if(Object.prototype.hasOwnProperty.call(n,f)){var p=x(f,v(i,n[f]));c[f]=p,s.push(p)}s.sort(function(n,e){return e.ranking-n.ranking});for(var m={},y=0;y<s.length;y++){var b=s[y];null!=b&&b.finite&&function(){var n=b.name;m[n]=function(t){return e(K(c[n],t))}}()}return g({useRoute:function(n){var e=r.useMemo(function(){return s.filter(function(e){return n.includes(e.name)})},[JSON.stringify(n)]);return a(j,function(){return function(n,e){for(var t,r=d(e);!(t=r()).done;){var a=t.value,o=T(n,a);if(null!=o)return{key:n.key,name:a.name,params:o}}}(U(),e)},void 0,l,h)},push:function(n){return S.push(K(c[n],u([].slice.call(arguments,1))))},replace:function(n){return S.replace(K(c[n],u([].slice.call(arguments,1))))}},m)},W=function(n){var e=n.href,a=n.replace,i=void 0!==a&&a,u=n.target,l=r.useMemo(function(){return t(e).pathname},[e]),c=o(j,function(){return l===U().raw.path}),s=i||c,f=!u||"_self"===u;return{active:c,onClick:r.useCallback(function(n){n.defaultPrevented||!f||0!==n.button||n.metaKey||n.altKey||n.ctrlKey||n.shiftKey||(n.preventDefault(),s?E(e):A(e))},[s,f,e])}},M=["onClick","className","replace","style","target","to","activeClassName","activeStyle"],q=r.forwardRef(function(n,e){var t=n.onClick,a=n.className,o=n.replace,i=n.style,u=n.target,l=n.to,c=n.activeClassName,s=n.activeStyle,f=function(n,e){if(null==n)return{};var t,r,a={},o=Object.keys(n);for(r=0;r<o.length;r++)e.indexOf(t=o[r])>=0||(a[t]=n[t]);return a}(n,M),h=W({href:l,replace:o,target:u}),p=h.active,v=h.onClick;return r.createElement("a",g({},f,{ref:e,href:l,onClick:function(n){null==t||t(n),v(n)},target:u,className:p&&null!=c?null==a?c:a+" "+c:a,style:p&&null!=s?null==i?s:g({},i,s):i}))});q.displayName="Link";var D={A:!0,INPUT:!0,SELECT:!0,TEXTAREA:!0},J=function(n){var e=n.route,t=n.containerRef,a=null==e?void 0:e.key.split("-")[0];r.useEffect(function(){var n=t.current;if(n&&R)try{null!=n.getAttribute("tabIndex")||D[n.nodeName]||n.setAttribute("tabIndex","-1"),n.focus()}catch(n){}},[t,a])},X=function(n,e){r.useEffect(function(){if(n){var t=S.block(function(n){window.confirm(e)&&(t(),n.retry())});return t}},[n])};export{q as Link,m as createGroup,L as createRouter,b as decodeSearch,w as encodeSearch,U as getLocation,A as pushUnsafe,E as replaceUnsafe,j as subscribeToLocation,J as useFocusReset,W as useLinkProps,P as useLocation,X as useNavigationBlocker}; | ||
import{createBrowserHistory as n,createPath as t,parsePath as r}from"history";import*as e from"react";import{useSyncExternalStoreWithSelector as a}from"use-sync-external-store/shim/with-selector";import{useSyncExternalStore as o}from"use-sync-external-store/shim";import i from"@emotion/hash";var u=function(n){return n[0]},l=function(n){return n},c=function(n){return""!==n},s=function(n){return n.startsWith(":")},f=function(n,t){return n.length===t.length&&n.every(function(n,r){return n===t[r]})},h=function(n,t){return(null==n?void 0:n.key)===(null==t?void 0:t.key)},p=function(n){return"/"===n[0]?n:"/"+n},v=function(n,t){var r=p(n),e=p(t);return"/"===r?e:"/"===e?r:r+e},m=function(n,t,r){var e={};for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(e[n+a]=v(t,r[a]));return e};function y(){return y=Object.assign||function(n){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var e in r)Object.prototype.hasOwnProperty.call(r,e)&&(n[e]=r[e])}return n},y.apply(this,arguments)}function d(n,t){(null==t||t>n.length)&&(t=n.length);for(var r=0,e=new Array(t);r<t;r++)e[r]=n[r];return e}function g(n,t){var r="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(r)return(r=r.call(n)).next.bind(r);if(Array.isArray(n)||(r=function(n,t){if(n){if("string"==typeof n)return d(n,t);var r=Object.prototype.toString.call(n).slice(8,-1);return"Object"===r&&n.constructor&&(r=n.constructor.name),"Map"===r||"Set"===r?Array.from(n):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?d(n,t):void 0}}(n))||t&&n&&"number"==typeof n.length){r&&(n=r);var e=0;return function(){return e>=n.length?{done:!0}:{done:!1,value:n[e++]}}}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 b=function(n){for(var t,r={},e=g(new URLSearchParams(n));!(t=e()).done;){var a=t.value,o=a[0],i=a[1],u=r[o];r[o]=null!=u?"string"==typeof u?[u,i]:u.concat(i):i}return r},k=function(n,t,r){var e=n+(""!==n?"&":"")+encodeURIComponent(t);return""!==r?e+"="+encodeURIComponent(r):e},w=function(n){var t=Object.keys(n);if(0===t.length)return"";var r="";t.sort();for(var e=0,a=t;e<a.length;e++){var o=a[e],i=n[o];if(null!=i)if("string"==typeof i)r=k(r,o,i);else for(var u,l=g(i);!(u=l()).done;)r=k(r,o,u.value)}return""===r?"":"?"+r},O=function(n,t){var r=n.search,e=n.hash,a=n.pathname.substring(1),o=""!==a?t?a.split("/").filter(c).map(decodeURIComponent):a.split("/").map(decodeURIComponent):[],u=""!==r?b(r):{},l=""!==e?decodeURIComponent(e.substring(1)):null,s="/"+o.map(encodeURIComponent).join("/"),f=w(u),h=null!=l?"#"+encodeURIComponent(l):"",p=s+f+h;return y({key:i(s)+"-"+i(f+h),path:o,search:u},null!==l&&{hash:l},{raw:{path:s,search:f,hash:h},toString:function(){return p}})},C=new Set,S=n(),I=O(S.location,!0),R=!1;I.toString()!==t(S.location)&&S.replace(I.toString()),S.listen(function(n){var t=O(n.location,!1);if(t.toString()!==I.toString()){R=!0;var r=t.raw.search!==I.raw.search,e=r?{}:I.search;if(r)for(var a in t.search)if(Object.prototype.hasOwnProperty.call(t.search,a)){var o=t.search[a];if(null==o)continue;var i=I.search[a];e[a]=null!=i&&"string"!=typeof i&&"string"!=typeof o&&f(o,i)?i:o}I=y({key:t.key,path:t.raw.path!==I.raw.path?t.path:I.path,search:e},null!=t.hash&&{hash:t.hash},{raw:t.raw,toString:t.toString}),C.forEach(function(n){return n(I)})}});var j=function(n){return C.add(n),function(){C.delete(n)}},U=function(){return I},P=function(){return o(j,U)},A=function(n){var t=r(n),e=t.pathname,a=t.search,o=t.hash;S.push({pathname:void 0===e?"":e,search:void 0===a?"":a,hash:void 0===o?"":o})},E=function(n){var t=r(n),e=t.pathname,a=t.search,o=t.hash;S.replace({pathname:void 0===e?"":e,search:void 0===a?"":a,hash:void 0===o?"":o})},N=function(n){for(var t,r=n.split("/").filter(c),e=[],a=r.length>0?4*r.length:5,o=g(r);!(t=o()).done;){var i=t.value,u=s(i);a+=u?2:3,e.push(u?{name:i.substring(1)}:encodeURIComponent(i))}return{ranking:a,path:e}},x=function(n,t){if(t.endsWith("*")){var e=r(t.slice(0,-1)).pathname,a=N(void 0===e?"/":e);return{finite:!1,name:n,ranking:a.ranking-1,path:a.path,search:{},hash:void 0}}for(var o,i=r(t),u=i.pathname,l=i.search,c=void 0===l?"":l,f=i.hash,h=void 0===f?"":f,p=N(void 0===u?"/":u),v=p.ranking,m=p.path,y={},d=g(new URLSearchParams(c.substring(1)));!(o=d()).done;){var b=o.value[0];(k=b).startsWith(":")&&k.endsWith("[]")?y[b.substring(1,b.length-2)]="multiple":s(b)&&(y[b.substring(1,b.length)]="unique")}return{finite:!0,name:n,ranking:v,path:m,search:y,hash:s(h.substring(1))?h.substring(2):void 0};var k},T=function(n,t){var r=n.path,e=t.finite,a=t.path;if(!(e&&r.length!==a.length||!e&&r.length<a.length)){for(var o={},i=0;i<a.length;i++){var u=r[i],l=a[i];if(null!=l)if("string"==typeof l){if(l!==u)return}else{if(null==u)return;o[l.name]=u}}if(!e)return o;for(var c in t.search)if(Object.prototype.hasOwnProperty.call(t.search,c)){var s=t.search[c],f=n.search[c];if(null==s||null==f)continue;if("multiple"===s){o[c]="string"==typeof f?[f]:f;continue}"string"==typeof f?o[c]=f:null!=f[0]&&(o[c]=f[0])}return null!=t.hash&&null!=n.hash&&(o[t.hash]=n.hash),o}},K=function(n,t){void 0===t&&(t={});var r="/"+n.path.map(function(n){return encodeURIComponent("string"==typeof n?n:String(t[n.name]))}).join("/"),e="",a="";if(null!=n.search){var o={};for(var i in t){var u=t[i];Object.prototype.hasOwnProperty.call(t,i)&&Object.prototype.hasOwnProperty.call(n.search,i)&&null!=u&&(o[i]=u)}e=w(o)}if(null!=n.hash){var l=t[n.hash];"string"==typeof l&&(a="#"+encodeURIComponent(l))}return{pathname:r,search:e,hash:a}},L=function(n,r){void 0===r&&(r={});var o=r.basePath,i=void 0===o?"":o,c={},s=[];for(var f in n)if(Object.prototype.hasOwnProperty.call(n,f)){var p=x(f,v(i,n[f]));c[f]=p,s.push(p)}s.sort(function(n,t){return t.ranking-n.ranking});for(var m={},d=0;d<s.length;d++){var b=s[d];null!=b&&b.finite&&function(){var n=b.name;m[n]=function(r){return t(K(c[n],r))}}()}return y({useRoute:function(n){var t=e.useMemo(function(){return s.filter(function(t){return n.includes(t.name)})},[JSON.stringify(n)]);return a(j,function(){return function(n,t){for(var r,e=g(t);!(r=e()).done;){var a=r.value,o=T(n,a);if(null!=o)return{key:n.key,name:a.name,params:o}}}(U(),t)},void 0,l,h)},push:function(n){return S.push(K(c[n],u([].slice.call(arguments,1))))},replace:function(n){return S.replace(K(c[n],u([].slice.call(arguments,1))))}},m)},W=function(n){var t=n.href,a=n.replace,i=void 0!==a&&a,u=n.target,l=e.useMemo(function(){return r(t).pathname},[t]),c=o(j,function(){return l===U().raw.path}),s=i||c,f=!u||"_self"===u;return{active:c,onClick:e.useCallback(function(n){n.defaultPrevented||!f||0!==n.button||n.metaKey||n.altKey||n.ctrlKey||n.shiftKey||(n.preventDefault(),s?E(t):A(t))},[s,f,t])}},M=["onClick","className","replace","style","target","to","activeClassName","activeStyle"],q=e.forwardRef(function(n,t){var r=n.onClick,a=n.className,o=n.replace,i=n.style,u=n.target,l=n.to,c=n.activeClassName,s=n.activeStyle,f=function(n,t){if(null==n)return{};var r,e,a={},o=Object.keys(n);for(e=0;e<o.length;e++)t.indexOf(r=o[e])>=0||(a[r]=n[r]);return a}(n,M),h=W({href:l,replace:o,target:u}),p=h.active,v=h.onClick;return e.createElement("a",y({},f,{ref:t,href:l,onClick:function(n){null==r||r(n),v(n)},target:u,className:p&&null!=c?null==a?c:a+" "+c:a,style:p&&null!=s?null==i?s:y({},i,s):i}))});q.displayName="Link";var D={A:!0,INPUT:!0,SELECT:!0,TEXTAREA:!0},J=function(n){var t=n.route,r=n.containerRef,a=null==t?void 0:t.key.split("-")[0];e.useEffect(function(){var n=r.current;if(n&&R)try{null!=n.getAttribute("tabIndex")||D[n.nodeName]||n.setAttribute("tabIndex","-1"),n.focus()}catch(n){}},[r,a])},X=function(n,t){e.useEffect(function(){if(n){var r=S.block(function(n){window.confirm(t)&&(r(),n.retry())});return r}},[n])};export{q as Link,m as createGroup,L as createRouter,b as decodeSearch,w as encodeSearch,U as getLocation,A as pushUnsafe,E as replaceUnsafe,j as subscribeToLocation,J as useFocusReset,W as useLinkProps,P as useLocation,X as useNavigationBlocker}; | ||
//# sourceMappingURL=index.esm.js.map |
@@ -1,2 +0,2 @@ | ||
var e=require("history"),t=require("react"),r=require("use-sync-external-store/shim/with-selector"),n=require("use-sync-external-store/shim"),a=require("@emotion/hash");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function i(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}}),t.default=e,t}var u=/*#__PURE__*/i(t),c=/*#__PURE__*/o(a),s=function(e){return e[0]},l=function(e){return e},f=function(e){return""!==e},h=function(e){return e.startsWith(":")},p=function(e,t){return e.length===t.length&&e.every(function(e,r){return e===t[r]})},v=function(e,t){return(null==e?void 0:e.key)===(null==t?void 0:t.key)},m=function(e){return"/"===e[0]?e:"/"+e},g=function(e,t){var r=m(e),n=m(t);return"/"===r?n:"/"===n?r:r+n};function d(){return d=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},d.apply(this,arguments)}function y(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function b(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(r)return(r=r.call(e)).next.bind(r);if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return y(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?y(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0;return function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}}}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 k=function(e){for(var t,r={},n=b(new URLSearchParams(e));!(t=n()).done;){var a=t.value,o=a[0],i=a[1],u=r[o];r[o]=null!=u?"string"==typeof u?[u,i]:u.concat(i):i}return r},S=function(e,t,r){var n=e+(""!==e?"&":"")+encodeURIComponent(t);return""!==r?n+"="+encodeURIComponent(r):n},w=function(e){var t=Object.keys(e);if(0===t.length)return"";var r="";t.sort();for(var n=0,a=t;n<a.length;n++){var o=a[n],i=e[o];if(null!=i)if("string"==typeof i)r=S(r,o,i);else for(var u,c=b(i);!(u=c()).done;)r=S(r,o,u.value)}return""===r?"":"?"+r},O=function(e,t){var r=e.search,n=e.hash,a=e.pathname.substring(1),o=""!==a?t?a.split("/").filter(f).map(decodeURIComponent):a.split("/").map(decodeURIComponent):[],i=""!==r?k(r):{},u=""!==n?decodeURIComponent(n.substring(1)):null,s="/"+o.map(encodeURIComponent).join("/"),l=w(i),h=null!=u?"#"+encodeURIComponent(u):"",p=s+l+h;return d({key:c.default(s)+"-"+c.default(l+h),path:o,search:i},null!==u&&{hash:u},{raw:{path:s,search:l,hash:h},toString:function(){return p}})},x=new Set,P=e.createBrowserHistory(),j=O(P.location,!0),C=!1;j.toString()!==e.createPath(P.location)&&P.replace(j.toString()),P.listen(function(e){var t=O(e.location,!1);if(t.toString()!==j.toString()){C=!0;var r=t.raw.search!==j.raw.search,n=r?{}:j.search;if(r)for(var a in t.search)if(Object.prototype.hasOwnProperty.call(t.search,a)){var o=t.search[a];if(null==o)continue;var i=j.search[a];n[a]=null!=i&&"string"!=typeof i&&"string"!=typeof o&&p(o,i)?i:o}j=d({key:t.key,path:t.raw.path!==j.raw.path?t.path:j.path,search:n},null!=t.hash&&{hash:t.hash},{raw:t.raw,toString:t.toString}),x.forEach(function(e){return e(j)})}});var R=function(e){return x.add(e),function(){x.delete(e)}},I=function(){return j},U=function(t){var r=e.parsePath(t),n=r.pathname,a=r.search,o=r.hash;P.push({pathname:void 0===n?"":n,search:void 0===a?"":a,hash:void 0===o?"":o})},E=function(t){var r=e.parsePath(t),n=r.pathname,a=r.search,o=r.hash;P.replace({pathname:void 0===n?"":n,search:void 0===a?"":a,hash:void 0===o?"":o})},A=function(e){for(var t,r=e.split("/").filter(f),n=[],a=r.length>0?4*r.length:5,o=b(r);!(t=o()).done;){var i=t.value,u=h(i),c=u?i.substring(1):encodeURIComponent(i);a+=u?2:3,n.push({name:c,param:u})}return{ranking:a,segments:n}},N=function(t,r){if(r.endsWith("*")){var n=e.parsePath(r.slice(0,-1)).pathname,a=A(void 0===n?"/":n);return{finite:!1,name:t,ranking:a.ranking-1,segments:a.segments,search:{}}}for(var o,i=e.parsePath(r),u=i.pathname,c=i.search,s=void 0===c?"":c,l=i.hash,f=void 0===l?"":l,p=A(void 0===u?"/":u),v=p.ranking,m=p.segments,g={},y=b(new URLSearchParams(s.substring(1)));!(o=y()).done;){var k=o.value[0];(S=k).startsWith(":")&&S.endsWith("[]")?g[k.substring(1,k.length-2)]="multiple":h(k)&&(g[k.substring(1,k.length)]="unique")}return d({finite:!0,name:t,ranking:v,segments:m,search:g},h(f.substring(1))&&{hash:f.substring(2)});var S},L=function(e,t){var r=e.path,n=t.finite,a=t.segments;if(!(n&&r.length!==a.length||!n&&r.length<a.length)){for(var o={},i=0;i<a.length;i++){var u=r[i],c=a[i];if(null!=c)if(c.param){if(null==u)return;o[c.name]=u}else if(c.name!==u)return}if(!n)return o;for(var s in t.search)if(Object.prototype.hasOwnProperty.call(t.search,s)){var l=t.search[s],f=e.search[s];if(null==l||null==f)continue;if("multiple"===l){o[s]="string"==typeof f?[f]:f;continue}"string"==typeof f?o[s]=f:null!=f[0]&&(o[s]=f[0])}return null!=t.hash&&null!=e.hash&&(o[t.hash]=e.hash),o}},q=function(e,t){void 0===t&&(t={});var r="/"+e.segments.map(function(e){var r=e.name,n=t[r];return encodeURIComponent(e.param&&"string"==typeof n?n:r)}).join("/"),n="",a="";if(null!=e.search){var o={};for(var i in t){var u=t[i];Object.prototype.hasOwnProperty.call(t,i)&&Object.prototype.hasOwnProperty.call(e.search,i)&&null!=u&&(o[i]=u)}n=w(o)}if(null!=e.hash){var c=t[e.hash];"string"==typeof c&&(a="#"+encodeURIComponent(c))}return{pathname:r,search:n,hash:a}},T=function(t){var r=t.href,a=t.replace,o=void 0!==a&&a,i=t.target,c=u.useMemo(function(){return e.parsePath(r).pathname},[r]),s=n.useSyncExternalStore(R,function(){return c===I().raw.path}),l=o||s,f=!i||"_self"===i;return{active:s,onClick:u.useCallback(function(e){e.defaultPrevented||!f||0!==e.button||e.metaKey||e.altKey||e.ctrlKey||e.shiftKey||(e.preventDefault(),l?E(r):U(r))},[l,f,r])}},W=["onClick","className","replace","style","target","to","activeClassName","activeStyle"],K=u.forwardRef(function(e,t){var r=e.onClick,n=e.className,a=e.replace,o=e.style,i=e.target,c=e.to,s=e.activeClassName,l=e.activeStyle,f=function(e,t){if(null==e)return{};var r,n,a={},o=Object.keys(e);for(n=0;n<o.length;n++)t.indexOf(r=o[n])>=0||(a[r]=e[r]);return a}(e,W),h=T({href:c,replace:a,target:i}),p=h.active,v=h.onClick;return u.createElement("a",d({},f,{ref:t,href:c,onClick:function(e){null==r||r(e),v(e)},target:i,className:p&&null!=s?null==n?s:n+" "+s:n,style:p&&null!=l?null==o?l:d({},o,l):o}))});K.displayName="Link";var M={A:!0,INPUT:!0,SELECT:!0,TEXTAREA:!0};exports.Link=K,exports.createGroup=function(e,t,r){var n={};for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(n[e+a]=g(t,r[a]));return n},exports.createRouter=function(t,n){void 0===n&&(n={});var a=n.basePath,o=void 0===a?"":a,i={},c=[];for(var f in t)if(Object.prototype.hasOwnProperty.call(t,f)){var h=N(f,g(o,t[f]));i[f]=h,c.push(h)}c.sort(function(e,t){return t.ranking-e.ranking});for(var p={},m=0;m<c.length;m++){var y=c[m];null!=y&&y.finite&&function(){var t=y.name;p[t]=function(r){return e.createPath(q(i[t],r))}}()}return d({useRoute:function(e){var t=u.useMemo(function(){return c.filter(function(t){return e.includes(t.name)})},[JSON.stringify(e)]);return r.useSyncExternalStoreWithSelector(R,function(){return function(e,t){for(var r,n=b(t);!(r=n()).done;){var a=r.value,o=L(e,a);if(null!=o)return{key:e.key,name:a.name,params:o}}}(I(),t)},void 0,l,v)},push:function(e){return P.push(q(i[e],s([].slice.call(arguments,1))))},replace:function(e){return P.replace(q(i[e],s([].slice.call(arguments,1))))}},p)},exports.decodeSearch=k,exports.encodeSearch=w,exports.getLocation=I,exports.pushUnsafe=U,exports.replaceUnsafe=E,exports.subscribeToLocation=R,exports.useFocusReset=function(e){var t=e.route,r=e.containerRef,n=null==t?void 0:t.key.split("-")[0];u.useEffect(function(){var e=r.current;if(e&&C)try{null!=e.getAttribute("tabIndex")||M[e.nodeName]||e.setAttribute("tabIndex","-1"),e.focus()}catch(e){}},[r,n])},exports.useLinkProps=T,exports.useLocation=function(){return n.useSyncExternalStore(R,I)},exports.useNavigationBlocker=function(e,t){u.useEffect(function(){if(e){var r=P.block(function(e){window.confirm(t)&&(r(),e.retry())});return r}},[e])}; | ||
var e=require("history"),t=require("react"),r=require("use-sync-external-store/shim/with-selector"),n=require("use-sync-external-store/shim"),a=require("@emotion/hash");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}function i(e){if(e&&e.__esModule)return e;var t=Object.create(null);return e&&Object.keys(e).forEach(function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}}),t.default=e,t}var u=/*#__PURE__*/i(t),c=/*#__PURE__*/o(a),s=function(e){return e[0]},l=function(e){return e},f=function(e){return""!==e},h=function(e){return e.startsWith(":")},p=function(e,t){return e.length===t.length&&e.every(function(e,r){return e===t[r]})},v=function(e,t){return(null==e?void 0:e.key)===(null==t?void 0:t.key)},d=function(e){return"/"===e[0]?e:"/"+e},y=function(e,t){var r=d(e),n=d(t);return"/"===r?n:"/"===n?r:r+n};function g(){return g=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},g.apply(this,arguments)}function m(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function b(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(r)return(r=r.call(e)).next.bind(r);if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return m(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?m(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0;return function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}}}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 k=function(e){for(var t,r={},n=b(new URLSearchParams(e));!(t=n()).done;){var a=t.value,o=a[0],i=a[1],u=r[o];r[o]=null!=u?"string"==typeof u?[u,i]:u.concat(i):i}return r},S=function(e,t,r){var n=e+(""!==e?"&":"")+encodeURIComponent(t);return""!==r?n+"="+encodeURIComponent(r):n},w=function(e){var t=Object.keys(e);if(0===t.length)return"";var r="";t.sort();for(var n=0,a=t;n<a.length;n++){var o=a[n],i=e[o];if(null!=i)if("string"==typeof i)r=S(r,o,i);else for(var u,c=b(i);!(u=c()).done;)r=S(r,o,u.value)}return""===r?"":"?"+r},O=function(e,t){var r=e.search,n=e.hash,a=e.pathname.substring(1),o=""!==a?t?a.split("/").filter(f).map(decodeURIComponent):a.split("/").map(decodeURIComponent):[],i=""!==r?k(r):{},u=""!==n?decodeURIComponent(n.substring(1)):null,s="/"+o.map(encodeURIComponent).join("/"),l=w(i),h=null!=u?"#"+encodeURIComponent(u):"",p=s+l+h;return g({key:c.default(s)+"-"+c.default(l+h),path:o,search:i},null!==u&&{hash:u},{raw:{path:s,search:l,hash:h},toString:function(){return p}})},x=new Set,P=e.createBrowserHistory(),j=O(P.location,!0),C=!1;j.toString()!==e.createPath(P.location)&&P.replace(j.toString()),P.listen(function(e){var t=O(e.location,!1);if(t.toString()!==j.toString()){C=!0;var r=t.raw.search!==j.raw.search,n=r?{}:j.search;if(r)for(var a in t.search)if(Object.prototype.hasOwnProperty.call(t.search,a)){var o=t.search[a];if(null==o)continue;var i=j.search[a];n[a]=null!=i&&"string"!=typeof i&&"string"!=typeof o&&p(o,i)?i:o}j=g({key:t.key,path:t.raw.path!==j.raw.path?t.path:j.path,search:n},null!=t.hash&&{hash:t.hash},{raw:t.raw,toString:t.toString}),x.forEach(function(e){return e(j)})}});var R=function(e){return x.add(e),function(){x.delete(e)}},I=function(){return j},U=function(t){var r=e.parsePath(t),n=r.pathname,a=r.search,o=r.hash;P.push({pathname:void 0===n?"":n,search:void 0===a?"":a,hash:void 0===o?"":o})},E=function(t){var r=e.parsePath(t),n=r.pathname,a=r.search,o=r.hash;P.replace({pathname:void 0===n?"":n,search:void 0===a?"":a,hash:void 0===o?"":o})},A=function(e){for(var t,r=e.split("/").filter(f),n=[],a=r.length>0?4*r.length:5,o=b(r);!(t=o()).done;){var i=t.value,u=h(i);a+=u?2:3,n.push(u?{name:i.substring(1)}:encodeURIComponent(i))}return{ranking:a,path:n}},N=function(t,r){if(r.endsWith("*")){var n=e.parsePath(r.slice(0,-1)).pathname,a=A(void 0===n?"/":n);return{finite:!1,name:t,ranking:a.ranking-1,path:a.path,search:{},hash:void 0}}for(var o,i=e.parsePath(r),u=i.pathname,c=i.search,s=void 0===c?"":c,l=i.hash,f=void 0===l?"":l,p=A(void 0===u?"/":u),v=p.ranking,d=p.path,y={},g=b(new URLSearchParams(s.substring(1)));!(o=g()).done;){var m=o.value[0];(k=m).startsWith(":")&&k.endsWith("[]")?y[m.substring(1,m.length-2)]="multiple":h(m)&&(y[m.substring(1,m.length)]="unique")}return{finite:!0,name:t,ranking:v,path:d,search:y,hash:h(f.substring(1))?f.substring(2):void 0};var k},L=function(e,t){var r=e.path,n=t.finite,a=t.path;if(!(n&&r.length!==a.length||!n&&r.length<a.length)){for(var o={},i=0;i<a.length;i++){var u=r[i],c=a[i];if(null!=c)if("string"==typeof c){if(c!==u)return}else{if(null==u)return;o[c.name]=u}}if(!n)return o;for(var s in t.search)if(Object.prototype.hasOwnProperty.call(t.search,s)){var l=t.search[s],f=e.search[s];if(null==l||null==f)continue;if("multiple"===l){o[s]="string"==typeof f?[f]:f;continue}"string"==typeof f?o[s]=f:null!=f[0]&&(o[s]=f[0])}return null!=t.hash&&null!=e.hash&&(o[t.hash]=e.hash),o}},q=function(e,t){void 0===t&&(t={});var r="/"+e.path.map(function(e){return encodeURIComponent("string"==typeof e?e:String(t[e.name]))}).join("/"),n="",a="";if(null!=e.search){var o={};for(var i in t){var u=t[i];Object.prototype.hasOwnProperty.call(t,i)&&Object.prototype.hasOwnProperty.call(e.search,i)&&null!=u&&(o[i]=u)}n=w(o)}if(null!=e.hash){var c=t[e.hash];"string"==typeof c&&(a="#"+encodeURIComponent(c))}return{pathname:r,search:n,hash:a}},T=function(t){var r=t.href,a=t.replace,o=void 0!==a&&a,i=t.target,c=u.useMemo(function(){return e.parsePath(r).pathname},[r]),s=n.useSyncExternalStore(R,function(){return c===I().raw.path}),l=o||s,f=!i||"_self"===i;return{active:s,onClick:u.useCallback(function(e){e.defaultPrevented||!f||0!==e.button||e.metaKey||e.altKey||e.ctrlKey||e.shiftKey||(e.preventDefault(),l?E(r):U(r))},[l,f,r])}},W=["onClick","className","replace","style","target","to","activeClassName","activeStyle"],K=u.forwardRef(function(e,t){var r=e.onClick,n=e.className,a=e.replace,o=e.style,i=e.target,c=e.to,s=e.activeClassName,l=e.activeStyle,f=function(e,t){if(null==e)return{};var r,n,a={},o=Object.keys(e);for(n=0;n<o.length;n++)t.indexOf(r=o[n])>=0||(a[r]=e[r]);return a}(e,W),h=T({href:c,replace:a,target:i}),p=h.active,v=h.onClick;return u.createElement("a",g({},f,{ref:t,href:c,onClick:function(e){null==r||r(e),v(e)},target:i,className:p&&null!=s?null==n?s:n+" "+s:n,style:p&&null!=l?null==o?l:g({},o,l):o}))});K.displayName="Link";var M={A:!0,INPUT:!0,SELECT:!0,TEXTAREA:!0};exports.Link=K,exports.createGroup=function(e,t,r){var n={};for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(n[e+a]=y(t,r[a]));return n},exports.createRouter=function(t,n){void 0===n&&(n={});var a=n.basePath,o=void 0===a?"":a,i={},c=[];for(var f in t)if(Object.prototype.hasOwnProperty.call(t,f)){var h=N(f,y(o,t[f]));i[f]=h,c.push(h)}c.sort(function(e,t){return t.ranking-e.ranking});for(var p={},d=0;d<c.length;d++){var m=c[d];null!=m&&m.finite&&function(){var t=m.name;p[t]=function(r){return e.createPath(q(i[t],r))}}()}return g({useRoute:function(e){var t=u.useMemo(function(){return c.filter(function(t){return e.includes(t.name)})},[JSON.stringify(e)]);return r.useSyncExternalStoreWithSelector(R,function(){return function(e,t){for(var r,n=b(t);!(r=n()).done;){var a=r.value,o=L(e,a);if(null!=o)return{key:e.key,name:a.name,params:o}}}(I(),t)},void 0,l,v)},push:function(e){return P.push(q(i[e],s([].slice.call(arguments,1))))},replace:function(e){return P.replace(q(i[e],s([].slice.call(arguments,1))))}},p)},exports.decodeSearch=k,exports.encodeSearch=w,exports.getLocation=I,exports.pushUnsafe=U,exports.replaceUnsafe=E,exports.subscribeToLocation=R,exports.useFocusReset=function(e){var t=e.route,r=e.containerRef,n=null==t?void 0:t.key.split("-")[0];u.useEffect(function(){var e=r.current;if(e&&C)try{null!=e.getAttribute("tabIndex")||M[e.nodeName]||e.setAttribute("tabIndex","-1"),e.focus()}catch(e){}},[r,n])},exports.useLinkProps=T,exports.useLocation=function(){return n.useSyncExternalStore(R,I)},exports.useNavigationBlocker=function(e,t){u.useEffect(function(){if(e){var r=P.block(function(e){window.confirm(t)&&(r(),e.retry())});return r}},[e])}; | ||
//# sourceMappingURL=index.js.map |
import { Path as HistoryLocation } from "history"; | ||
import { MutableLocation } from "./types"; | ||
export declare const decodeLocation: ({ pathname, search, hash }: HistoryLocation, removeExtraSlashes: boolean) => MutableLocation; | ||
import { Location } from "./types"; | ||
export declare const decodeLocation: ({ pathname, search, hash }: HistoryLocation, removeExtraSlashes: boolean) => Location; |
@@ -6,12 +6,11 @@ export declare type Search = Record<string, string | string[]>; | ||
name: string; | ||
segments: { | ||
path: (string | { | ||
name: string; | ||
param: boolean; | ||
}[]; | ||
})[]; | ||
search: Record<string, "unique" | "multiple">; | ||
hash?: string; | ||
hash: string | undefined; | ||
finite: boolean; | ||
ranking: number; | ||
}; | ||
export declare type MutableLocation = { | ||
export declare type Location = Readonly<{ | ||
key: string; | ||
@@ -27,9 +26,8 @@ path: readonly string[]; | ||
toString(): string; | ||
}; | ||
export declare type Location = Readonly<MutableLocation>; | ||
declare type Split<Value extends string, Separator extends string> = Value extends `${infer Head}${Separator}${infer Tail}` ? [Head, ...Split<Tail, Separator>] : Value extends Separator ? [] : [Value]; | ||
declare type ExtractPathParams<Path extends string, Parts = Split<Path, "/">> = Parts extends [infer Head, ...infer Tail] ? Head extends `:${infer Name}` ? { | ||
}>; | ||
declare type SplitAndFilterEmpty<Value extends string, Separator extends string> = Value extends `${infer Head}${Separator}${infer Tail}` ? Head extends "" ? SplitAndFilterEmpty<Tail, Separator> : [Head, ...SplitAndFilterEmpty<Tail, Separator>] : Value extends Separator | "" ? [] : [Value]; | ||
declare type ExtractPathParams<Path extends string, Parts = SplitAndFilterEmpty<Path, "/">> = Parts extends [infer Head, ...infer Tail] ? Head extends `:${infer Name}` ? { | ||
[K in Name]: string; | ||
} & ExtractPathParams<Path, Tail> : ExtractPathParams<Path, Tail> : {}; | ||
declare type ExtractSearchParams<Search extends string, Parts = Split<Search, "&">> = Parts extends [infer Head, ...infer Tail] ? Head extends `:${infer Name}[]` ? { | ||
declare type ExtractSearchParams<Search extends string, Parts = SplitAndFilterEmpty<Search, "&">> = Parts extends [infer Head, ...infer Tail] ? Head extends `:${infer Name}[]` ? { | ||
[K in Name]?: string[]; | ||
@@ -36,0 +34,0 @@ } & ExtractSearchParams<Search, Tail> : Head extends `:${infer Name}` ? { |
{ | ||
"name": "@swan-io/chicane", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"license": "MIT", | ||
@@ -24,3 +24,4 @@ "description": "A simple and safe router for React and TypeScript", | ||
"publishConfig": { | ||
"access": "public" | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org" | ||
}, | ||
@@ -62,3 +63,3 @@ "scripts": { | ||
"@babel/preset-typescript": "^7.16.7", | ||
"@testing-library/jest-dom": "^5.16.3", | ||
"@testing-library/jest-dom": "^5.16.4", | ||
"@testing-library/react": "^12.1.4", | ||
@@ -71,3 +72,3 @@ "@types/jest": "^27.4.1", | ||
"microbundle": "^0.14.2", | ||
"prettier": "2.6.1", | ||
"prettier": "2.6.2", | ||
"prettier-plugin-organize-imports": "^2.3.4", | ||
@@ -74,0 +75,0 @@ "react": "^17.0.2", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
98578
238