import { memoizeJ } from "@thi.ng/memoize/memoizej" ; import type { Stringer } from "./api.js" ; import { repeat } from "./repeat.js" ; import { truncate } from "./truncate.js" ; example param lineWidth param pad export const center = memoizeJ < number , string | number | undefined , Stringer < any > > ( ( n , pad = " " ) => { const buf = repeat ( String ( pad ) , n ) ; return ( x ) => { if ( x == null ) return buf ; x = x . toString ( ) ; const r = ( n - x . length ) / 2 ; return x . length < n ? buf . substr ( 0 , r ) + x + buf . substr ( 0 , r + ( ( n & 1 ) === ( x . length & 1 ) ? 0 : 1 ) ) : truncate ( n ) ( x ) ; } ; } ) ;