Skip to content

Commit fc19fcc

Browse files
author
John Doe
committed
refactor: fix lint
1 parent ef3cea0 commit fc19fcc

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

packages/utils/src/lib/clock-epoch.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export type EpochMilliseconds = number;
88
const hasPerf = (): boolean =>
99
typeof performance !== 'undefined' && typeof performance.now === 'function';
1010
const hasTimeOrigin = (): boolean =>
11-
hasPerf() && typeof (performance as any).timeOrigin === 'number';
11+
hasPerf() &&
12+
typeof (performance as { timeOrigin: number | undefined }).timeOrigin ===
13+
'number';
1214

1315
const msToUs = (ms: number): Microseconds => Math.round(ms * 1000);
1416
const usToUs = (us: number): Microseconds => Math.round(us);
@@ -19,10 +21,10 @@ const usToUs = (us: number): Microseconds => Math.round(us);
1921
* Provides process and thread IDs.
2022
* @param init
2123
*/
22-
export interface EpochClockOptions {
24+
export type EpochClockOptions = {
2325
pid?: number;
2426
tid?: number;
25-
}
27+
};
2628
/**
2729
* Creates epoch-based clock utility.
2830
* Epoch time has been the time since January 1, 1970 (UNIX epoch).
@@ -34,20 +36,19 @@ export function epochClock(init: EpochClockOptions = {}) {
3436
const tid = init.tid ?? threadId;
3537

3638
const timeOriginMs = hasTimeOrigin()
37-
? ((performance as any).timeOrigin as number)
39+
? (performance as { timeOrigin: number | undefined }).timeOrigin
3840
: undefined;
3941

4042
const epochNowUs = (): Microseconds => {
4143
if (hasTimeOrigin()) {
42-
return msToUs((performance as any).timeOrigin + performance.now());
44+
return msToUs(performance.timeOrigin + performance.now());
4345
}
4446
return msToUs(Date.now());
4547
};
4648

47-
const fromEpochUs = (epochUs: Microseconds): Microseconds => usToUs(epochUs);
49+
const fromEpochUs = usToUs;
4850

49-
const fromEpochMs = (epochMs: EpochMilliseconds): Microseconds =>
50-
msToUs(epochMs);
51+
const fromEpochMs = msToUs;
5152

5253
const fromPerfMs = (perfMs: Milliseconds): Microseconds => {
5354
if (timeOriginMs === undefined) {
@@ -56,10 +57,8 @@ export function epochClock(init: EpochClockOptions = {}) {
5657
return msToUs(timeOriginMs + perfMs);
5758
};
5859

59-
const fromEntryStartTimeMs = (startTimeMs: Milliseconds): Microseconds =>
60-
fromPerfMs(startTimeMs);
61-
const fromDateNowMs = (dateNowMs: EpochMilliseconds): Microseconds =>
62-
fromEpochMs(dateNowMs);
60+
const fromEntryStartTimeMs = fromPerfMs;
61+
const fromDateNowMs = fromEpochMs;
6362

6463
return {
6564
timeOriginMs,

packages/utils/src/lib/clock-epoch.unit.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterEach, describe, expect, it, vi } from 'vitest';
2-
import { defaultClock, epochClock } from './clock-epoch';
2+
import { defaultClock, epochClock } from './clock-epoch.js';
33

44
describe('epochClock', () => {
55
afterEach(() => {
@@ -37,10 +37,9 @@ describe('epochClock', () => {
3737
});
3838

3939
it('should return undefined if performance.timeOrigin is NOT present', () => {
40-
Object.defineProperty(performance, 'timeOrigin', {
41-
value: undefined,
42-
writable: true,
43-
configurable: true,
40+
vi.stubGlobal('performance', {
41+
...performance,
42+
timeOrigin: undefined,
4443
});
4544
const c = epochClock();
4645
expect(c.hasTimeOrigin()).toBe(false);

0 commit comments

Comments
 (0)