@@ -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>) {
1313function 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