@@ -671,8 +671,8 @@ export default class Doc {
671671 * ageSinceFromMs returns a string representation of the duration since the
672672 * specified unix timestamp (milliseconds).
673673 */
674- static ageSinceFromMs ( ms : number ) : string {
675- return Doc . formatDuration ( ( new Date ( ) . getTime ( ) ) - ms )
674+ static ageSinceFromMs ( ms : number , trimSeconds ?: boolean ) : string {
675+ return Doc . formatDuration ( ( new Date ( ) . getTime ( ) ) - ms , trimSeconds )
676676 }
677677
678678 /*
@@ -696,9 +696,9 @@ export default class Doc {
696696 }
697697
698698 /*
699- * hmsSinceFromS returns a time duration since the specified unix timestamp
700- * formatted as YYYY/MM/DD hh:mm.
701- */
699+ * hmsSinceFromS returns a time duration since the specified unix timestamp
700+ * formatted as YYYY/MM/DD hh:mm.
701+ */
702702 static ymdhmSinceFromMS ( ms : number ) : string {
703703 const date = new Date ( ms )
704704 const year = String ( date . getFullYear ( ) )
@@ -710,29 +710,54 @@ export default class Doc {
710710 }
711711
712712 /* formatDuration returns a string representation of the duration */
713- static formatDuration ( dur : number ) : string {
713+ static formatDuration ( dur : number , trimSeconds ?: boolean ) : string {
714714 let seconds = Math . floor ( dur )
715715 let result = ''
716- let count = 0
717- const add = ( n : number , s : string ) => {
718- if ( n > 0 || count > 0 ) count ++
719- if ( n > 0 ) result += `${ n } ${ s } `
720- return count >= 2
716+ // significantChunkCnt counts how many chunks (year, month, day, hour, minute) we've added to the result.
717+ let significantChunkCnt = 0
718+ const add = ( n : number , s : string ) : boolean => {
719+ if ( n === 0 && significantChunkCnt === 0 ) {
720+ // we haven't started building the result, so we aren't done yet
721+ return false
722+ }
723+ significantChunkCnt ++
724+ let chunk = `${ n } ${ s } `
725+ if ( n < 10 ) {
726+ // gotta pad 1-digit number chunk so that it occupies the same amount of space as 2-digit chunk
727+ chunk = ' ' + chunk // use a space that's of the same size as a digit
728+ }
729+ result += chunk
730+ return significantChunkCnt >= 2 // we want to show 2 chunks (year, month, day, hour, minute) at most
721731 }
722- let y , mo , d , h , m , s
723- [ y , seconds ] = timeMod ( seconds , aYear )
724- if ( add ( y , 'y' ) ) { return result }
725- [ mo , seconds ] = timeMod ( seconds , aMonth )
726- if ( add ( mo , 'mo' ) ) { return result }
727- [ d , seconds ] = timeMod ( seconds , aDay )
728- if ( add ( d , 'd' ) ) { return result }
732+
733+ const aYear = 31536000000
734+ const aMonth = 2592000000
735+ const aDay = 86400000
736+ const anHour = 3600000
737+ const aMinute = 60000
738+ const aSecond = 1000
739+
740+ let Y , M , D , h , m , s
741+ [ Y , seconds ] = timeMod ( seconds , aYear )
742+ if ( add ( Y , 'y' ) ) { return result }
743+ [ M , seconds ] = timeMod ( seconds , aMonth )
744+ if ( add ( M , 'm' ) ) { return result }
745+ [ D , seconds ] = timeMod ( seconds , aDay )
746+ if ( add ( D , 'd' ) ) { return result }
729747 [ h , seconds ] = timeMod ( seconds , anHour )
730748 if ( add ( h , 'h' ) ) { return result }
749+ if ( trimSeconds ) {
750+ // show minutes chunk and be done with it
751+ [ m , seconds ] = timeMod ( seconds , aMinute )
752+ add ( m , 'm' )
753+ return result || '0m'
754+ }
755+ // show both minutes and seconds chunks then
731756 [ m , seconds ] = timeMod ( seconds , aMinute )
732757 if ( add ( m , 'm' ) ) { return result }
733- [ s , seconds ] = timeMod ( seconds , 1000 )
758+ [ s , seconds ] = timeMod ( seconds , aSecond )
734759 add ( s , 's' )
735- return result . trimEnd ( ) || '0s'
760+ return result || '0s'
736761 }
737762
738763 // showFormError can be used to set and display error message on forms.
@@ -875,12 +900,6 @@ function sleep (ms: number) {
875900 return new Promise ( resolve => setTimeout ( resolve , ms ) )
876901}
877902
878- const aYear = 31536000000
879- const aMonth = 2592000000
880- const aDay = 86400000
881- const anHour = 3600000
882- const aMinute = 60000
883-
884903/* timeMod returns the quotient and remainder of t / dur. */
885904function timeMod ( t : number , dur : number ) {
886905 const n = Math . floor ( t / dur )
0 commit comments