@builtwithjavascript/countdown
Advanced tools
+30
-28
| var d = Object.defineProperty; | ||
| var h = (s, t, i) => t in s ? d(s, t, { enumerable: !0, configurable: !0, writable: !0, value: i }) : s[t] = i; | ||
| var r = (s, t, i) => h(s, typeof t != "symbol" ? t + "" : t, i); | ||
| 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 { | ||
@@ -12,6 +12,6 @@ constructor(t) { | ||
| // Modified to return the new object structure | ||
| calculateRemainingTime(t, i) { | ||
| const e = t.getTime() - i.getTime(); | ||
| calculateRemainingTime(t, e) { | ||
| const s = t.getTime() - e.getTime(); | ||
| let n; | ||
| if (e <= 0) | ||
| if (s <= 0) | ||
| n = { | ||
@@ -26,22 +26,23 @@ days: 0, | ||
| else { | ||
| const a = Math.floor(e / 864e5), l = Math.floor(e % (1e3 * 60 * 60 * 24) / (1e3 * 60 * 60)), c = Math.floor(e % (1e3 * 60 * 60) / (1e3 * 60)), u = Math.ceil(e % (1e3 * 60) / 1e3); | ||
| 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: a, | ||
| days: o, | ||
| hours: l, | ||
| minutes: c, | ||
| seconds: u, | ||
| totalMilliseconds: e, | ||
| totalMilliseconds: s, | ||
| isFinished: !1 | ||
| }; | ||
| } | ||
| const o = { | ||
| days: 0, | ||
| // Days are not typically relevant for current time display in HH:MM:SS | ||
| hours: i.getHours(), | ||
| minutes: i.getMinutes(), | ||
| seconds: i.getSeconds() | ||
| const a = { | ||
| year: e.getFullYear(), | ||
| month: e.getMonth(), | ||
| day: e.getDate(), | ||
| hours: e.getHours(), | ||
| minutes: e.getMinutes(), | ||
| seconds: e.getSeconds() | ||
| }; | ||
| return { | ||
| timeRemaining: n, | ||
| currentTime: o | ||
| currentTime: a | ||
| }; | ||
@@ -53,9 +54,9 @@ } | ||
| this.isRunning = !0; | ||
| const i = () => { | ||
| const e = /* @__PURE__ */ new Date(), n = this.calculateRemainingTime(t, e); | ||
| const e = () => { | ||
| const s = /* @__PURE__ */ new Date(), n = this.calculateRemainingTime(t, s); | ||
| this.callback(n), n.timeRemaining.isFinished && this.stop(); | ||
| let a = 1e3 - e.getMilliseconds(); | ||
| a < 10 && (a += 1e3), this.intervalId = window.setTimeout(i, Math.max(1, a)); | ||
| let o = 1e3 - s.getMilliseconds(); | ||
| o < 10 && (o += 1e3), this.intervalId = window.setTimeout(e, Math.max(1, o)); | ||
| }; | ||
| i(); | ||
| e(); | ||
| } | ||
@@ -66,12 +67,13 @@ stop() { | ||
| } | ||
| function g(s) { | ||
| let t = s.seconds, i = s.minutes, e = s.hours, n = s.days; | ||
| if (t === 60 && (t = 0, i++, i === 60 && (i = 0, e++, e === 24 && (e = 0, n++))), s.isFinished) | ||
| 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 o = (a) => a.toString().padStart(2, "0"); | ||
| return `${o(n)}:${o(e)}:${o(i)}:${o(t)}`; | ||
| const a = (o) => o.toString().padStart(2, "0"); | ||
| return `${a(n)}:${a(s)}:${a(e)}:${a(t)}`; | ||
| } | ||
| function y(s) { | ||
| const t = (i) => i.toString().padStart(2, "0"); | ||
| return `${t(s.hours)}:${t(s.minutes)}:${t(s.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}`; | ||
| } | ||
@@ -78,0 +80,0 @@ export { |
@@ -1,1 +0,1 @@ | ||
| (function(e,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(e=typeof globalThis<"u"?globalThis:e||self,n(e.countdown={}))})(this,function(e){"use strict";var g=Object.defineProperty;var y=(e,n,c)=>n in e?g(e,n,{enumerable:!0,configurable:!0,writable:!0,value:c}):e[n]=c;var u=(e,n,c)=>y(e,typeof n!="symbol"?n+"":n,c);class n{constructor(s){u(this,"intervalId");u(this,"callback");u(this,"isRunning",!1);this.callback=s}calculateRemainingTime(s,t){const i=s.getTime()-t.getTime();let a;if(i<=0)a={days:0,hours:0,minutes:0,seconds:0,totalMilliseconds:0,isFinished:!0};else{const l=Math.floor(i/864e5),f=Math.floor(i%(1e3*60*60*24)/(1e3*60*60)),m=Math.floor(i%(1e3*60*60)/(1e3*60)),h=Math.ceil(i%(1e3*60)/1e3);a={days:l,hours:f,minutes:m,seconds:h,totalMilliseconds:i,isFinished:!1}}const r={days:0,hours:t.getHours(),minutes:t.getMinutes(),seconds:t.getSeconds()};return{timeRemaining:a,currentTime:r}}start(s){if(this.isRunning)return;this.isRunning=!0;const t=()=>{const i=new Date,a=this.calculateRemainingTime(s,i);this.callback(a),a.timeRemaining.isFinished&&this.stop();let l=1e3-i.getMilliseconds();l<10&&(l+=1e3),this.intervalId=window.setTimeout(t,Math.max(1,l))};t()}stop(){this.intervalId&&(clearTimeout(this.intervalId),this.intervalId=void 0),this.isRunning=!1}}function c(o){let s=o.seconds,t=o.minutes,i=o.hours,a=o.days;if(s===60&&(s=0,t++,t===60&&(t=0,i++,i===24&&(i=0,a++))),o.isFinished)return"00:00:00:00";const r=l=>l.toString().padStart(2,"0");return`${r(a)}:${r(i)}:${r(t)}:${r(s)}`}function d(o){const s=t=>t.toString().padStart(2,"0");return`${s(o.hours)}:${s(o.minutes)}:${s(o.seconds)}`}e.Countdown=n,e.formatCurrentTime=d,e.formatTimeRemaining=c,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}); | ||
| (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(e){u(this,"intervalId");u(this,"callback");u(this,"isRunning",!1);this.callback=e}calculateRemainingTime(e,t){const n=e.getTime()-t.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:t.getFullYear(),month:t.getMonth(),day:t.getDate(),hours:t.getHours(),minutes:t.getMinutes(),seconds:t.getSeconds()};return{timeRemaining:a,currentTime:r}}start(e){if(this.isRunning)return;this.isRunning=!0;const t=()=>{const n=new Date,a=this.calculateRemainingTime(e,n);this.callback(a),a.timeRemaining.isFinished&&this.stop();let l=1e3-n.getMilliseconds();l<10&&(l+=1e3),this.intervalId=window.setTimeout(t,Math.max(1,l))};t()}stop(){this.intervalId&&(clearTimeout(this.intervalId),this.intervalId=void 0),this.isRunning=!1}}function d(o){let e=o.seconds,t=o.minutes,n=o.hours,a=o.days;if(e===60&&(e=0,t++,t===60&&(t=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(t)}:${r(e)}`}function c(o){const e=a=>a.toString().padStart(2,"0");let t=`${e(o.year)}:${e(o.month)}:${e(o.day)}`,n=`${e(o.hours)}:${e(o.minutes)}:${e(o.seconds)}`;return`${t} ${n}`}i.Countdown=s,i.formatCurrentTime=c,i.formatTimeRemaining=d,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}); |
@@ -1,3 +0,3 @@ | ||
| import { TTimeRemaining, TFormattedTime } from './types'; | ||
| import { TTimeRemaining, TCurrentTime } from './types'; | ||
| export declare function formatTimeRemaining(time: TTimeRemaining): string; | ||
| export declare function formatCurrentTime(time: TFormattedTime): string; | ||
| export declare function formatCurrentTime(time: TCurrentTime): string; |
@@ -1,3 +0,3 @@ | ||
| export type { TTimeRemaining, TRemainingTimeResult, TFormattedTime, TCountdownCallback } from './types'; | ||
| export type { TTimeRemaining, TRemainingTimeResult, TCurrentTime, TCountdownCallback } from './types'; | ||
| export * from './countdown'; | ||
| export * from './formatters'; |
@@ -1,2 +0,10 @@ | ||
| export type TFormattedTime = { | ||
| export type TCurrentTime = { | ||
| year: number; | ||
| month: number; | ||
| day: number; | ||
| hours: number; | ||
| minutes: number; | ||
| seconds: number; | ||
| }; | ||
| export type TTimeRemaining = { | ||
| days: number; | ||
@@ -6,4 +14,2 @@ hours: number; | ||
| seconds: number; | ||
| }; | ||
| export type TTimeRemaining = TFormattedTime & { | ||
| totalMilliseconds: number; | ||
@@ -14,4 +20,4 @@ isFinished: boolean; | ||
| timeRemaining: TTimeRemaining; | ||
| currentTime: TFormattedTime; | ||
| currentTime: TCurrentTime; | ||
| }; | ||
| export type TCountdownCallback = (result: TRemainingTimeResult) => void; |
+1
-1
| { | ||
| "name": "@builtwithjavascript/countdown", | ||
| "version": "1.0.5", | ||
| "version": "1.0.7", | ||
| "description": "Accurate countdown", | ||
@@ -5,0 +5,0 @@ "author": "Damiano Fusco", |
+4
-3
@@ -51,2 +51,3 @@ # `@builtwithjavascript/countdown` | ||
| // countdown.start(targetDate) | ||
| ``` | ||
@@ -70,4 +71,4 @@ ## Project Structure and Code Description | ||
| * **`src/countdown/types.ts`**: This file defines all the TypeScript types used throughout the package. | ||
| * `TFormattedTime`: Represents a standard time structure with `days`, `hours`, `minutes`, and `seconds`. | ||
| * `TTimeRemaining`: Extends `TFormattedTime` by adding `totalMilliseconds` and `isFinished`, specifically for the countdown's remaining time. | ||
| * `TCurrentTime`: Represents a standard time structure with `year`, `month`, `day`, `hours`, `minutes`, and `seconds`. | ||
| * `TTimeRemaining`: Represent the reamining time structure with `day`, `hours`, `minutes`, `seconds`, `totalMilliseconds` and `isFinished`, specifically for the countdown's remaining time. | ||
| * `TRemainingTimeResult`: The primary return type for the countdown callback, containing both `timeRemaining` and `currentTime` objects. | ||
@@ -85,3 +86,3 @@ * `TCountdownCallback`: Defines the signature for the callback function that consumers will provide to receive countdown updates. | ||
| * `formatTimeRemaining(time: TTimeRemaining): string`: Takes a `TTimeRemaining` object and formats it into a `DD:HH:MM:SS` string. It handles carry-overs for seconds, minutes, and hours to ensure correct display. | ||
| * `formatCurrentTime(time: TFormattedTime): string`: Takes a `TFormattedTime` object and formats it into an `HH:MM:SS` string, suitable for displaying the current time. | ||
| * `formatCurrentTime(time: TCurrentTime): string`: Takes a `TCurrentTime` object and formats it into an `yyyy-mm-dd hh:mm:ss` string, suitable for displaying the current time. | ||
@@ -88,0 +89,0 @@ * **`src/countdown/index.ts`**: This file serves as the main entry point for the `countdown` module, re-exporting all necessary types, classes, and functions from `types.ts`, `countdown.ts`, and `formatters.ts`. |
13121
2.55%126
7.69%89
1.14%