Skip to content

Commit a9088cd

Browse files
author
John Doe
committed
refactor: wip
1 parent 11fa63e commit a9088cd

File tree

4 files changed

+148
-145
lines changed

4 files changed

+148
-145
lines changed
Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { objectFromEntries } from '@code-pushup/utils';
12
import type {
23
DevToolsColor,
34
DevToolsProperties,
@@ -6,54 +7,43 @@ import type {
67
MarkerPayload,
78
MeasureOptionsWithDevtools,
89
TrackEntryPayload,
10+
WithDevToolsPayload,
911
} from './user-timing-extensibility-api.type.js';
1012

1113
const dataTypeTrackEntry = 'track-entry';
1214
const dataTypeMarker = 'marker';
1315

14-
export function objToPropertiesPayload(
15-
object: Record<string, string | number | boolean | object | undefined>,
16-
): DevToolsProperties {
17-
return Object.entries(object);
18-
}
19-
2016
export function mergePropertiesWithOverwrite(
2117
baseProperties: DevToolsProperties | undefined,
2218
overrideProperties?: DevToolsProperties | undefined,
23-
): DevToolsProperties {
24-
return objToPropertiesPayload({
25-
...Object.fromEntries(
26-
(baseProperties ?? []).map(([key, value]) => [key, String(value)]),
27-
),
28-
...Object.fromEntries(
29-
(overrideProperties ?? []).map(([key, value]) => [key, String(value)]),
30-
),
31-
});
19+
) {
20+
return Object.entries({
21+
...objectFromEntries(baseProperties ?? []),
22+
...objectFromEntries(overrideProperties ?? []),
23+
}) satisfies DevToolsProperties;
3224
}
3325

34-
export function markerPayload(
35-
options?: Omit<MarkerPayload, 'dataType'>,
36-
): MarkerPayload {
26+
export function markerPayload(options?: Omit<MarkerPayload, 'dataType'>) {
3727
return {
3828
dataType: dataTypeMarker,
3929
...options,
40-
};
30+
} satisfies MarkerPayload;
4131
}
4232

4333
export function trackEntryPayload(
4434
options: Omit<TrackEntryPayload, 'dataType'>,
45-
): TrackEntryPayload {
35+
) {
4636
const { track, ...rest } = options;
4737
return {
4838
dataType: dataTypeTrackEntry,
4939
track,
5040
...rest,
51-
};
41+
} satisfies TrackEntryPayload;
5242
}
5343

5444
export function markerErrorPayload<T extends DevToolsColor>(
5545
options?: Omit<MarkerPayload, 'dataType' | 'color'>,
56-
): MarkerPayload {
46+
) {
5747
return {
5848
dataType: dataTypeMarker,
5949
color: 'error' as T,
@@ -69,8 +59,8 @@ export function trackEntryErrorPayload<
6959
track: T;
7060
color?: C;
7161
},
72-
): TrackEntryPayload {
73-
const { track, color = 'error', ...restOptions } = options;
62+
) {
63+
const { track, color = 'error' as const, ...restOptions } = options;
7464
return {
7565
dataType: dataTypeTrackEntry,
7666
color,
@@ -79,13 +69,13 @@ export function trackEntryErrorPayload<
7969
} satisfies TrackEntryPayload;
8070
}
8171

82-
export function errorToDevToolsProperties(e: unknown): DevToolsProperties {
72+
export function errorToDevToolsProperties(e: unknown) {
8373
const name = e instanceof Error ? e.name : 'UnknownError';
8474
const message = e instanceof Error ? e.message : String(e);
8575
return [
86-
['Error Type', name],
87-
['Error Message', message],
88-
];
76+
['Error Type' as const, name],
77+
['Error Message' as const, message],
78+
] satisfies DevToolsProperties;
8979
}
9080

9181
export function errorToEntryMeta(
@@ -94,7 +84,7 @@ export function errorToEntryMeta(
9484
tooltipText?: string;
9585
properties?: DevToolsProperties;
9686
},
97-
): EntryMeta {
87+
) {
9888
const { properties, tooltipText } = options ?? {};
9989
const props = mergePropertiesWithOverwrite(
10090
errorToDevToolsProperties(e),
@@ -103,7 +93,7 @@ export function errorToEntryMeta(
10393
return {
10494
properties: props,
10595
...(tooltipText ? { tooltipText } : {}),
106-
};
96+
} satisfies EntryMeta;
10797
}
10898

10999
export function errorToTrackEntryPayload<T extends string>(
@@ -124,14 +114,14 @@ export function errorToTrackEntryPayload<T extends string>(
124114
} satisfies TrackEntryPayload;
125115
}
126116

127-
export function errorToMarkerPayload<T extends DevToolsColor>(
117+
export function errorToMarkerPayload(
128118
error: unknown,
129119
detail?: Omit<MarkerPayload, 'color' | 'dataType'>,
130-
): MarkerPayload {
120+
) {
131121
const { properties, tooltipText } = detail ?? {};
132122
return {
133123
dataType: dataTypeMarker,
134-
color: 'error' as T,
124+
color: 'error' as const,
135125
...errorToEntryMeta(error, {
136126
properties,
137127
tooltipText,
@@ -140,27 +130,33 @@ export function errorToMarkerPayload<T extends DevToolsColor>(
140130
}
141131

142132
/**
133+
* asOptions wraps a DevTools payload into the `detail` property of User Timing entry options.
143134
*
144135
* @example
145136
* profiler.mark('mark', asOptions({
137+
* dataType: 'marker',
138+
* color: 'error',
139+
* tooltipText: 'This is a marker',
146140
* properties: [
147-
* ['str', 'This is a detail property'],
148-
* ['num', 42],
149-
* ['object', { str: '42', num: 42 }],
150-
* ['array', [42, 42, 42]],
141+
* ['str', 'This is a detail property']
151142
* ],
152143
* }));
153144
*/
154-
export function asOptions(
155-
devtools: MarkerPayload,
156-
): Pick<MarkOptionsWithDevtools, 'detail'>;
157-
export function asOptions(
158-
devtools: TrackEntryPayload,
159-
): Pick<MeasureOptionsWithDevtools, 'detail'>;
160-
export function asOptions(
161-
devtools: MarkerPayload | TrackEntryPayload,
162-
):
163-
| Pick<MarkOptionsWithDevtools, 'detail'>
164-
| Pick<MeasureOptionsWithDevtools, 'detail'> {
145+
export function asOptions<T extends MarkerPayload>(
146+
devtools: T,
147+
): MarkOptionsWithDevtools<T>;
148+
export function asOptions<T extends TrackEntryPayload>(
149+
devtools: T,
150+
): MeasureOptionsWithDevtools<T>;
151+
export function asOptions<T extends MarkerPayload | TrackEntryPayload>(
152+
devtools?: T,
153+
): {
154+
detail?: WithDevToolsPayload<T>;
155+
} {
165156
return devtools ? { detail: { devtools } } : { detail: {} };
166157
}
158+
159+
const o = asOptions({
160+
dataType: 'marker',
161+
color: 'error',
162+
});

packages/utils/src/lib/user-timing-extensibility-api-utils.unit.test.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,10 @@ import {
88
markerErrorPayload,
99
markerPayload,
1010
mergePropertiesWithOverwrite,
11-
objToPropertiesPayload,
1211
trackEntryErrorPayload,
1312
trackEntryPayload,
1413
} from './user-timing-extensibility-api-utils.js';
1514

16-
describe('objToPropertiesPayload', () => {
17-
it('should convert object to properties array', () => {
18-
expect(
19-
objToPropertiesPayload({ key: 'value', number: 42, bool: true }),
20-
).toStrictEqual([
21-
['key', 'value'],
22-
['number', 42],
23-
['bool', true],
24-
]);
25-
});
26-
27-
it('should keep undefined values', () => {
28-
expect(
29-
objToPropertiesPayload({ key: 'value', undef: undefined }),
30-
).toStrictEqual([
31-
['key', 'value'],
32-
['undef', undefined],
33-
]);
34-
});
35-
36-
it('should handle empty object', () => {
37-
expect(objToPropertiesPayload({})).toStrictEqual([]);
38-
});
39-
});
40-
4115
describe('mergePropertiesWithOverwrite', () => {
4216
it('should merge properties with overwrite', () => {
4317
expect(

packages/utils/src/lib/user-timing-extensibility-api.type.test.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)