@tmlmobilidade/timer 20251202.1817.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Timer class for tracking elapsed time.
3
+ * Provides methods to get formatted time strings and raw time values.
4
+ */
5
+ declare class TimerClass {
6
+ private startTime;
7
+ constructor();
8
+ /**
9
+ * Gets the elapsed time as a formatted string.
10
+ * @param reset - If true, resets the timer after getting the elapsed time
11
+ * @returns Formatted string (e.g., "1h 2m 3s 456ms" or "0ms")
12
+ */
13
+ get(reset?: boolean): string;
14
+ /**
15
+ * Gets the elapsed time in milliseconds.
16
+ * @param reset - If true, resets the timer after getting the elapsed time
17
+ * @returns Elapsed time in milliseconds
18
+ */
19
+ getMs(reset?: boolean): number;
20
+ /**
21
+ * Gets the elapsed time in seconds.
22
+ * @param reset - If true, resets the timer after getting the elapsed time
23
+ * @returns Elapsed time in seconds (with decimal precision)
24
+ */
25
+ getSeconds(reset?: boolean): number;
26
+ /**
27
+ * Resets the timer to the current time.
28
+ */
29
+ reset(): void;
30
+ /**
31
+ * Formats elapsed time in milliseconds to a human-readable string.
32
+ * @param elapsedTime - Time in milliseconds
33
+ * @returns Formatted string (e.g., "1h 2m 3s 456ms" or "0ms")
34
+ */
35
+ private formatElapsedTime;
36
+ }
37
+ /**
38
+ * Singleton Timer instance for tracking time.
39
+ * @example
40
+ * ```ts
41
+ * Timer.reset();
42
+ * // ... do work ...
43
+ * console.log(Timer.get()); // "1s 234ms"
44
+ * ```
45
+ */
46
+ export { TimerClass as Timer };
package/dist/index.js ADDED
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Timer class for tracking elapsed time.
3
+ * Provides methods to get formatted time strings and raw time values.
4
+ */
5
+ class TimerClass {
6
+ startTime;
7
+ constructor() {
8
+ this.startTime = Date.now();
9
+ }
10
+ /**
11
+ * Gets the elapsed time as a formatted string.
12
+ * @param reset - If true, resets the timer after getting the elapsed time
13
+ * @returns Formatted string (e.g., "1h 2m 3s 456ms" or "0ms")
14
+ */
15
+ get(reset = false) {
16
+ const elapsedTime = Date.now() - this.startTime;
17
+ const result = this.formatElapsedTime(elapsedTime);
18
+ if (reset) {
19
+ this.reset();
20
+ }
21
+ return result;
22
+ }
23
+ /**
24
+ * Gets the elapsed time in milliseconds.
25
+ * @param reset - If true, resets the timer after getting the elapsed time
26
+ * @returns Elapsed time in milliseconds
27
+ */
28
+ getMs(reset = false) {
29
+ const elapsedTime = Date.now() - this.startTime;
30
+ if (reset) {
31
+ this.reset();
32
+ }
33
+ return elapsedTime;
34
+ }
35
+ /**
36
+ * Gets the elapsed time in seconds.
37
+ * @param reset - If true, resets the timer after getting the elapsed time
38
+ * @returns Elapsed time in seconds (with decimal precision)
39
+ */
40
+ getSeconds(reset = false) {
41
+ const elapsedTime = Date.now() - this.startTime;
42
+ const seconds = elapsedTime / 1000;
43
+ if (reset) {
44
+ this.reset();
45
+ }
46
+ return seconds;
47
+ }
48
+ /**
49
+ * Resets the timer to the current time.
50
+ */
51
+ reset() {
52
+ this.startTime = Date.now();
53
+ }
54
+ /**
55
+ * Formats elapsed time in milliseconds to a human-readable string.
56
+ * @param elapsedTime - Time in milliseconds
57
+ * @returns Formatted string (e.g., "1h 2m 3s 456ms" or "0ms")
58
+ */
59
+ formatElapsedTime(elapsedTime) {
60
+ // Ensure non-negative time
61
+ const time = Math.max(0, elapsedTime);
62
+ const hours = Math.floor(time / (1000 * 60 * 60));
63
+ const minutes = Math.floor(time / (1000 * 60)) % 60;
64
+ const seconds = Math.floor(time / 1000) % 60;
65
+ const milliseconds = time % 1000;
66
+ const parts = [];
67
+ if (hours > 0)
68
+ parts.push(`${hours}h`);
69
+ if (minutes > 0)
70
+ parts.push(`${minutes}m`);
71
+ if (seconds > 0)
72
+ parts.push(`${seconds}s`);
73
+ if (milliseconds > 0 || parts.length === 0) {
74
+ parts.push(`${milliseconds}ms`);
75
+ }
76
+ return parts.join(' ');
77
+ }
78
+ }
79
+ /**
80
+ * Singleton Timer instance for tracking time.
81
+ * @example
82
+ * ```ts
83
+ * Timer.reset();
84
+ * // ... do work ...
85
+ * console.log(Timer.get()); // "1s 234ms"
86
+ * ```
87
+ */
88
+ export { TimerClass as Timer };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@tmlmobilidade/timer",
3
+ "version": "20251202.1817.5",
4
+ "author": {
5
+ "email": "iso@tmlmobilidade.pt",
6
+ "name": "TML-ISO"
7
+ },
8
+ "license": "AGPL-3.0-or-later",
9
+ "homepage": "https://github.com/tmlmobilidade/go#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/tmlmobilidade/go/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/tmlmobilidade/go.git"
16
+ },
17
+ "keywords": [
18
+ "public transit",
19
+ "tml",
20
+ "transportes metropolitanos de lisboa",
21
+ "go"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "type": "module",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "main": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "scripts": {
33
+ "build": "tsc && resolve-tspaths",
34
+ "lint": "eslint ./src/ && tsc --noEmit",
35
+ "lint:fix": "eslint ./src/ --fix",
36
+ "watch": "tsc-watch --onSuccess 'resolve-tspaths'"
37
+ },
38
+ "devDependencies": {
39
+ "@tmlmobilidade/tsconfig": "*",
40
+ "@types/node": "24.10.1",
41
+ "resolve-tspaths": "0.8.23",
42
+ "tsc-watch": "7.2.0",
43
+ "typescript": "5.9.3"
44
+ }
45
+ }