Skip to content

Commit d11cd22

Browse files
committed
refactor(InfoViewer): change formatter creator signature from params to object
1 parent bd9e5ba commit d11cd22

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/components/IndexInfoViewer/IndexInfoViewer.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ const DISPLAYED_FIELDS: Set<keyof TIndexDescription> = new Set([
1010
]);
1111

1212
const formatItem = createInfoFormatter<TIndexDescription>({
13-
Type: (value) => value?.substring(10), // trims EIndexType prefix
14-
State: (value) => value?.substring(11), // trims EIndexState prefix
15-
KeyColumnNames: (value) => value?.join(', '),
16-
DataColumnNames: (value) => value?.join(', '),
17-
}, {
18-
KeyColumnNames: 'Columns',
19-
DataColumnNames: 'Includes',
13+
values: {
14+
Type: (value) => value?.substring(10), // trims EIndexType prefix
15+
State: (value) => value?.substring(11), // trims EIndexState prefix
16+
KeyColumnNames: (value) => value?.join(', '),
17+
DataColumnNames: (value) => value?.join(', '),
18+
},
19+
labels: {
20+
KeyColumnNames: 'Columns',
21+
DataColumnNames: 'Includes',
22+
},
2023
});
2124

2225
interface IndexInfoViewerProps {

src/components/InfoViewer/utils.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ type LabelMap<T> = {
22
[label in keyof T]?: string;
33
}
44

5-
type FieldMappers<T> = {
5+
type ValueFormatters<T> = {
66
[label in keyof T]?: (value: T[label]) => string | undefined;
77
}
88

@@ -13,20 +13,25 @@ function formatLabel<Shape>(label: keyof Shape, map: LabelMap<Shape>) {
1313
function formatValue<Shape, Key extends keyof Shape>(
1414
label: Key,
1515
value: Shape[Key],
16-
mappers: FieldMappers<Shape>,
16+
formatters: ValueFormatters<Shape>,
1717
) {
18-
const mapper = mappers[label];
19-
const mappedValue = mapper ? mapper(value) : value;
18+
const formatter = formatters[label];
19+
const formattedValue = formatter ? formatter(value) : value;
2020

21-
return String(mappedValue ?? '');
21+
return String(formattedValue ?? '');
2222
}
2323

24-
export function createInfoFormatter<Shape extends Record<string, any>>(
25-
fieldMappers?: FieldMappers<Shape>,
26-
labelMap?: LabelMap<Shape>,
27-
) {
24+
interface CreateInfoFormatterOptions<Shape> {
25+
values?: ValueFormatters<Shape>,
26+
labels?: LabelMap<Shape>,
27+
}
28+
29+
export function createInfoFormatter<Shape extends Record<string, any>>({
30+
values: valueFormatters,
31+
labels: labelMap,
32+
}: CreateInfoFormatterOptions<Shape>) {
2833
return <Key extends keyof Shape>(label: Key, value: Shape[Key]) => ({
2934
label: formatLabel(label, labelMap || {}),
30-
value: formatValue(label, value, fieldMappers || {}),
35+
value: formatValue(label, value, valueFormatters || {}),
3136
});
3237
}

0 commit comments

Comments
 (0)