@universal-packages/time-measurer 2.0.0 → 2.1.0
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.
- package/Measurement.d.ts +58 -4
- package/Measurement.d.ts.map +1 -1
- package/Measurement.js +103 -11
- package/Measurement.js.map +1 -1
- package/README.md +145 -1
- package/package.json +1 -1
package/Measurement.d.ts
CHANGED
@@ -1,10 +1,64 @@
|
|
1
1
|
import { TimeFormat } from './Measurement.types';
|
2
2
|
export declare class Measurement {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
private _nanoseconds;
|
4
|
+
private _milliseconds;
|
5
|
+
private _seconds;
|
6
|
+
private _minutes;
|
7
|
+
private _hours;
|
8
|
+
get nanoseconds(): bigint;
|
9
|
+
get milliseconds(): number;
|
10
|
+
get seconds(): number;
|
11
|
+
get minutes(): number;
|
12
|
+
get hours(): number;
|
7
13
|
constructor(nanoseconds: bigint);
|
14
|
+
/**
|
15
|
+
* Convert the measurement to a primitive value
|
16
|
+
* @param hint - The hint to convert the measurement to
|
17
|
+
* @returns The measurement as a primitive value
|
18
|
+
*/
|
19
|
+
[Symbol.toPrimitive](hint: 'bigint' | 'number' | 'string' | 'default'): bigint | number | string;
|
20
|
+
/**
|
21
|
+
* Add another measurement to this one
|
22
|
+
* @param other - The measurement to add
|
23
|
+
* @returns A new measurement with the sum of both times
|
24
|
+
*/
|
25
|
+
add(other: Measurement): Measurement;
|
26
|
+
/**
|
27
|
+
* Subtract another measurement from this one
|
28
|
+
* @param other - The measurement to subtract
|
29
|
+
* @returns A new measurement with the difference
|
30
|
+
*/
|
31
|
+
subtract(other: Measurement): Measurement;
|
32
|
+
/**
|
33
|
+
* Check if this measurement is equal to another
|
34
|
+
* @param other - The measurement to compare with
|
35
|
+
* @returns True if measurements are equal
|
36
|
+
*/
|
37
|
+
equals(other: Measurement): boolean;
|
38
|
+
/**
|
39
|
+
* Check if this measurement is less than another
|
40
|
+
* @param other - The measurement to compare with
|
41
|
+
* @returns True if this measurement is less than the other
|
42
|
+
*/
|
43
|
+
lessThan(other: Measurement): boolean;
|
44
|
+
/**
|
45
|
+
* Check if this measurement is greater than another
|
46
|
+
* @param other - The measurement to compare with
|
47
|
+
* @returns True if this measurement is greater than the other
|
48
|
+
*/
|
49
|
+
greaterThan(other: Measurement): boolean;
|
50
|
+
/**
|
51
|
+
* Check if this measurement is less than or equal to another
|
52
|
+
* @param other - The measurement to compare with
|
53
|
+
* @returns True if this measurement is less than or equal to the other
|
54
|
+
*/
|
55
|
+
lessThanOrEqual(other: Measurement): boolean;
|
56
|
+
/**
|
57
|
+
* Check if this measurement is greater than or equal to another
|
58
|
+
* @param other - The measurement to compare with
|
59
|
+
* @returns True if this measurement is greater than or equal to the other
|
60
|
+
*/
|
61
|
+
greaterThanOrEqual(other: Measurement): boolean;
|
8
62
|
/**
|
9
63
|
* Convert the measurement to a string
|
10
64
|
* @param format - The format to convert the measurement to
|
package/Measurement.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Measurement.d.ts","sourceRoot":"","sources":["../src/Measurement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAEhD,qBAAa,WAAW;IACtB,
|
1
|
+
{"version":3,"file":"Measurement.d.ts","sourceRoot":"","sources":["../src/Measurement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAEhD,qBAAa,WAAW;IACtB,OAAO,CAAC,YAAY,CAAQ;IAC5B,OAAO,CAAC,aAAa,CAAQ;IAC7B,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,MAAM,CAAQ;IAEtB,IAAW,WAAW,IAAI,MAAM,CAE/B;IAED,IAAW,YAAY,IAAI,MAAM,CAEhC;IAED,IAAW,OAAO,IAAI,MAAM,CAE3B;IAED,IAAW,OAAO,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liseconds() {
|
11
|
+
return this._milliseconds;
|
12
|
+
}
|
13
|
+
get seconds() {
|
14
|
+
return this._seconds;
|
15
|
+
}
|
16
|
+
get minutes() {
|
17
|
+
return this._minutes;
|
18
|
+
}
|
19
|
+
get hours() {
|
20
|
+
return this._hours;
|
21
|
+
}
|
6
22
|
constructor(nanoseconds) {
|
23
|
+
this._nanoseconds = nanoseconds;
|
7
24
|
let currentNanoseconds = nanoseconds;
|
8
|
-
this.
|
9
|
-
currentNanoseconds = currentNanoseconds - BigInt(Math.floor(this.
|
10
|
-
this.
|
11
|
-
currentNanoseconds = currentNanoseconds - BigInt(Math.floor(this.
|
12
|
-
this.
|
13
|
-
currentNanoseconds = currentNanoseconds - BigInt(Math.floor(this.
|
14
|
-
this.
|
25
|
+
this._hours = Number(currentNanoseconds / 3600000000000n);
|
26
|
+
currentNanoseconds = currentNanoseconds - BigInt(Math.floor(this._hours)) * 3600000000000n;
|
27
|
+
this._minutes = Number(currentNanoseconds / 60000000000n);
|
28
|
+
currentNanoseconds = currentNanoseconds - BigInt(Math.floor(this._minutes)) * 60000000000n;
|
29
|
+
this._seconds = Number(currentNanoseconds / 1000000000n);
|
30
|
+
currentNanoseconds = currentNanoseconds - BigInt(Math.floor(this._seconds)) * 1000000000n;
|
31
|
+
this._milliseconds = Number(currentNanoseconds) / 1000000;
|
32
|
+
}
|
33
|
+
/**
|
34
|
+
* Convert the measurement to a primitive value
|
35
|
+
* @param hint - The hint to convert the measurement to
|
36
|
+
* @returns The measurement as a primitive value
|
37
|
+
*/
|
38
|
+
[Symbol.toPrimitive](hint) {
|
39
|
+
if (hint === 'bigint') {
|
40
|
+
return this.nanoseconds;
|
41
|
+
}
|
42
|
+
else if (hint === 'number' || hint === 'default') {
|
43
|
+
// For numeric operations and comparisons, return nanoseconds as number
|
44
|
+
return Number(this.nanoseconds);
|
45
|
+
}
|
46
|
+
else if (hint === 'string') {
|
47
|
+
return this.toString();
|
48
|
+
}
|
49
|
+
return Number(this.nanoseconds);
|
50
|
+
}
|
51
|
+
/**
|
52
|
+
* Add another measurement to this one
|
53
|
+
* @param other - The measurement to add
|
54
|
+
* @returns A new measurement with the sum of both times
|
55
|
+
*/
|
56
|
+
add(other) {
|
57
|
+
return new Measurement(this.nanoseconds + other.nanoseconds);
|
58
|
+
}
|
59
|
+
/**
|
60
|
+
* Subtract another measurement from this one
|
61
|
+
* @param other - The measurement to subtract
|
62
|
+
* @returns A new measurement with the difference
|
63
|
+
*/
|
64
|
+
subtract(other) {
|
65
|
+
const result = this.nanoseconds - other.nanoseconds;
|
66
|
+
return new Measurement(result < 0n ? 0n : result);
|
67
|
+
}
|
68
|
+
/**
|
69
|
+
* Check if this measurement is equal to another
|
70
|
+
* @param other - The measurement to compare with
|
71
|
+
* @returns True if measurements are equal
|
72
|
+
*/
|
73
|
+
equals(other) {
|
74
|
+
return this.nanoseconds === other.nanoseconds;
|
75
|
+
}
|
76
|
+
/**
|
77
|
+
* Check if this measurement is less than another
|
78
|
+
* @param other - The measurement to compare with
|
79
|
+
* @returns True if this measurement is less than the other
|
80
|
+
*/
|
81
|
+
lessThan(other) {
|
82
|
+
return this.nanoseconds < other.nanoseconds;
|
83
|
+
}
|
84
|
+
/**
|
85
|
+
* Check if this measurement is greater than another
|
86
|
+
* @param other - The measurement to compare with
|
87
|
+
* @returns True if this measurement is greater than the other
|
88
|
+
*/
|
89
|
+
greaterThan(other) {
|
90
|
+
return this.nanoseconds > other.nanoseconds;
|
91
|
+
}
|
92
|
+
/**
|
93
|
+
* Check if this measurement is less than or equal to another
|
94
|
+
* @param other - The measurement to compare with
|
95
|
+
* @returns True if this measurement is less than or equal to the other
|
96
|
+
*/
|
97
|
+
lessThanOrEqual(other) {
|
98
|
+
return this.nanoseconds <= other.nanoseconds;
|
99
|
+
}
|
100
|
+
/**
|
101
|
+
* Check if this measurement is greater than or equal to another
|
102
|
+
* @param other - The measurement to compare with
|
103
|
+
* @returns True if this measurement is greater than or equal to the other
|
104
|
+
*/
|
105
|
+
greaterThanOrEqual(other) {
|
106
|
+
return this.nanoseconds >= other.nanoseconds;
|
15
107
|
}
|
16
108
|
/**
|
17
109
|
* Convert the measurement to a string
|
package/Measurement.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Measurement.js","sourceRoot":"","sources":["../src/Measurement.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,WAAW;
|
1
|
+
{"version":3,"file":"Measurement.js","sourceRoot":"","sources":["../src/Measurement.ts"],"names":[],"mappings":"AAEA,MAAM,OAAO,WAAW;IACd,YAAY,CAAQ;IACpB,aAAa,CAAQ;IACrB,QAAQ,CAAQ;IAChB,QAAQ,CAAQ;IAChB,MAAM,CAAQ;IAEtB,IAAW,WAAW;QACpB,OAAO,IAAI,CAAC,YAAY,CAAA;IAC1B,CAAC;IAED,IAAW,YAAY;QACrB,OAAO,IAAI,CAAC,aAAa,CAAA;IAC3B,CAAC;IAED,IAAW,OAAO;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAA;IACtB,CAAC;IAED,IAAW,OAAO;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAA;IACtB,CAAC;IAED,IAAW,KAAK;QACd,OAAO,IAAI,CAAC,MAAM,CAAA;IACpB,CAAC;IAED,YAAmB,WAAmB;QACpC,IAAI,CAAC,YAAY,GAAG,WAAW,CAAA;QAE/B,IAAI,kBAAkB,GAAG,WAAW,CAAA;QAEpC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,kBAAkB,GAAG,cAAc,CAAC,CAAA;QACzD,kBAAkB,GAAG,kBAAkB,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,cAAc,CAAA;QAE1F,IAAI,CAAC,QAAQ,GAAG,MAAM,CAAC,kBAAkB,GAAG,YAAY,CAAC,CAAA;QACzD,kBAAkB,GAAG,kBAAkB,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CA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ED
@@ -86,7 +86,7 @@ const measurement = measurer.finish()
|
|
86
86
|
|
87
87
|
## Measurement `class`
|
88
88
|
|
89
|
-
The `Measurement` class represents a measured time duration with various formatting options
|
89
|
+
The `Measurement` class represents a measured time duration with various formatting options, convenient accessors for different time units, arithmetic operations, and comparison capabilities.
|
90
90
|
|
91
91
|
```ts
|
92
92
|
import { Measurement, TimeMeasurer } from '@universal-packages/time-measurer'
|
@@ -99,6 +99,17 @@ console.log(measurement.hours) // 0
|
|
99
99
|
console.log(measurement.minutes) // 0
|
100
100
|
console.log(measurement.seconds) // 1
|
101
101
|
console.log(measurement.milliseconds) // 234.567
|
102
|
+
|
103
|
+
// Arithmetic operations
|
104
|
+
const measurement1 = new Measurement(1000000000n) // 1 second
|
105
|
+
const measurement2 = new Measurement(2000000000n) // 2 seconds
|
106
|
+
|
107
|
+
const sum = measurement1.add(measurement2)
|
108
|
+
console.log(sum.toString()) // "3.000sec"
|
109
|
+
|
110
|
+
// Comparison operations
|
111
|
+
console.log(measurement1 < measurement2) // true
|
112
|
+
console.log(measurement1.lessThan(measurement2)) // true
|
102
113
|
```
|
103
114
|
|
104
115
|
### Constructor <small><small>`constructor`</small></small>
|
@@ -115,9 +126,142 @@ Creates a new Measurement instance from nanoseconds. This is typically called in
|
|
115
126
|
- **`minutes`**: `number` - The minutes component of the measurement
|
116
127
|
- **`seconds`**: `number` - The seconds component of the measurement
|
117
128
|
- **`milliseconds`**: `number` - The milliseconds component (including fractional part)
|
129
|
+
- **`nanoseconds`**: `bigint` - The total nanoseconds of the measurement
|
118
130
|
|
119
131
|
### Instance Methods
|
120
132
|
|
133
|
+
#### Arithmetic Operations
|
134
|
+
|
135
|
+
##### add
|
136
|
+
|
137
|
+
```ts
|
138
|
+
add(other: Measurement): Measurement
|
139
|
+
```
|
140
|
+
|
141
|
+
Adds another measurement to this one and returns a new measurement with the sum of both times.
|
142
|
+
|
143
|
+
```ts
|
144
|
+
const measurement1 = new Measurement(1000000000n) // 1 second
|
145
|
+
const measurement2 = new Measurement(500000000n) // 0.5 seconds
|
146
|
+
const sum = measurement1.add(measurement2)
|
147
|
+
console.log(sum.toString()) // "1.500sec"
|
148
|
+
```
|
149
|
+
|
150
|
+
##### subtract
|
151
|
+
|
152
|
+
```ts
|
153
|
+
subtract(other: Measurement): Measurement
|
154
|
+
```
|
155
|
+
|
156
|
+
Subtracts another measurement from this one and returns a new measurement with the difference. If the result would be negative, returns a zero measurement.
|
157
|
+
|
158
|
+
```ts
|
159
|
+
const measurement1 = new Measurement(2000000000n) // 2 seconds
|
160
|
+
const measurement2 = new Measurement(500000000n) // 0.5 seconds
|
161
|
+
const difference = measurement1.subtract(measurement2)
|
162
|
+
console.log(difference.toString()) // "1.500sec"
|
163
|
+
|
164
|
+
// Negative results are clamped to zero
|
165
|
+
const negative = measurement2.subtract(measurement1)
|
166
|
+
console.log(negative.toString()) // "0.00ms"
|
167
|
+
```
|
168
|
+
|
169
|
+
#### Comparison Methods
|
170
|
+
|
171
|
+
##### equals
|
172
|
+
|
173
|
+
```ts
|
174
|
+
equals(other: Measurement): boolean
|
175
|
+
```
|
176
|
+
|
177
|
+
Checks if this measurement is equal to another measurement.
|
178
|
+
|
179
|
+
```ts
|
180
|
+
const measurement1 = new Measurement(1000000000n)
|
181
|
+
const measurement2 = new Measurement(1000000000n)
|
182
|
+
console.log(measurement1.equals(measurement2)) // true
|
183
|
+
```
|
184
|
+
|
185
|
+
##### lessThan
|
186
|
+
|
187
|
+
```ts
|
188
|
+
lessThan(other: Measurement): boolean
|
189
|
+
```
|
190
|
+
|
191
|
+
Checks if this measurement is less than another measurement.
|
192
|
+
|
193
|
+
```ts
|
194
|
+
const measurement1 = new Measurement(500000000n) // 0.5 seconds
|
195
|
+
const measurement2 = new Measurement(1000000000n) // 1 second
|
196
|
+
console.log(measurement1.lessThan(measurement2)) // true
|
197
|
+
```
|
198
|
+
|
199
|
+
##### greaterThan
|
200
|
+
|
201
|
+
```ts
|
202
|
+
greaterThan(other: Measurement): boolean
|
203
|
+
```
|
204
|
+
|
205
|
+
Checks if this measurement is greater than another measurement.
|
206
|
+
|
207
|
+
```ts
|
208
|
+
console.log(measurement2.greaterThan(measurement1)) // true
|
209
|
+
```
|
210
|
+
|
211
|
+
##### lessThanOrEqual
|
212
|
+
|
213
|
+
```ts
|
214
|
+
lessThanOrEqual(other: Measurement): boolean
|
215
|
+
```
|
216
|
+
|
217
|
+
Checks if this measurement is less than or equal to another measurement.
|
218
|
+
|
219
|
+
##### greaterThanOrEqual
|
220
|
+
|
221
|
+
```ts
|
222
|
+
greaterThanOrEqual(other: Measurement): boolean
|
223
|
+
```
|
224
|
+
|
225
|
+
Checks if this measurement is greater than or equal to another measurement.
|
226
|
+
|
227
|
+
#### Operator Overloading
|
228
|
+
|
229
|
+
The `Measurement` class supports JavaScript's native comparison and arithmetic operators through `Symbol.toPrimitive`:
|
230
|
+
|
231
|
+
```ts
|
232
|
+
const measurement1 = new Measurement(1000000000n) // 1 second
|
233
|
+
const measurement2 = new Measurement(2000000000n) // 2 seconds
|
234
|
+
|
235
|
+
// Comparison operators (recommended)
|
236
|
+
console.log(measurement1 < measurement2) // true
|
237
|
+
console.log(measurement1 > measurement2) // false
|
238
|
+
console.log(measurement1 <= measurement2) // true
|
239
|
+
console.log(measurement1 >= measurement2) // false
|
240
|
+
console.log(measurement1 == measurement2) // false
|
241
|
+
console.log(measurement1 != measurement2) // true
|
242
|
+
|
243
|
+
// Arithmetic operators (returns numbers in nanoseconds)
|
244
|
+
console.log(measurement1 + measurement2) // 3000000000 (nanoseconds)
|
245
|
+
console.log(measurement2 - measurement1) // 1000000000 (nanoseconds)
|
246
|
+
|
247
|
+
// For arithmetic operations that return Measurement objects, use methods:
|
248
|
+
const sum = measurement1.add(measurement2) // Returns Measurement
|
249
|
+
const diff = measurement2.subtract(measurement1) // Returns Measurement
|
250
|
+
```
|
251
|
+
|
252
|
+
#### Method Chaining
|
253
|
+
|
254
|
+
All arithmetic methods return new `Measurement` instances, allowing for method chaining:
|
255
|
+
|
256
|
+
```ts
|
257
|
+
const base = new Measurement(1000000000n) // 1 second
|
258
|
+
const half = new Measurement(500000000n) // 0.5 seconds
|
259
|
+
const quarter = new Measurement(250000000n) // 0.25 seconds
|
260
|
+
|
261
|
+
const result = base.add(half).subtract(quarter)
|
262
|
+
console.log(result.toString()) // "1.250sec"
|
263
|
+
```
|
264
|
+
|
121
265
|
#### toString
|
122
266
|
|
123
267
|
```ts
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@universal-packages/time-measurer",
|
3
|
-
"version": "2.
|
3
|
+
"version": "2.1.0",
|
4
4
|
"description": "Utility to measure routines times with precision",
|
5
5
|
"author": "David De Anda <david@universal-packages.com> (https://github.com/universal-packages)",
|
6
6
|
"license": "MIT",
|