-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathitem-value.ts
More file actions
159 lines (145 loc) · 4.32 KB
/
item-value.ts
File metadata and controls
159 lines (145 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { bold } from 'ansis';
import type { IcuMessage } from 'lighthouse';
import type Details from 'lighthouse/types/lhr/audit-details';
import {
formatBytes,
formatDuration,
html,
roundDecimals,
truncateText,
ui,
} from '@code-pushup/utils';
export type PrimitiveItemValue = string | number | boolean;
export type ObjectItemValue = Exclude<
Details.ItemValue,
PrimitiveItemValue | IcuMessage
>;
export type SimpleItemValue =
| Extract<
ObjectItemValue,
Details.NumericValue | Details.CodeValue | Details.UrlValue
>
| PrimitiveItemValue;
export function trimSlice(item?: PrimitiveItemValue, maxLength = 0) {
const str = String(item).trim();
return maxLength > 0 ? str.slice(0, maxLength) : str;
}
export function parseNodeValue(node?: Details.NodeValue): string {
const { selector = '' } = node ?? {};
return selector;
}
// eslint-disable-next-line max-lines-per-function
export function formatTableItemPropertyValue(
itemValue?: Details.ItemValue,
itemValueFormat?: Details.ItemValueType,
) {
// null
if (itemValue == null) {
return '';
}
// Primitive Values
if (itemValueFormat == null) {
if (typeof itemValue === 'string') {
return trimSlice(itemValue);
}
if (typeof itemValue === 'number') {
return Number(itemValue);
}
if (typeof itemValue === 'boolean') {
return itemValue;
}
}
const parsedItemValue = parseTableItemPropertyValue(itemValue);
/* eslint-disable @typescript-eslint/no-magic-numbers */
switch (itemValueFormat) {
case 'bytes':
return formatBytes(Number(parsedItemValue));
case 'code':
return html.code(trimSlice(parsedItemValue as string));
case 'link':
const link = parsedItemValue as Details.LinkValue;
return html.link(link.url, link.text);
case 'url':
const url = parsedItemValue as string;
return html.link(url);
case 'timespanMs':
case 'ms':
return formatDuration(Number(parsedItemValue), 3);
case 'node':
return parseNodeValue(itemValue as Details.NodeValue);
case 'source-location':
return truncateText(String(parsedItemValue), 200);
case 'numeric':
const num = Number(parsedItemValue);
return roundDecimals(num, 3).toString();
case 'text':
return truncateText(String(parsedItemValue), 500);
case 'multi': // @TODO
// @TODO log verbose first, then implement data type
ui().logger.info(`Format type ${bold('multi')} is not implemented`);
return '';
case 'thumbnail': // @TODO
// @TODO log verbose first, then implement data type
ui().logger.info(`Format type ${bold('thumbnail')} is not implemented`);
return '';
}
/* eslint-enable @typescript-eslint/no-magic-numbers */
return itemValue;
}
export function parseSimpleItemValue(
item: SimpleItemValue,
): PrimitiveItemValue {
if (typeof item === 'object') {
const value = item.value;
if (typeof value === 'object') {
return value.formattedDefault;
}
return value;
}
return item;
}
// @TODO extract Link type from logic
export function parseTableItemPropertyValue(
itemValue?: Details.ItemValue,
): PrimitiveItemValue | Details.LinkValue {
if (itemValue == null) {
return '';
}
// Primitive Values
if (
typeof itemValue === 'string' ||
typeof itemValue === 'number' ||
typeof itemValue === 'boolean'
) {
return parseSimpleItemValue(itemValue);
}
// Object Values
const objectValue = itemValue as ObjectItemValue;
const { type } = objectValue;
switch (type) {
case 'code':
case 'url':
return String(parseSimpleItemValue(objectValue));
case 'node':
return parseNodeValue(objectValue);
case 'link':
return objectValue;
case 'numeric':
return Number(parseSimpleItemValue(objectValue));
case 'source-location':
const { url } = objectValue;
return String(url);
case 'subitems':
// @TODO log verbose first, then implement data type
ui().logger.info(`Value type ${bold('subitems')} is not implemented`);
return '';
case 'debugdata':
// @TODO log verbose first, then implement data type
ui().logger.info(`Value type ${bold('debugdata')} is not implemented`, {
silent: true,
});
return '';
}
// IcuMessage
return parseSimpleItemValue(objectValue as SimpleItemValue);
}