-
Notifications
You must be signed in to change notification settings - Fork 1
/
sasstore.min.js
1 lines (1 loc) · 2.25 KB
/
sasstore.min.js
1
!function(){function t(t){function e(t){for(var e=JSON.parse(t),n=Object.keys(a),i=!0,r=0;r<n.length;r++)i=i&&a[n[r]]==e[n[r]];return i}function n(t){for(var e,n,i=[0,10,13,61],r="",o=0;o<t.length;o++)n=t[o],e=(n+42)%256,i.indexOf(e)<0?r+=String.fromCharCode(e):(e=(e+64)%256,r+="="+String.fromCharCode(e));return r}function i(t){var e,n,i=[],r=!1;for(e=0;e<t.length;e++)n=t.charCodeAt(e),13!==n&&10!==n&&(61!==n||r?(r&&(r=!1,n-=64),42>n&&n>0?i.push(n+214):i.push(n-42)):r=!0);return i}function r(t){for(var e=[],i=0;i<t.length;i+=a.chunkSize)i+a.chunkSize>=t.length?e.push(parseInt(t.substring(i,t.length),2)):e.push(parseInt(t.substring(i,i+a.chunkSize),2));var r=t.length%a.chunkSize;return r+n(e)}function o(t){for(var e=t[0],n=t.slice(1),r=i(n),o="",h=0;h<r.length;h++){var g=r[h].toString(2),l=a.chunkSize;for(h==r.length-1&&e>0&&(l=e);g.length<l;)g="0"+g;o+=g}return o}function h(t){for(var e=!0,n=0;n<t.length;n++)e=e&&("0"==t[n]||"1"==t[n]);return e}for(var a={id:Math.random().toString(36).substring(7),chunkSize:7,length:100,itemSize:1},g=Object.keys(t),l=0;l<g.length;l++)a[g[l]]=t[g[l]]||a[g[l]];var u,c=localStorage.getItem(a.id),s=localStorage.getItem(a.id+"-config");c&&s&&e(s)?u=o(c):(u=new Array(a.length*a.itemSize+1).join("0"),localStorage.setItem(a.id,r(u)),localStorage.setItem(a.id+"-config",JSON.stringify(a))),this.getItem=function(t){if(t*a.itemSize+a.itemSize>u.length||0>t)throw new Error("Invalid item location.");return u.substring(t*a.itemSize,t*a.itemSize+a.itemSize)},this.setItem=function(t,e){if(e.length!=a.itemSize)throw new Error("wrong item size");if(t*a.itemSize+a.itemSize>u.length||0>t)throw new Error("position out of range");if(!h(e))throw new Error("not binary data");u=u.slice(0,t*a.itemSize)+e+u.slice((t+1)*a.itemSize),localStorage.setItem(a.id,r(u))},this.randomize=function(){for(var t="",e=0;e<a.length*a.itemSize;e++)t+=Math.round(Math.random()).toString();u=t,localStorage.setItem(a.id,r(u))},this.getAll=function(){for(var t=[],e=0;e<a.length;e++){for(var n="",i=0;i<a.itemSize;i++)n+=u[e*a.itemSize+i];t.push(n)}return t},this.countOccurrencesOf=function(t){for(var e=this.getAll(),n=0,i=0;i<e.length;i++)t==e[i]&&(n+=1);return n},this.reset=function(){u=new Array(a.length*a.itemSize+1).join("0"),localStorage.setItem(a.id,r(u))}}window.SASStore=t}();