UNPKG

svelte-navigator-history

Version:
3 lines (2 loc) 4 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).SvelteNavigatorHistory={})}(this,(function(e){"use strict";const t=(e,t,n)=>{if(e)throw new Error("<SvelteNavigatorHistory>: An error ocurred. Read more at https://github.com/mefechoel/svelte-navigator-history/blob/main/ERROR_CODES.md#"+t)},n=(e,t,n)=>e.substr(t,n),s=(e,t)=>n(e,0,1)===t,o=()=>{},i=e=>"string"==typeof e,a=e=>"number"==typeof e,r=e=>1===e.length?"":e,c=e=>{t(!i(e),3),t(""!==e&&!s(e,"/")&&!s(e,"?")&&!s(e,"#"),4);const o=e.indexOf("?"),a=e.indexOf("#"),c=-1!==o,u=-1!==a,l=u?r(n(e,a)):"",h=u?n(e,0,a):e,g=c?r(n(h,o)):"";return{pathname:(c?n(h,0,o):h)||"/",search:g,hash:l}},u=e=>{const{pathname:n,search:s,hash:o}=e;return t(!i(n)||!i(s)||!i(o),5),n+s+o},l=e=>i(e)?e:u(e),h=(e,t,n)=>(e.addEventListener(t,n),()=>e.removeEventListener(t,n)),g="POP",b="PUSH",p="REPLACE",y=(e,n,o)=>{t(!i(e),n),t(!s(e,"/"),o)},f=(e,n,s)=>{t(!a(e),n),t(e!==(0|e),s)};function d({go:e,push:t,replace:n}){return(s,o)=>{const{state:i=null,replace:r=!1}=o||{};a(s)?(f(s,0,1),e(s)):(y(s,0,2),(r?n:t)(s,i))}}const m=e=>{const{state:t}=e;return{state:t&&t.value||null,key:t&&t.key||"initial"}},v=e=>({value:void 0===e?null:e,key:n(Math.random().toString(36),2)});function O({initialLocation:e}){let t=e,n=g;const{subscribe:s,notify:i}=function({getState:e=o,onInit:t=o,onDestroy:n=o}={}){const s=new Set;return{subscribe:o=>(0===s.size&&t(),s.add(o),o(e()),()=>{s.delete(o),0===s.size&&n()}),notify:()=>s.forEach((t=>t(e()))),getSize:()=>s.size}}({getState:()=>({location:t,action:n})});return{getLocation:()=>t,getAction:()=>n,set(e,s){t=e,n=s,i()},subscribe:s}}const H="@@historyType";function S({window:e=document.defaultView}={}){const{history:t,location:n}=e,s=()=>{const{pathname:e,search:s,hash:o}=n;return Object.assign({pathname:encodeURI(decodeURI(e)),search:s,hash:o},m(t))},{subscribe:o,set:i,getAction:a,getLocation:r}=O({initialLocation:s()}),c=h(e,"popstate",(()=>i(s(),g))),u={push(e,o){y(e,6,7);try{t.pushState(v(o),"",e)}catch(t){n.assign(e)}i(s(),b)},replace(e,n){y(e,8,9),t.replaceState(v(n),"",e),i(s(),p)},go(e){f(e,10,11),t.go(e)}};return Object.assign(Object.assign({get location(){return r()},get action(){return a()},subscribe:o,createHref:l,navigate:d(u),release:c},u),{[H]:"browser"})}function j({window:e=document.defaultView}={}){const{history:t,location:s}=e,o=()=>n(s.hash,1),i=()=>Object.assign(Object.assign({},c(o())),m(t)),{subscribe:a,set:r,getAction:S,getLocation:j}=O({initialLocation:i()}),w=()=>r(i(),g),L=h(e,"popstate",w),E=h(e,"hashchange",(()=>{o()!==u(j())&&w()})),P=e=>"#"+l(e),R={push(e,n){y(e,6,7);const o=P(e);try{t.pushState(v(n),"",o)}catch(e){s.assign(o)}r(i(),b)},replace(e,n){y(e,8,9),t.replaceState(v(n),"",P(e)),r(i(),p)},go(e){f(e,10,11),t.go(e)}};return Object.assign(Object.assign({get location(){return j()},get action(){return S()},subscribe:a,createHref:P,navigate:d(R),release(){L(),E()}},R),{[H]:"hash"})}const w=(e,t)=>{const{value:n,key:s}=v(t);return Object.assign({state:n,key:s},(e=>i(e)?c(e):e)(e))};function L(e){const t=i(e)?[e]:e&&e.initialEntries||["/"];let n=i(e)?0:e&&e.initialIndex||0,s=t.map((e=>w(e,null)));const a=()=>s[n],{subscribe:r,set:c,getAction:u,getLocation:h}=O({initialLocation:a()}),m={push(e,t){y(e,6,7),n++,s=s.slice(0,n),s.push(w(e,t)),c(a(),b)},replace(e,t){y(e,8,9),s[n]=w(e,t),c(a(),p)},go(e){f(e,10,11);const t=n+e;t<0||t>s.length-1||(n=t,c(a(),g))}};return Object.assign(Object.assign({get location(){return h()},get action(){return u()},subscribe:r,createHref:l,navigate:d(m),release:o},m),{[H]:"memory"})}const E=S(),P=j(),R=L();e.HistoryType=H,e.browserHistory=E,e.createBrowserHistory=S,e.createHashHistory=j,e.createMemoryHistory=L,e.createNavigate=d,e.hashHistory=P,e.memoryHistory=R,e.parsePath=c,e.stringifyPath=u,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=history.umd.min.js.map