@builtwithjavascript/countdown
Advanced tools
+66
-79
@@ -1,81 +0,68 @@ | ||
| var d = Object.defineProperty; | ||
| var h = (i, t, e) => t in i ? d(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e; | ||
| var r = (i, t, e) => h(i, typeof t != "symbol" ? t + "" : t, e); | ||
| class m { | ||
| constructor(t) { | ||
| r(this, "intervalId"); | ||
| r(this, "callback"); | ||
| r(this, "isRunning", !1); | ||
| this.callback = t; | ||
| } | ||
| // Modified to return the new object structure | ||
| calculateRemainingTime(t, e) { | ||
| const s = t.getTime() - e.getTime(); | ||
| let n; | ||
| if (s <= 0) | ||
| n = { | ||
| days: 0, | ||
| hours: 0, | ||
| minutes: 0, | ||
| seconds: 0, | ||
| totalMilliseconds: 0, | ||
| isFinished: !0 | ||
| }; | ||
| else { | ||
| const o = Math.floor(s / 864e5), l = Math.floor(s % (1e3 * 60 * 60 * 24) / (1e3 * 60 * 60)), c = Math.floor(s % (1e3 * 60 * 60) / (1e3 * 60)), u = Math.ceil(s % (1e3 * 60) / 1e3); | ||
| n = { | ||
| days: o, | ||
| hours: l, | ||
| minutes: c, | ||
| seconds: u, | ||
| totalMilliseconds: s, | ||
| isFinished: !1 | ||
| }; | ||
| } | ||
| const a = { | ||
| year: e.getFullYear(), | ||
| month: e.getMonth() + 1, | ||
| day: e.getDate(), | ||
| hours: e.getHours(), | ||
| minutes: e.getMinutes(), | ||
| seconds: e.getSeconds(), | ||
| weekDay: e.getDay() | ||
| }; | ||
| return { | ||
| timeRemaining: n, | ||
| currentTime: a | ||
| }; | ||
| } | ||
| start(t) { | ||
| if (this.isRunning) | ||
| return; | ||
| this.isRunning = !0; | ||
| const e = () => { | ||
| const s = /* @__PURE__ */ new Date(), n = this.calculateRemainingTime(t, s); | ||
| this.callback(n), n.timeRemaining.isFinished && this.stop(); | ||
| let o = 1e3 - s.getMilliseconds(); | ||
| o < 10 && (o += 1e3), this.intervalId = window.setTimeout(e, Math.max(1, o)); | ||
| }; | ||
| e(); | ||
| } | ||
| stop() { | ||
| this.intervalId && (clearTimeout(this.intervalId), this.intervalId = void 0), this.isRunning = !1; | ||
| } | ||
| //#region src/countdown/countdown.ts | ||
| var e = class { | ||
| intervalId; | ||
| callback; | ||
| isRunning = !1; | ||
| constructor(e) { | ||
| this.callback = e; | ||
| } | ||
| calculateRemainingTime(e, t) { | ||
| let n = e.getTime() - t.getTime(), r; | ||
| r = n <= 0 ? { | ||
| days: 0, | ||
| hours: 0, | ||
| minutes: 0, | ||
| seconds: 0, | ||
| totalMilliseconds: 0, | ||
| isFinished: !0 | ||
| } : { | ||
| days: Math.floor(n / (1e3 * 60 * 60 * 24)), | ||
| hours: Math.floor(n % (1e3 * 60 * 60 * 24) / (1e3 * 60 * 60)), | ||
| minutes: Math.floor(n % (1e3 * 60 * 60) / (1e3 * 60)), | ||
| seconds: Math.ceil(n % (1e3 * 60) / 1e3), | ||
| totalMilliseconds: n, | ||
| isFinished: !1 | ||
| }; | ||
| let i = { | ||
| year: t.getFullYear(), | ||
| month: t.getMonth() + 1, | ||
| day: t.getDate(), | ||
| hours: t.getHours(), | ||
| minutes: t.getMinutes(), | ||
| seconds: t.getSeconds(), | ||
| weekDay: t.getDay() | ||
| }; | ||
| return { | ||
| timeRemaining: r, | ||
| currentTime: i | ||
| }; | ||
| } | ||
| start(e) { | ||
| if (this.isRunning) return; | ||
| this.isRunning = !0; | ||
| let t = () => { | ||
| let n = /* @__PURE__ */ new Date(), r = this.calculateRemainingTime(e, n); | ||
| this.callback(r), r.timeRemaining.isFinished && this.stop(); | ||
| let i = 1e3 - n.getMilliseconds(); | ||
| i < 10 && (i += 1e3), this.intervalId = window.setTimeout(t, Math.max(1, i)); | ||
| }; | ||
| t(); | ||
| } | ||
| stop() { | ||
| this.intervalId &&= (clearTimeout(this.intervalId), void 0), this.isRunning = !1; | ||
| } | ||
| }; | ||
| //#endregion | ||
| //#region src/countdown/formatters.ts | ||
| function t(e) { | ||
| let t = e.seconds, n = e.minutes, r = e.hours, i = e.days; | ||
| if (t === 60 && (t = 0, n++, n === 60 && (n = 0, r++, r === 24 && (r = 0, i++))), e.isFinished) return "00:00:00:00"; | ||
| let a = (e) => e.toString().padStart(2, "0"); | ||
| return `${a(i)}:${a(r)}:${a(n)}:${a(t)}`; | ||
| } | ||
| function g(i) { | ||
| let t = i.seconds, e = i.minutes, s = i.hours, n = i.days; | ||
| if (t === 60 && (t = 0, e++, e === 60 && (e = 0, s++, s === 24 && (s = 0, n++))), i.isFinished) | ||
| return "00:00:00:00"; | ||
| const a = (o) => o.toString().padStart(2, "0"); | ||
| return `${a(n)}:${a(s)}:${a(e)}:${a(t)}`; | ||
| function n(e) { | ||
| let t = (e) => e.toString().padStart(2, "0"); | ||
| return `${`${t(e.year)}:${t(e.month)}:${t(e.day)}`} ${`${t(e.hours)}:${t(e.minutes)}:${t(e.seconds)}`}`; | ||
| } | ||
| function y(i) { | ||
| const t = (n) => n.toString().padStart(2, "0"); | ||
| let e = `${t(i.year)}:${t(i.month)}:${t(i.day)}`, s = `${t(i.hours)}:${t(i.minutes)}:${t(i.seconds)}`; | ||
| return `${e} ${s}`; | ||
| } | ||
| export { | ||
| m as Countdown, | ||
| y as formatCurrentTime, | ||
| g as formatTimeRemaining | ||
| }; | ||
| //#endregion | ||
| export { e as Countdown, n as formatCurrentTime, t as formatTimeRemaining }; |
@@ -1,1 +0,1 @@ | ||
| (function(i,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(i=typeof globalThis<"u"?globalThis:i||self,s(i.countdown={}))})(this,function(i){"use strict";var g=Object.defineProperty;var y=(i,s,d)=>s in i?g(i,s,{enumerable:!0,configurable:!0,writable:!0,value:d}):i[s]=d;var u=(i,s,d)=>y(i,typeof s!="symbol"?s+"":s,d);class s{constructor(t){u(this,"intervalId");u(this,"callback");u(this,"isRunning",!1);this.callback=t}calculateRemainingTime(t,e){const n=t.getTime()-e.getTime();let a;if(n<=0)a={days:0,hours:0,minutes:0,seconds:0,totalMilliseconds:0,isFinished:!0};else{const l=Math.floor(n/864e5),f=Math.floor(n%(1e3*60*60*24)/(1e3*60*60)),h=Math.floor(n%(1e3*60*60)/(1e3*60)),m=Math.ceil(n%(1e3*60)/1e3);a={days:l,hours:f,minutes:h,seconds:m,totalMilliseconds:n,isFinished:!1}}const r={year:e.getFullYear(),month:e.getMonth()+1,day:e.getDate(),hours:e.getHours(),minutes:e.getMinutes(),seconds:e.getSeconds(),weekDay:e.getDay()};return{timeRemaining:a,currentTime:r}}start(t){if(this.isRunning)return;this.isRunning=!0;const e=()=>{const n=new Date,a=this.calculateRemainingTime(t,n);this.callback(a),a.timeRemaining.isFinished&&this.stop();let l=1e3-n.getMilliseconds();l<10&&(l+=1e3),this.intervalId=window.setTimeout(e,Math.max(1,l))};e()}stop(){this.intervalId&&(clearTimeout(this.intervalId),this.intervalId=void 0),this.isRunning=!1}}function d(o){let t=o.seconds,e=o.minutes,n=o.hours,a=o.days;if(t===60&&(t=0,e++,e===60&&(e=0,n++,n===24&&(n=0,a++))),o.isFinished)return"00:00:00:00";const r=l=>l.toString().padStart(2,"0");return`${r(a)}:${r(n)}:${r(e)}:${r(t)}`}function c(o){const t=a=>a.toString().padStart(2,"0");let e=`${t(o.year)}:${t(o.month)}:${t(o.day)}`,n=`${t(o.hours)}:${t(o.minutes)}:${t(o.seconds)}`;return`${e} ${n}`}i.Countdown=s,i.formatCurrentTime=c,i.formatTimeRemaining=d,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}); | ||
| (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.countdown={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=class{intervalId;callback;isRunning=!1;constructor(e){this.callback=e}calculateRemainingTime(e,t){let n=e.getTime()-t.getTime(),r;r=n<=0?{days:0,hours:0,minutes:0,seconds:0,totalMilliseconds:0,isFinished:!0}:{days:Math.floor(n/(1e3*60*60*24)),hours:Math.floor(n%(1e3*60*60*24)/(1e3*60*60)),minutes:Math.floor(n%(1e3*60*60)/(1e3*60)),seconds:Math.ceil(n%(1e3*60)/1e3),totalMilliseconds:n,isFinished:!1};let i={year:t.getFullYear(),month:t.getMonth()+1,day:t.getDate(),hours:t.getHours(),minutes:t.getMinutes(),seconds:t.getSeconds(),weekDay:t.getDay()};return{timeRemaining:r,currentTime:i}}start(e){if(this.isRunning)return;this.isRunning=!0;let t=()=>{let n=new Date,r=this.calculateRemainingTime(e,n);this.callback(r),r.timeRemaining.isFinished&&this.stop();let i=1e3-n.getMilliseconds();i<10&&(i+=1e3),this.intervalId=window.setTimeout(t,Math.max(1,i))};t()}stop(){this.intervalId&&=(clearTimeout(this.intervalId),void 0),this.isRunning=!1}};function n(e){let t=e.seconds,n=e.minutes,r=e.hours,i=e.days;if(t===60&&(t=0,n++,n===60&&(n=0,r++,r===24&&(r=0,i++))),e.isFinished)return`00:00:00:00`;let a=e=>e.toString().padStart(2,`0`);return`${a(i)}:${a(r)}:${a(n)}:${a(t)}`}function r(e){let t=e=>e.toString().padStart(2,`0`);return`${`${t(e.year)}:${t(e.month)}:${t(e.day)}`} ${`${t(e.hours)}:${t(e.minutes)}:${t(e.seconds)}`}`}e.Countdown=t,e.formatCurrentTime=r,e.formatTimeRemaining=n}); |
+3
-1
@@ -1,1 +0,3 @@ | ||
| export * from './countdown'; | ||
| export type { TCurrentTime, TTimeRemaining, TRemainingTimeResult, TCountdownCallback } from './countdown/types'; | ||
| export { Countdown } from './countdown/countdown'; | ||
| export { formatTimeRemaining, formatCurrentTime } from './countdown/formatters'; |
+12
-17
| { | ||
| "name": "@builtwithjavascript/countdown", | ||
| "version": "1.1.0", | ||
| "version": "1.1.1", | ||
| "description": "Accurate countdown", | ||
@@ -21,5 +21,5 @@ "author": "Damiano Fusco", | ||
| "dev": "vite", | ||
| "build-types": "tsc src/index.ts --esModuleInterop --declaration --emitDeclarationOnly --outDir dist", | ||
| "build": "tsc --esModuleInterop && vite build && npm run build-types", | ||
| "pub": "npm publish --access public", | ||
| "build-types": "tsc --project tsconfig.dts.json", | ||
| "build": "npm run pretty && tsc --esModuleInterop && vite build && npm run build-types", | ||
| "pub": "npm run build; npm publish --access public --otp=", | ||
| "test": "TESTING=true vitest run", | ||
@@ -34,7 +34,8 @@ "test-watch": "TESTING=true vitest watch", | ||
| "devDependencies": { | ||
| "@types/node": "^24.0.3", | ||
| "jsdom": "^26.1.0", | ||
| "prettier": "^3.5.3", | ||
| "typescript": "^5.8.3", | ||
| "vite": "^6.3.5" | ||
| "@types/node": "^25.5.0", | ||
| "jsdom": "^29.0.1", | ||
| "prettier": "^3.8.1", | ||
| "typescript": "^6.0.2", | ||
| "vite": "^8.0.2", | ||
| "vitest": "^4.1.1" | ||
| }, | ||
@@ -47,10 +48,4 @@ "files": [ | ||
| "types": "./dist/index.d.ts", | ||
| "import": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/countdown.es.js" | ||
| }, | ||
| "require": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/countdown.umd.js" | ||
| } | ||
| "import": "./dist/countdown.es.js", | ||
| "require": "./dist/countdown.umd.js" | ||
| }, | ||
@@ -57,0 +52,0 @@ "./package.json": "./package.json" |
+4
-4
@@ -25,6 +25,6 @@ # `@builtwithjavascript/countdown` | ||
| // format the results in a friendly format: | ||
| const fornattedRemainingTime = formatTimeRemaining(results.timeRemaining) | ||
| const furmattedCurrentTime = formatCurrentTime(results.currentTime) | ||
| console.log(` ${furmattedCurrentTime} (Current)`) | ||
| console.log(`${fornattedRemainingTime} (Remaining)`) | ||
| const formattedRemainingTime = formatTimeRemaining(results.timeRemaining) | ||
| const formattedCurrentTime = formatCurrentTime(results.currentTime) | ||
| console.log(`${formattedCurrentTime} (Current)`) | ||
| console.log(`${formattedRemainingTime} (Remaining)`) | ||
@@ -31,0 +31,0 @@ if (results.timeRemaining.isFinished) { |
| export type { TTimeRemaining, TRemainingTimeResult, TCurrentTime, TCountdownCallback } from './types'; | ||
| export * from './countdown'; | ||
| export * from './formatters'; |
12383
-6.15%6
20%9
-10%106
-17.19%