1414
1515import { Trace } from '../types/trace' ;
1616
17- function getFormatFunctions < T = Trace [ keyof Trace ] > ( ) : Record < string , ( value : T ) => string | T > {
17+ function getFormatFunctions < T = Trace [ keyof Trace ] > ( ) : Record <
18+ string ,
19+ ( value : T , ...args : string [ ] ) => string | T
20+ > {
1821 return {
1922 epoch_micros_to_date_iso : microsSinceEpoch => {
2023 if ( typeof microsSinceEpoch !== 'number' ) {
@@ -26,6 +29,26 @@ function getFormatFunctions<T = Trace[keyof Trace]>(): Record<string, (value: T)
2629
2730 return new Date ( microsSinceEpoch / 1000 ) . toISOString ( ) ;
2831 } ,
32+ pad_start : ( value , desiredLengthString : string , padCharacter : string ) => {
33+ if ( typeof value !== 'string' ) {
34+ console . error ( 'pad_start can only operate on strings, ignoring formatting' , {
35+ value,
36+ desiredLength : desiredLengthString ,
37+ padCharacter,
38+ } ) ;
39+ return value ;
40+ }
41+ const desiredLength = parseInt ( desiredLengthString , 10 ) ;
42+ if ( Number . isNaN ( desiredLength ) ) {
43+ console . error ( 'pad_start needs a desired length as second argument, ignoring formatting' , {
44+ value,
45+ desiredLength : desiredLengthString ,
46+ padCharacter,
47+ } ) ;
48+ }
49+
50+ return value . padStart ( desiredLength , padCharacter ) ;
51+ } ,
2952 } ;
3053}
3154
@@ -35,8 +58,12 @@ export function getParameterAndFormatter<T = Trace[keyof Trace]>(
3558 parameterName : string ;
3659 formatFunction : ( ( value : T ) => T | string ) | null ;
3760} {
38- const [ parameterName , formatFunctionName ] = parameter . split ( '|' ) . map ( part => part . trim ( ) ) ;
39- if ( ! formatFunctionName ) return { parameterName, formatFunction : null } ;
61+ const parts = parameter . split ( '|' ) . map ( part => part . trim ( ) ) ;
62+ const parameterName = parts [ 0 ] ;
63+ if ( parts . length === 1 ) return { parameterName, formatFunction : null } ;
64+
65+ const [ formatFunctionName , ...args ] = parts [ 1 ] . split ( ' ' ) ;
66+
4067 const formatFunctions = getFormatFunctions < T > ( ) ;
4168
4269 const formatFunction = formatFunctions [ formatFunctionName ] ;
@@ -48,5 +75,5 @@ export function getParameterAndFormatter<T = Trace[keyof Trace]>(
4875 } ) ;
4976 }
5077
51- return { parameterName, formatFunction : formatFunction ?? null } ;
78+ return { parameterName, formatFunction : formatFunction ? val => formatFunction ( val , ... args ) : null } ;
5279}
0 commit comments