@@ -2,7 +2,7 @@ type LabelMap<T> = {
2
2
[ label in keyof T ] ?: string ;
3
3
}
4
4
5
- type FieldMappers < T > = {
5
+ type ValueFormatters < T > = {
6
6
[ label in keyof T ] ?: ( value : T [ label ] ) => string | undefined ;
7
7
}
8
8
@@ -13,20 +13,25 @@ function formatLabel<Shape>(label: keyof Shape, map: LabelMap<Shape>) {
13
13
function formatValue < Shape , Key extends keyof Shape > (
14
14
label : Key ,
15
15
value : Shape [ Key ] ,
16
- mappers : FieldMappers < Shape > ,
16
+ formatters : ValueFormatters < Shape > ,
17
17
) {
18
- const mapper = mappers [ label ] ;
19
- const mappedValue = mapper ? mapper ( value ) : value ;
18
+ const formatter = formatters [ label ] ;
19
+ const formattedValue = formatter ? formatter ( value ) : value ;
20
20
21
- return String ( mappedValue ?? '' ) ;
21
+ return String ( formattedValue ?? '' ) ;
22
22
}
23
23
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 > ) {
28
33
return < Key extends keyof Shape > ( label : Key , value : Shape [ Key ] ) => ( {
29
34
label : formatLabel ( label , labelMap || { } ) ,
30
- value : formatValue ( label , value , fieldMappers || { } ) ,
35
+ value : formatValue ( label , value , valueFormatters || { } ) ,
31
36
} ) ;
32
37
}
0 commit comments