@fernandomg/streak-counter
Version:
This is a basic streak counter - inspired by Duolingo - written in TypeScript and ment for the browser (uses `localStorage`).
3 lines (2 loc) • 1.01 kB
JavaScript
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])}return t},t.apply(this,arguments)}const n="streak";function e(t){return t.toLocaleDateString("en-US")}function r(n,r){return t({},{currentCount:1,startDate:e(n),lastLoginDate:e(n)},r)}function o(t,e){t.setItem(n,JSON.stringify(e))}function a(t,e){const a=t.getItem(n);if(a)try{const n=JSON.parse(a),c=function(t,n){const e=function(t,n){const e=Math.abs(t.getTime()-n.getTime());return Math.ceil(e/864e5)}(t,new Date(n));return 0===e?"none":1===e?"increment":"reset"}(e,n.lastLoginDate),s="reset"===c;if("increment"===c){const a=r(e,{currentCount:n.currentCount+1,startDate:n.startDate});return o(t,a),a}if(s){const n=r(e);return o(t,n),n}return n}catch(t){console.error("Failed to parse streak from localStorage")}const c=r(e);return o(t,c),c}export{a as streakCounter};
//# sourceMappingURL=index.modern.js.map