@universal-packages/time-measurer 1.6.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/Benchmark.d.ts +24 -0
- package/Benchmark.d.ts.map +1 -0
- package/Benchmark.js +82 -0
- package/Benchmark.js.map +1 -0
- package/Benchmark.types.d.ts +28 -0
- package/Benchmark.types.d.ts.map +1 -0
- package/Benchmark.types.js +2 -0
- package/Benchmark.types.js.map +1 -0
- package/Measurement.d.ts +103 -7
- package/Measurement.d.ts.map +1 -0
- package/Measurement.js +148 -14
- package/Measurement.js.map +1 -1
- package/Measurement.types.d.ts +1 -0
- package/Measurement.types.d.ts.map +1 -0
- package/Measurement.types.js +1 -2
- package/README.md +557 -55
- package/Sleep.d.ts +5 -0
- package/Sleep.d.ts.map +1 -0
- package/Sleep.js +8 -0
- package/Sleep.js.map +1 -0
- package/TimeMeasurer.d.ts +15 -10
- package/TimeMeasurer.d.ts.map +1 -0
- package/TimeMeasurer.js +23 -20
- package/TimeMeasurer.js.map +1 -1
- package/TimeProfiler.d.ts +71 -0
- package/TimeProfiler.d.ts.map +1 -0
- package/TimeProfiler.js +171 -0
- package/TimeProfiler.js.map +1 -0
- package/TimeProfiler.types.d.ts +20 -0
- package/TimeProfiler.types.d.ts.map +1 -0
- package/TimeProfiler.types.js +2 -0
- package/TimeProfiler.types.js.map +1 -0
- package/index.d.ts +8 -4
- package/index.d.ts.map +1 -0
- package/index.js +8 -28
- package/index.js.map +1 -1
- package/package.json +22 -23
- package/sleep.d.ts +0 -2
- package/sleep.js +0 -8
- package/sleep.js.map +0 -1
- package/startMeasurement.d.ts +0 -2
- package/startMeasurement.js +0 -13
- package/startMeasurement.js.map +0 -1
package/Benchmark.d.ts
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
import { BenchmarkOptions, BenchmarkResult } from './Benchmark.types';
|
2
|
+
export declare class Benchmark {
|
3
|
+
private options;
|
4
|
+
constructor(options?: BenchmarkOptions);
|
5
|
+
/**
|
6
|
+
* Run the benchmark with the provided function
|
7
|
+
* @param fn - The function to benchmark
|
8
|
+
* @returns BenchmarkResult containing all measurements and statistics
|
9
|
+
*/
|
10
|
+
run(fn: () => void): BenchmarkResult;
|
11
|
+
/**
|
12
|
+
* Run an async function benchmark
|
13
|
+
* @param fn - The async function to benchmark
|
14
|
+
* @returns Promise<BenchmarkResult> containing all measurements and statistics
|
15
|
+
*/
|
16
|
+
runAsync(fn: () => Promise<void>): Promise<BenchmarkResult>;
|
17
|
+
/**
|
18
|
+
* Calculate statistics from measurements
|
19
|
+
* @param measurements - Array of measurements
|
20
|
+
* @returns BenchmarkResult with calculated statistics
|
21
|
+
*/
|
22
|
+
private calculateStatistics;
|
23
|
+
}
|
24
|
+
//# sourceMappingURL=Benchmark.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Benchmark.d.ts","sourceRoot":"","sources":["../src/Benchmark.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAIrE,qBAAa,SAAS;IACpB,OAAO,CAAC,OAAO,CAA4B;gBAE/B,OAAO,CAAC,EAAE,gBAAgB;IAStC;;;;OAIG;IACI,GAAG,CAAC,EAAE,EAAE,MAAM,IAAI,GAAG,eAAe;IAqB3C;;;;OAIG;IACU,QAAQ,CAAC,EAAE,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,eAAe,CAAC;IAqBxE;;;;OAIG;IACH,OAAO,CAAC,mBAAmB;CA0B5B"}
|
package/Benchmark.js
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
import { Measurement } from './Measurement';
|
2
|
+
import { TimeMeasurer } from './TimeMeasurer';
|
3
|
+
export class Benchmark {
|
4
|
+
options;
|
5
|
+
constructor(options) {
|
6
|
+
this.options = {
|
7
|
+
iterations: 1,
|
8
|
+
warmupIterations: 0,
|
9
|
+
name: 'Unnamed Benchmark',
|
10
|
+
...options
|
11
|
+
};
|
12
|
+
}
|
13
|
+
/**
|
14
|
+
* Run the benchmark with the provided function
|
15
|
+
* @param fn - The function to benchmark
|
16
|
+
* @returns BenchmarkResult containing all measurements and statistics
|
17
|
+
*/
|
18
|
+
run(fn) {
|
19
|
+
const measurements = [];
|
20
|
+
// Perform warmup iterations
|
21
|
+
for (let i = 0; i < this.options.warmupIterations; i++) {
|
22
|
+
fn();
|
23
|
+
}
|
24
|
+
// Perform actual benchmark iterations
|
25
|
+
for (let i = 0; i < this.options.iterations; i++) {
|
26
|
+
const measurer = TimeMeasurer.start();
|
27
|
+
fn();
|
28
|
+
const measurement = measurer.finish();
|
29
|
+
measurements.push(measurement);
|
30
|
+
}
|
31
|
+
const result = this.calculateStatistics(measurements);
|
32
|
+
return result;
|
33
|
+
}
|
34
|
+
/**
|
35
|
+
* Run an async function benchmark
|
36
|
+
* @param fn - The async function to benchmark
|
37
|
+
* @returns Promise<BenchmarkResult> containing all measurements and statistics
|
38
|
+
*/
|
39
|
+
async runAsync(fn) {
|
40
|
+
const measurements = [];
|
41
|
+
// Perform warmup iterations
|
42
|
+
for (let i = 0; i < this.options.warmupIterations; i++) {
|
43
|
+
await fn();
|
44
|
+
}
|
45
|
+
// Perform actual benchmark iterations
|
46
|
+
for (let i = 0; i < this.options.iterations; i++) {
|
47
|
+
const measurer = TimeMeasurer.start();
|
48
|
+
await fn();
|
49
|
+
const measurement = measurer.finish();
|
50
|
+
measurements.push(measurement);
|
51
|
+
}
|
52
|
+
const result = this.calculateStatistics(measurements);
|
53
|
+
return result;
|
54
|
+
}
|
55
|
+
/**
|
56
|
+
* Calculate statistics from measurements
|
57
|
+
* @param measurements - Array of measurements
|
58
|
+
* @returns BenchmarkResult with calculated statistics
|
59
|
+
*/
|
60
|
+
calculateStatistics(measurements) {
|
61
|
+
if (measurements.length === 0) {
|
62
|
+
throw new Error('No measurements to calculate statistics from');
|
63
|
+
}
|
64
|
+
// Convert measurements to nanoseconds for calculations
|
65
|
+
const nanoseconds = measurements.map((m) => BigInt(m.hours) * 3600000000000n + BigInt(m.minutes) * 60000000000n + BigInt(m.seconds) * 1000000000n + BigInt(Math.round(m.milliseconds * 1000000)));
|
66
|
+
const minNs = nanoseconds.reduce((min, current) => (current < min ? current : min));
|
67
|
+
const maxNs = nanoseconds.reduce((max, current) => (current > max ? current : max));
|
68
|
+
const totalNs = nanoseconds.reduce((sum, current) => sum + current, 0n);
|
69
|
+
const averageNs = totalNs / BigInt(nanoseconds.length);
|
70
|
+
return {
|
71
|
+
name: this.options.name,
|
72
|
+
iterations: this.options.iterations,
|
73
|
+
warmupIterations: this.options.warmupIterations,
|
74
|
+
measurements,
|
75
|
+
min: new Measurement(minNs),
|
76
|
+
max: new Measurement(maxNs),
|
77
|
+
average: new Measurement(averageNs),
|
78
|
+
total: new Measurement(totalNs)
|
79
|
+
};
|
80
|
+
}
|
81
|
+
}
|
82
|
+
//# sourceMappingURL=Benchmark.js.map
|
package/Benchmark.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Benchmark.js","sourceRoot":"","sources":["../src/Benchmark.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAE7C,MAAM,OAAO,SAAS;IACZ,OAAO,CAA4B;IAE3C,YAAY,OAA0B;QACpC,IAAI,CAAC,OAAO,GAAG;YACb,UAAU,EAAE,CAAC;YACb,gBAAgB,EAAE,CAAC;YACnB,IAAI,EAAE,mBAAmB;YACzB,GAAG,OAAO;SACX,CAAA;IACH,CAAC;IAED;;;;OAIG;IACI,GAAG,CAAC,EAAc;QACvB,MAAM,YAAY,GAAkB,EAAE,CAAA;QAEtC,4BAA4B;QAC5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE,CAAC,EAAE,EAAE,C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GAAG,EAAE,IAAI,WAAW,CAAC,KAAK,CAAC;YAC3B,OAAO,EAAE,IAAI,WAAW,CAAC,SAAS,CAAC;YACnC,KAAK,EAAE,IAAI,WAAW,CAAC,OAAO,CAAC;SAChC,CAAA;IACH,CAAC;CACF"}
|
@@ -0,0 +1,28 @@
|
|
1
|
+
import { Measurement } from './Measurement';
|
2
|
+
export interface BenchmarkOptions {
|
3
|
+
/** Number of iterations to run the benchmark */
|
4
|
+
iterations?: number;
|
5
|
+
/** Number of warmup iterations before actual measurement */
|
6
|
+
warmupIterations?: number;
|
7
|
+
/** Name/description of the benchmark */
|
8
|
+
name?: string;
|
9
|
+
}
|
10
|
+
export interface BenchmarkResult {
|
11
|
+
/** Name of the benchmark */
|
12
|
+
name: string;
|
13
|
+
/** Total number of iterations performed */
|
14
|
+
iterations: number;
|
15
|
+
/** Number of warmup iterations performed */
|
16
|
+
warmupIterations: number;
|
17
|
+
/** All individual measurements */
|
18
|
+
measurements: Measurement[];
|
19
|
+
/** Minimum measurement */
|
20
|
+
min: Measurement;
|
21
|
+
/** Maximum measurement */
|
22
|
+
max: Measurement;
|
23
|
+
/** Average measurement */
|
24
|
+
average: Measurement;
|
25
|
+
/** Total time for all iterations */
|
26
|
+
total: Measurement;
|
27
|
+
}
|
28
|
+
//# sourceMappingURL=Benchmark.types.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Benchmark.types.d.ts","sourceRoot":"","sources":["../src/Benchmark.types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAE3C,MAAM,WAAW,gBAAgB;IAC/B,gDAAgD;IAChD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,4DAA4D;IAC5D,gBAAgB,CAAC,EAAE,MAAM,CAAA;IACzB,wCAAwC;IACxC,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAED,MAAM,WAAW,eAAe;IAC9B,4BAA4B;IAC5B,IAAI,EAAE,MAAM,CAAA;IACZ,2CAA2C;IAC3C,UAAU,EAAE,MAAM,CAAA;IAClB,4CAA4C;IAC5C,gBAAgB,EAAE,MAAM,CAAA;IACxB,kCAAkC;IAClC,YAAY,EAAE,WAAW,EAAE,CAAA;IAC3B,0BAA0B;IAC1B,GAAG,EAAE,WAAW,CAAA;IAChB,0BAA0B;IAC1B,GAAG,EAAE,WAAW,CAAA;IAChB,0BAA0B;IAC1B,OAAO,EAAE,WAAW,CAAA;IACpB,oCAAoC;IACpC,KAAK,EAAE,WAAW,CAAA;CACnB"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Benchmark.types.js","sourceRoot":"","sources":["../src/Benchmark.types.ts"],"names":[],"mappings":""}
|
package/Measurement.d.ts
CHANGED
@@ -1,16 +1,112 @@
|
|
1
1
|
import { TimeFormat } from './Measurement.types';
|
2
|
-
export
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
2
|
+
export declare class Measurement {
|
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);
|
8
|
-
/**
|
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;
|
62
|
+
/**
|
63
|
+
* Convert the measurement to a string
|
64
|
+
* @param format - The format to convert the measurement to
|
65
|
+
* @returns The measurement as a string
|
66
|
+
*/
|
9
67
|
toString(format?: TimeFormat): string;
|
10
|
-
/**
|
68
|
+
/**
|
69
|
+
* Convert the measurement to a date
|
70
|
+
* @returns The measurement as a date
|
71
|
+
*/
|
11
72
|
toDate(): Date;
|
73
|
+
/**
|
74
|
+
* Convert the measurement to a condensed string
|
75
|
+
*
|
76
|
+
* 1000ms = 1.000s
|
77
|
+
* 60s = 1m
|
78
|
+
* 60m = 1h
|
79
|
+
*
|
80
|
+
* @returns The measurement as a condensed string
|
81
|
+
*/
|
12
82
|
private getCondensed;
|
83
|
+
/**
|
84
|
+
* Convert the measurement to a human readable string
|
85
|
+
*
|
86
|
+
* 1000ms = 1.000sec
|
87
|
+
* 60s = 1min
|
88
|
+
* 60m = 1hrs
|
89
|
+
*
|
90
|
+
* @returns The measurement as a human readable string
|
91
|
+
*/
|
13
92
|
private getHuman;
|
93
|
+
/**
|
94
|
+
* Convert the measurement to a expressive string
|
95
|
+
*
|
96
|
+
* 1000ms = 1.000 Second
|
97
|
+
* 60s = 1 Minute
|
98
|
+
* 60m = 1 Hour
|
99
|
+
*
|
100
|
+
* @returns The measurement as a expressive string
|
101
|
+
*/
|
14
102
|
private gerExpressive;
|
103
|
+
/**
|
104
|
+
* Pad a number with zeros
|
105
|
+
* @param num - The number to pad
|
106
|
+
* @param places - The number of places to pad the number to
|
107
|
+
* @param fixPositions - The number of positions to fix the number to
|
108
|
+
* @returns The padded number
|
109
|
+
*/
|
15
110
|
private pad;
|
16
111
|
}
|
112
|
+
//# sourceMappingURL=Measurement.d.ts.map
|
@@ -0,0 +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,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,IAAI,MAAM,CAE3B;IAED,IAAW,KAAK,IAAI,MAAM,CAEzB;gBAEkB,WAAW,EAAE,MAAM;IAiBtC;;;;OAIG;IACH,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,IAAI,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM;IAYhG;;;;OAIG;IACI,GAAG,CAAC,KAAK,EAAE,WAAW,GAAG,WAAW;IAI3C;;;;OAIG;IACI,QAAQ,CAAC,KAAK,EAAE,WAAW,GAAG,WAAW;IAKhD;;;;OAIG;IACI,MAAM,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;IAI1C;;;;OAIG;IACI,QAAQ,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;IAI5C;;;;OAIG;IACI,WAAW,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;IAI/C;;;;OAIG;IACI,eAAe,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;IAInD;;;;OAIG;IACI,kBAAkB,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO;IAItD;;;;OAIG;IACI,QAAQ,CAAC,MAAM,GAAE,UAAoB,GAAG,MAAM;IAWrD;;;OAGG;IACI,MAAM,IAAI,IAAI;IAIrB;;;;;;;;OAQG;IACH,OAAO,CAAC,YAAY;IAYpB;;;;;;;;OAQG;IACH,OAAO,CAAC,QAAQ;IAYhB;;;;;;;;OAQG;IACH,OAAO,CAAC,aAAa;IAYrB;;;;;;OAMG;IACH,OAAO,CAAC,GAAG;CAGZ"}
|
package/Measurement.js
CHANGED
@@ -1,17 +1,115 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
export class Measurement {
|
2
|
+
_nanoseconds;
|
3
|
+
_milliseconds;
|
4
|
+
_seconds;
|
5
|
+
_minutes;
|
6
|
+
_hours;
|
7
|
+
get nanoseconds() {
|
8
|
+
return this._nanoseconds;
|
9
|
+
}
|
10
|
+
get milliseconds() {
|
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
|
+
}
|
4
22
|
constructor(nanoseconds) {
|
23
|
+
this._nanoseconds = nanoseconds;
|
5
24
|
let currentNanoseconds = nanoseconds;
|
6
|
-
this.
|
7
|
-
currentNanoseconds = currentNanoseconds - BigInt(Math.floor(this.
|
8
|
-
this.
|
9
|
-
currentNanoseconds = currentNanoseconds - BigInt(Math.floor(this.
|
10
|
-
this.
|
11
|
-
currentNanoseconds = currentNanoseconds - BigInt(Math.floor(this.
|
12
|
-
this.
|
13
|
-
}
|
14
|
-
/**
|
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;
|
107
|
+
}
|
108
|
+
/**
|
109
|
+
* Convert the measurement to a string
|
110
|
+
* @param format - The format to convert the measurement to
|
111
|
+
* @returns The measurement as a string
|
112
|
+
*/
|
15
113
|
toString(format = 'Human') {
|
16
114
|
switch (format) {
|
17
115
|
case 'Condensed':
|
@@ -22,10 +120,22 @@ class Measurement {
|
|
22
120
|
return this.gerExpressive();
|
23
121
|
}
|
24
122
|
}
|
25
|
-
/**
|
123
|
+
/**
|
124
|
+
* Convert the measurement to a date
|
125
|
+
* @returns The measurement as a date
|
126
|
+
*/
|
26
127
|
toDate() {
|
27
128
|
return new Date(0, 0, 0, this.hours, this.minutes, this.seconds, this.milliseconds);
|
28
129
|
}
|
130
|
+
/**
|
131
|
+
* Convert the measurement to a condensed string
|
132
|
+
*
|
133
|
+
* 1000ms = 1.000s
|
134
|
+
* 60s = 1m
|
135
|
+
* 60m = 1h
|
136
|
+
*
|
137
|
+
* @returns The measurement as a condensed string
|
138
|
+
*/
|
29
139
|
getCondensed() {
|
30
140
|
if (this.hours !== 0) {
|
31
141
|
return `${this.pad(this.hours)}:${this.pad(this.minutes)}:${this.pad(this.seconds)}.${this.pad(this.milliseconds, 3)}`;
|
@@ -40,6 +150,15 @@ class Measurement {
|
|
40
150
|
return `${this.pad(this.milliseconds, 3, 2)}`;
|
41
151
|
}
|
42
152
|
}
|
153
|
+
/**
|
154
|
+
* Convert the measurement to a human readable string
|
155
|
+
*
|
156
|
+
* 1000ms = 1.000sec
|
157
|
+
* 60s = 1min
|
158
|
+
* 60m = 1hrs
|
159
|
+
*
|
160
|
+
* @returns The measurement as a human readable string
|
161
|
+
*/
|
43
162
|
getHuman() {
|
44
163
|
if (this.hours !== 0) {
|
45
164
|
return `${this.hours}hrs ${this.minutes}min ${this.seconds}.${this.pad(this.milliseconds, 3)}sec`;
|
@@ -54,6 +173,15 @@ class Measurement {
|
|
54
173
|
return `${this.pad(this.milliseconds, 3, 2)}ms`;
|
55
174
|
}
|
56
175
|
}
|
176
|
+
/**
|
177
|
+
* Convert the measurement to a expressive string
|
178
|
+
*
|
179
|
+
* 1000ms = 1.000 Second
|
180
|
+
* 60s = 1 Minute
|
181
|
+
* 60m = 1 Hour
|
182
|
+
*
|
183
|
+
* @returns The measurement as a expressive string
|
184
|
+
*/
|
57
185
|
gerExpressive() {
|
58
186
|
if (this.hours !== 0) {
|
59
187
|
return `${this.hours} Hours, ${this.minutes} Minutes, and ${this.seconds}.${this.pad(this.milliseconds, 3)} Seconds`;
|
@@ -68,9 +196,15 @@ class Measurement {
|
|
68
196
|
return `${this.pad(this.milliseconds, 3, 2)} Milliseconds`;
|
69
197
|
}
|
70
198
|
}
|
199
|
+
/**
|
200
|
+
* Pad a number with zeros
|
201
|
+
* @param num - The number to pad
|
202
|
+
* @param places - The number of places to pad the number to
|
203
|
+
* @param fixPositions - The number of positions to fix the number to
|
204
|
+
* @returns The padded number
|
205
|
+
*/
|
71
206
|
pad(num, places = 2, fixPositions = 0) {
|
72
207
|
return num.toFixed(fixPositions).padStart(places, '0');
|
73
208
|
}
|
74
209
|
}
|
75
|
-
exports.default = Measurement;
|
76
210
|
//# sourceMappingURL=Measurement.js.map
|
package/Measurement.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"Measurement.js","sourceRoot":"","sources":["../src/Measurement.ts"],"names":[],"mappings":"
|
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,CA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C9B,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,CAAC,UAAU,CAAA;QACpE,CAAC;aAAM,CAAC;YACN,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC,EAAE,CAAC,CAAC,eAAe,CAAA;QAC5D,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACK,GAAG,CAAC,GAAW,EAAE,MAAM,GAAG,CAAC,EAAE,YAAY,GAAG,CAAC;QACnD,OAAO,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACxD,CAAC;CACF"}
|
package/Measurement.types.d.ts
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Measurement.types.d.ts","sourceRoot":"","sources":["../src/Measurement.types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GAAG,YAAY,GAAG,OAAO,GAAG,WAAW,CAAA"}
|
package/Measurement.types.js
CHANGED