@@ -110,7 +110,7 @@ const outputFormats = [
110110 defaultMessage : 'Seconds' ,
111111 } ) ,
112112 shortText : i18n . translate ( 'data.fieldFormats.duration.outputFormats.asSeconds.short' , {
113- defaultMessage : 'sec ' ,
113+ defaultMessage : 's ' ,
114114 } ) ,
115115 method : 'asSeconds' ,
116116 } ,
@@ -128,7 +128,7 @@ const outputFormats = [
128128 defaultMessage : 'Hours' ,
129129 } ) ,
130130 shortText : i18n . translate ( 'data.fieldFormats.duration.outputFormats.asHours.short' , {
131- defaultMessage : 'hr ' ,
131+ defaultMessage : 'h ' ,
132132 } ) ,
133133 method : 'asHours' ,
134134 } ,
@@ -164,7 +164,7 @@ const outputFormats = [
164164 defaultMessage : 'Years' ,
165165 } ) ,
166166 shortText : i18n . translate ( 'data.fieldFormats.duration.outputFormats.asYears.short' , {
167- defaultMessage : 'Yr ' ,
167+ defaultMessage : 'y ' ,
168168 } ) ,
169169 method : 'asYears' ,
170170 } ,
@@ -188,16 +188,23 @@ function formatInputDynamically(
188188 val : number ,
189189 inputFormat : string ,
190190 outputPrecision : number ,
191- showSuffix : boolean ,
192- useShortSuffix : boolean
191+ useShortSuffix : boolean ,
192+ includeSpace : string
193193) {
194194 const ratio = ratioToSeconds [ inputFormat ] || 1 ;
195195 const kind = ( inputFormat in ratioToSeconds
196196 ? 'seconds'
197197 : inputFormat ) as unitOfTime . DurationConstructor ;
198198 const valueInDuration = moment . duration ( val * ratio , kind ) ;
199199
200- return formatDuration ( valueInDuration , outputPrecision , showSuffix , useShortSuffix ) ;
200+ return formatDuration (
201+ val ,
202+ valueInDuration ,
203+ inputFormat ,
204+ outputPrecision ,
205+ useShortSuffix ,
206+ includeSpace
207+ ) ;
201208}
202209
203210export class DurationFormat extends FieldFormat {
@@ -223,6 +230,7 @@ export class DurationFormat extends FieldFormat {
223230 inputFormat : DEFAULT_INPUT_FORMAT . kind ,
224231 outputFormat : DEFAULT_OUTPUT_FORMAT . method ,
225232 outputPrecision : DEFAULT_OUTPUT_PRECISION ,
233+ includeSpaceWithSuffix : true ,
226234 } ;
227235 }
228236
@@ -232,41 +240,46 @@ export class DurationFormat extends FieldFormat {
232240 const outputPrecision = this . param ( 'outputPrecision' ) ;
233241 const showSuffix = Boolean ( this . param ( 'showSuffix' ) ) ;
234242 const useShortSuffix = Boolean ( this . param ( 'useShortSuffix' ) ) ;
243+ const includeSpaceWithSuffix = this . param ( 'includeSpaceWithSuffix' ) ;
244+
245+ // explicitly checking for false
246+ const includeSpace = includeSpaceWithSuffix ? ' ' : '' ;
247+
235248 const human = this . isHuman ( ) ;
236249 const dyanmic = this . isDynamic ( ) ;
250+
237251 const prefix =
238252 val < 0 && human
239253 ? i18n . translate ( 'data.fieldFormats.duration.negativeLabel' , {
240254 defaultMessage : 'minus' ,
241255 } ) + ' '
242256 : '' ;
257+
243258 const duration = parseInputAsDuration ( val , inputFormat ) as Record < keyof Duration , Function > ;
244259 const formatted = dyanmic
245- ? formatInputDynamically ( val , inputFormat , outputPrecision , showSuffix , useShortSuffix )
260+ ? formatInputDynamically ( val , inputFormat , outputPrecision , useShortSuffix , includeSpace )
246261 : duration [ outputFormat ] ( ) ;
247262 const precise = human || dyanmic ? formatted : formatted . toFixed ( outputPrecision ) ;
248263 const type = outputFormats . find ( ( { method } ) => method === outputFormat ) ;
249264
250265 const unitText = useShortSuffix ? type ?. shortText : type ?. text ;
251266
252- const suffix = showSuffix && unitText ? ` ${ unitText } ` : '' ;
267+ const suffix = showSuffix && unitText ? `${ includeSpace } ${ unitText } ` : '' ;
253268
254269 return dyanmic ? precise : prefix + precise + suffix ;
255270 } ;
256271}
257272
258273function formatDuration (
274+ val : number ,
259275 duration : moment . Duration ,
276+ inputFormat : string ,
260277 outputPrecision : number ,
261- showSuffix : boolean ,
262- useShortSuffix : boolean
278+ useShortSuffix : boolean ,
279+ includeSpace : string
263280) {
264- const parts = [ ] ;
265- // const duration = moment.duration(period);
266-
267281 // return nothing when the duration is falsy or not correctly parsed (P0D)
268282 if ( ! duration || duration . toISOString ( ) === 'P0D' ) return ;
269-
270283 const units = [
271284 { unit : duration . years ( ) , nextUnitRate : 12 , method : 'asYears' } ,
272285 { unit : duration . months ( ) , nextUnitRate : 4 , method : 'asMonths' } ,
@@ -278,27 +291,32 @@ function formatDuration(
278291 { unit : duration . milliseconds ( ) , nextUnitRate : 1000 , method : 'asMilliseconds' } ,
279292 ] ;
280293
294+ const getUnitText = ( method : string ) => {
295+ const type = outputFormats . find ( ( { method : methodT } ) => method === methodT ) ;
296+ return useShortSuffix ? type ?. shortText : type ?. text ;
297+ } ;
298+
281299 for ( let i = 0 ; i < units . length ; i ++ ) {
282300 const unitValue = units [ i ] . unit ;
283301 if ( unitValue >= 1 ) {
284- const type = outputFormats . find ( ( { method } ) => method === units [ i ] . method ) ;
285- const unitText = showSuffix ? ( useShortSuffix ? type ?. shortText : type ?. text ) : '' ;
302+ const unitText = getUnitText ( units [ i ] . method ) ;
286303
287304 const value = Math . floor ( unitValue ) ;
288305 if ( units ?. [ i + 1 ] ) {
289306 const decimalPointValue = Math . floor ( units [ i + 1 ] . unit ) ;
290- parts . push (
307+ return (
291308 ( value + decimalPointValue / units [ i ] . nextUnitRate ) . toFixed ( outputPrecision ) +
292- ' ' +
293- unitText
309+ includeSpace +
310+ unitText
294311 ) ;
295- return parts . join ( ', ' ) ;
296312 } else {
297- parts . push ( value + ' ' + unitText ) ;
298- return parts . join ( ', ' ) ;
313+ return unitValue . toFixed ( outputPrecision ) + includeSpace + unitText ;
299314 }
300315 }
301316 }
302317
303- return 0 ;
318+ const unitValue = units [ units . length - 1 ] . unit ;
319+ const unitText = getUnitText ( units [ units . length - 1 ] . method ) ;
320+
321+ return unitValue . toFixed ( outputPrecision ) + includeSpace + unitText ;
304322}
0 commit comments