time-storage
Version:
Small library to work with the local storage implementing lifetime for its items
1 lines • 9.11 kB
JavaScript
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r=e();for(var n in r)("object"==typeof exports?exports:t)[n]=r[n]}}(window,function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)r.d(n,i,function(e){return t[e]}.bind(null,i));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=1)}([function(t,e,r){t.exports=function(){"use strict";var t="millisecond",e="second",r="minute",n="hour",i="day",s="week",a="month",u="year",o=/^(\d{4})-?(\d{1,2})-?(\d{0,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/,c=/\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,d={name:"en",weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_")},h=function(t,e,r){var n=String(t);return!n||n.length>=e?t:""+Array(e+1-n.length).join(r)+t},f={padStart:h,padZoneStr:function(t){var e=Math.abs(t),r=Math.floor(e/60),n=e%60;return(t<=0?"+":"-")+h(r,2,"0")+":"+h(n,2,"0")},monthDiff:function(t,e){var r=12*(e.year()-t.year())+(e.month()-t.month()),n=t.clone().add(r,"months"),i=e-n<0,s=t.clone().add(r+(i?-1:1),"months");return Number(-(r+(e-n)/(i?n-s:s-n)))},absFloor:function(t){return t<0?Math.ceil(t)||0:Math.floor(t)},prettyUnit:function(o){return{M:a,y:u,w:s,d:i,h:n,m:r,s:e,ms:t}[o]||String(o||"").toLowerCase().replace(/s$/,"")},isUndefined:function(t){return void 0===t}},l="en",$={};$[l]=d;var m=function(t){return t instanceof b},p=function(t,e,r){var n;if(!t)return null;if("string"==typeof t)$[t]&&(n=t),e&&($[t]=e,n=t);else{var i=t.name;$[i]=t,n=i}return r||(l=n),n},y=function(t,e){if(m(t))return t.clone();var r=e||{};return r.date=t,new b(r)},g=function(t,e){return y(t,{locale:e.$L})},S=f;S.parseLocale=p,S.isDayjs=m,S.wrapper=g;var b=function(){function d(t){this.parse(t)}var h=d.prototype;return h.parse=function(t){var e,r;this.$d=null===(e=t.date)?new Date(NaN):S.isUndefined(e)?new Date:e instanceof Date?e:"string"==typeof e&&/.*[^Z]$/i.test(e)&&(r=e.match(o))?new Date(r[1],r[2]-1,r[3]||1,r[5]||0,r[6]||0,r[7]||0,r[8]||0):new Date(e),this.init(t)},h.init=function(t){this.$y=this.$d.getFullYear(),this.$M=this.$d.getMonth(),this.$D=this.$d.getDate(),this.$W=this.$d.getDay(),this.$H=this.$d.getHours(),this.$m=this.$d.getMinutes(),this.$s=this.$d.getSeconds(),this.$ms=this.$d.getMilliseconds(),this.$L=this.$L||p(t.locale,null,!0)||l},h.$utils=function(){return S},h.isValid=function(){return!("Invalid Date"===this.$d.toString())},h.$compare=function(t){return this.valueOf()-y(t).valueOf()},h.isSame=function(t){return 0===this.$compare(t)},h.isBefore=function(t){return this.$compare(t)<0},h.isAfter=function(t){return this.$compare(t)>0},h.year=function(){return this.$y},h.month=function(){return this.$M},h.day=function(){return this.$W},h.date=function(){return this.$D},h.hour=function(){return this.$H},h.minute=function(){return this.$m},h.second=function(){return this.$s},h.millisecond=function(){return this.$ms},h.unix=function(){return Math.floor(this.valueOf()/1e3)},h.valueOf=function(){return this.$d.getTime()},h.startOf=function(t,o){var c=this,d=!!S.isUndefined(o)||o,h=function(t,e){var r=g(new Date(c.$y,e,t),c);return d?r:r.endOf(i)},f=function(t,e){return g(c.toDate()[t].apply(c.toDate(),d?[0,0,0,0].slice(e):[23,59,59,999].slice(e)),c)};switch(S.prettyUnit(t)){case u:return d?h(1,0):h(31,11);case a:return d?h(1,this.$M):h(0,this.$M+1);case s:return h(d?this.$D-this.$W:this.$D+(6-this.$W),this.$M);case i:case"date":return f("setHours",0);case n:return f("setMinutes",1);case r:return f("setSeconds",2);case e:return f("setMilliseconds",3);default:return this.clone()}},h.endOf=function(t){return this.startOf(t,!1)},h.$set=function(i,s){switch(S.prettyUnit(i)){case"date":this.$d.setDate(s);break;case a:this.$d.setMonth(s);break;case u:this.$d.setFullYear(s);break;case n:this.$d.setHours(s);break;case r:this.$d.setMinutes(s);break;case e:this.$d.setSeconds(s);break;case t:this.$d.setMilliseconds(s)}return this.init(),this},h.set=function(t,e){return this.clone().$set(t,e)},h.add=function(t,o){var c=this;t=Number(t);var d,h=S.prettyUnit(o),f=function(e,r){var n=c.set("date",1).set(e,r+t);return n.set("date",Math.min(c.$D,n.daysInMonth()))};if(h===a)return f(a,this.$M);if(h===u)return f(u,this.$y);switch(h){case r:d=6e4;break;case n:d=36e5;break;case i:d=864e5;break;case s:d=6048e5;break;case e:d=1e3;break;default:d=1}var l=this.valueOf()+t*d;return g(l,this)},h.subtract=function(t,e){return this.add(-1*t,e)},h.format=function(t){var e=this,r=t||"YYYY-MM-DDTHH:mm:ssZ",n=S.padZoneStr(this.$d.getTimezoneOffset()),i=this.$locale(),s=i.weekdays,a=i.months,u=function(t,e,r,n){return t&&t[e]||r[e].substr(0,n)};return r.replace(c,function(t){if(t.indexOf("[")>-1)return t.replace(/\[|\]/g,"");switch(t){case"YY":return String(e.$y).slice(-2);case"YYYY":return String(e.$y);case"M":return String(e.$M+1);case"MM":return S.padStart(e.$M+1,2,"0");case"MMM":return u(i.monthsShort,e.$M,a,3);case"MMMM":return a[e.$M];case"D":return String(e.$D);case"DD":return S.padStart(e.$D,2,"0");case"d":return String(e.$W);case"dd":return u(i.weekdaysMin,e.$W,s,2);case"ddd":return u(i.weekdaysShort,e.$W,s,3);case"dddd":return s[e.$W];case"H":return String(e.$H);case"HH":return S.padStart(e.$H,2,"0");case"h":case"hh":return 0===e.$H?12:S.padStart(e.$H<13?e.$H:e.$H-12,"hh"===t?2:1,"0");case"a":return e.$H<12?"am":"pm";case"A":return e.$H<12?"AM":"PM";case"m":return String(e.$m);case"mm":return S.padStart(e.$m,2,"0");case"s":return String(e.$s);case"ss":return S.padStart(e.$s,2,"0");case"SSS":return S.padStart(e.$ms,3,"0");case"Z":return n;default:return n.replace(":","")}})},h.diff=function(t,o,c){var d=S.prettyUnit(o),h=y(t),f=this-h,l=S.monthDiff(this,h);switch(d){case u:l/=12;break;case a:break;case"quarter":l/=3;break;case s:l=f/6048e5;break;case i:l=f/864e5;break;case n:l=f/36e5;break;case r:l=f/6e4;break;case e:l=f/1e3;break;default:l=f}return c?l:S.absFloor(l)},h.daysInMonth=function(){return this.endOf(a).$D},h.$locale=function(){return $[this.$L]},h.locale=function(t,e){var r=this.clone();return r.$L=p(t,e,!0),r},h.clone=function(){return g(this.toDate(),this)},h.toDate=function(){return new Date(this.$d)},h.toArray=function(){return[this.$y,this.$M,this.$D,this.$H,this.$m,this.$s,this.$ms]},h.toJSON=function(){return this.toISOString()},h.toISOString=function(){return this.toDate().toISOString()},h.toObject=function(){return{years:this.$y,months:this.$M,date:this.$D,hours:this.$H,minutes:this.$m,seconds:this.$s,milliseconds:this.$ms}},h.toString=function(){return this.$d.toUTCString()},d}();return y.extend=function(t,e){return t(e,b,y),y},y.locale=p,y.isDayjs=m,y.en=$[l],y}()},function(t,e,r){"use strict";r.r(e);var n=r(0),i=r.n(n);const s=["minute","hour","day"];e.default=function(t,e,r){if(!function(){try{const t="time-storage-test";return window.localStorage.setItem(t,t),window.localStorage.removeItem(t),!0}catch(t){return console.error("Local Storage is not supported, Time Storage can't be initialized"),!1}}())return null;if(!function(t){const e=void 0!==t&&null!==t&&"string"==typeof t&&t.length>0;return e||console.error("You must provide a valid namespace for the time storage"),e}(t))return null;return new class{constructor(t,e=30,r="minute"){this.namespace=t,this.defaultValidity=e,this.defaultTimeUnit=r,this.storage=window.localStorage}get(t,e=null){const r=this.storage.getItem(this.buildKey(t));if(void 0===r||null===r)return e;const n=JSON.parse(r);return i()().isAfter(i()(n.valid_until))?(this.remove(t),e):n.data}getAll(){const t={};return Object.keys(this.storage).filter(t=>t.startsWith(this.namespace)).map(t=>{const e=JSON.parse(this.storage.getItem(t));return e.key=t,e}).filter(t=>i()().isBefore(i()(t.valid_until))).forEach(e=>t[e.key]=e.data),t}set(t,e,r=this.defaultValidity,n=this.defaultTimeUnit){const s={valid_until:i()().add(r,n),data:e};this.storage.setItem(this.buildKey(t),JSON.stringify(s))}remove(t){this.storage.removeItem(this.buildKey(t))}clear(){Object.keys(this.storage).filter(t=>t.startsWith(this.namespace)).map(t=>this.storage.removeItem(t))}buildKey(t){return`${this.namespace}_${t}`}}(t,e,r&&function(t){return s.includes(t)}(r)?r:void 0)}}])});