UNPKG

@analytics/session-utils

Version:
3 lines (2 loc) 4.42 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e||self).utilSessions={})}(this,function(e){var n="undefined",t="object";"undefined"==typeof process||process;var r="undefined"!=typeof document;function o(e,n){return n.charAt(0)[e]()+n.slice(1)}"undefined"!=typeof Deno&&Deno,r&&"nodejs"===window.name||"undefined"!=typeof navigator&&void 0!==navigator.userAgent&&(navigator.userAgent.includes("Node.js")||navigator.userAgent.includes("jsdom"));var i=o.bind(null,"toUpperCase"),u=o.bind(null,"toLowerCase");function a(e,n){void 0===n&&(n=!0);var t=function(e){return d(e)?i("null"):"object"==typeof e?function(e){return c(e.constructor)?e.constructor.name:null}(e):Object.prototype.toString.call(e).slice(8,-1)}(e);return n?u(t):t}function s(e,n){return typeof n===e}var c=s.bind(null,"function"),f=s.bind(null,"string");function d(e){return null===e}function l(e,n){if("object"!=typeof n||d(n))return!1;if(n instanceof e)return!0;var t=a(new e(""));if(function(e){return e instanceof Error||f(e.message)&&e.constructor&&function(e){return"number"===a(e)&&!isNaN(e)}(e.constructor.stackTraceLimit)}(n))for(;n;){if(a(n)===t)return!0;n=Object.getPrototypeOf(n)}return!1}function v(e,n){var t=e instanceof Element||e instanceof HTMLDocument;return t&&n?function(e,n){return void 0===n&&(n=""),e&&e.nodeName===n.toUpperCase()}(e,n):t}function p(e){var n=[].slice.call(arguments,1);return function(){return e.apply(void 0,[].slice.call(arguments).concat(n))}}function x(){for(var e="",n=0,t=4294967295*Math.random()|0;n++<36;){var r="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"[n-1],o=15&t;e+="-"==r||"4"==r?r:("x"==r?o:3&o|8).toString(16),t=n%8==0?4294967295*Math.random()|0:t>>4}return e}s.bind(null,"undefined"),s.bind(null,"boolean"),s.bind(null,"symbol"),l.bind(null,TypeError),l.bind(null,SyntaxError),p(v,"form"),p(v,"button"),p(v,"input"),p(v,"select");var g="__global__",b=typeof self===t&&self.self===self&&self||typeof global===t&&global.global===global&&global||void 0;function m(e){return b[g][e]}function y(e,n){return b[g][e]=n}function S(e){delete b[g][e]}function w(e,n,t){var r;try{if(j(e)){var o=window[e];r=o[n].bind(o)}}catch(e){}return r||t}b[g]||(b[g]={});var h={};function j(e){if(typeof h[e]!==n)return h[e];try{var t=window[e];t.setItem(n,n),t.removeItem(n)}catch(n){return h[e]=!1}return h[e]=!0}j.bind(null,"sessionStorage");var D=w("sessionStorage","getItem",m),T=w("sessionStorage","setItem",y);w("sessionStorage","removeItem",S);var A=function(){if(void 0!==A)return A;var e="cookiecookie";try{k(e,e),A=-1!==document.cookie.indexOf(e),O(e)}catch(e){A=!1}return A}(),I=k,N=k;function O(e){return A?k(e,"",-1):S(e)}function k(e,n,t,r,o,i){if("undefined"!=typeof window){var u=arguments.length>1;return!1===A&&(u?y(e,n):m(e)),u?document.cookie=e+"="+encodeURIComponent(n)+(t?"; expires="+new Date(+new Date+1e3*t).toUTCString()+(r?"; path="+r:"")+(o?"; domain="+o:"")+(i?"; secure":""):""):decodeURIComponent((("; "+document.cookie).split("; "+e+"=")[1]||"").split(";")[0])}}var C="__",_="session",E=C+_,U="page",M=["id","createdAt","created"];function L(e){var n=e?new Date(e):new Date;return[n.toISOString(),n.getTime()]}function P(){var e=L(),n=e[0],t=e[1];return{id:x(),created:t,createdAt:n}}var J={session:[D,T],page:[m,y]};function R(e,n){for(var t=J[e],r=t[0],o=t[1],i=P(),u=!1,a={},s=0;s<M.length;s++){var c=M[s],f=C+e+C+_+C+c,d=r(f);u=n||!d,(n||!n&&!d)&&o(f,i[c]);var l=d&&!n?d:i[c],v="created"!==c?l:Number(l);a[c]=v}return H(a,u)}function H(e,n){var t=Date.now();return e.elapsed=t-e.created,e.expires&&(e.remaining=Math.max(e.expires-t,0),e.isExpired=0===e.remaining),e.isNew=n,e}function q(e,n){void 0===e&&(e=30);var t=I(E),r=t?JSON.parse(t):z(e);return n?r:H(r,!t)}function z(e,n,t){void 0===e&&(e=30);var r=t?q(e,!0):P(),o=60*e,i=r.created;if(t){var u=L(),a=u[0],s=u[1];r.modified=s,r.modifiedAt=a,i=s}var c=L(i+1e3*o),f=c[0];return r.expires=c[1],r.expiresAt=f,r.duration=1e3*o,n&&(r=Object.assign(r,n)),N(E,JSON.stringify(r),o),H(r,!t)}var B=R.bind(null,_),F=R.bind(null,_,!0),G=R.bind(null,U),K=R.bind(null,U,!0);e.extendSession=function(e,n){return void 0===e&&(e=30),z(e||1,n,!0)},e.getPageSession=G,e.getSession=q,e.getTabSession=B,e.removeSession=function(){return O(E)},e.sessionData=P,e.setPageSession=K,e.setSession=z,e.setTabSession=F}); //# sourceMappingURL=session-utils.umd.js.map