UNPKG

@svelte-navigator/history

Version:
3 lines (2 loc) 3.84 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=()=>{},r=e=>"string"==typeof e,a=e=>"number"==typeof e,i=e=>1===e.length?"":e,c=e=>{t(!r(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?i(n(e,a)):"",h=u?n(e,0,a):e,g=c?i(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(!r(n)||!r(s)||!r(o),5),n+s+o},l=e=>r(e)?e:u(e),h=(e,t,n)=>(e.addEventListener(t,n),()=>e.removeEventListener(t,n)),g="POP",b="PUSH",d="REPLACE",f=(e,n,o)=>{t(!r(e),n),t(!s(e,"/"),o)},p=(e,n,s)=>{t(!a(e),n),t(e!==(0|e),s)};function y({go:e,push:t,replace:n}){return(s,o)=>{const{state:r=null,replace:i=!1}=o||{};a(s)?(p(s,0,1),e(s)):(f(s,0,2),(i?n:t)(s,r))}}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(e){let t=e,n=g;const{subscribe:s,notify:r}=function(e=o){const t=new Set;return{subscribe:n=>(t.add(n),n(e()),()=>{t.delete(n)}),notify:()=>t.forEach((t=>t(e())))}}((()=>({location:t,action:n})));return{getLocation:()=>t,getAction:()=>n,set(e,s){t=e,n=s,r()},subscribe:s}}const j=Symbol("historyType");const w=(e,t)=>{const{value:n,key:s}=v(t);return Object.assign({state:n,key:s},(e=>r(e)?c(e):e)(e))};e.HistoryType=j,e.createBrowserHistory=function({window:e}={}){t("undefined"==typeof document,12);const n=e||document.defaultView,{history:s,location:o}=n,r=()=>{const{pathname:e,search:t,hash:n}=o;return Object.assign({pathname:encodeURI(decodeURI(e)),search:t,hash:n},m(s))},{subscribe:a,set:i,getAction:c,getLocation:u}=O(r()),w=h(n,"popstate",(()=>i(r(),g))),H={push(e,t){f(e,6,7);try{s.pushState(v(t),"",e)}catch(t){o.assign(e)}i(r(),b)},replace(e,t){f(e,8,9),s.replaceState(v(t),"",e),i(r(),d)},go(e){p(e,10,11),s.go(e)}};return Object.assign(Object.assign({get location(){return u()},get action(){return c()},subscribe:a,createHref:l,navigate:y(H),release:w},H),{[j]:"browser"})},e.createHashHistory=function({window:e}={}){t("undefined"==typeof document,13);const s=e||document.defaultView,{history:o,location:r}=s,a=()=>n(r.hash,1),i=()=>Object.assign(Object.assign({},c(a())),m(o)),{subscribe:w,set:H,getAction:S,getLocation:E}=O(i()),L=()=>H(i(),g),P=h(s,"popstate",L),R=h(s,"hashchange",(()=>{a()!==u(E())&&L()})),x=e=>`#${l(e)}`,A={push(e,t){f(e,6,7);const n=x(e);try{o.pushState(v(t),"",n)}catch(e){r.assign(n)}H(i(),b)},replace(e,t){f(e,8,9),o.replaceState(v(t),"",x(e)),H(i(),d)},go(e){p(e,10,11),o.go(e)}};return Object.assign(Object.assign({get location(){return E()},get action(){return S()},subscribe:w,createHref:x,navigate:y(A),release(){P(),R()}},A),{[j]:"hash"})},e.createMemoryHistory=function(e){const t=r(e)?[e]:e&&e.initialEntries||["/"];let n=r(e)?0:e&&e.initialIndex||0,s=t.map((e=>w(e,null)));const a=()=>s[n],{subscribe:i,set:c,getAction:u,getLocation:h}=O(a()),m={push(e,t){f(e,6,7),n++,s=s.slice(0,n),s.push(w(e,t)),c(a(),b)},replace(e,t){f(e,8,9),s[n]=w(e,t),c(a(),d)},go(e){p(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:i,createHref:l,navigate:y(m),release:o},m),{[j]:"memory"})},e.createNavigate=y,e.parsePath=c,e.stringifyPath=u,Object.defineProperty(e,"__esModule",{value:!0})})); //# sourceMappingURL=history.umd.min.js.map