@@ -7,63 +7,77 @@ const durationFormatter = new Intl.RelativeTimeFormat('en', {
77 numeric : 'auto' ,
88} ) ;
99
10- // All these are approximate, specifically months and years
10+ // Months and years are approximate
1111const MINUTE_IN_MILLIS = 1000 * 60 ;
1212const HOUR_IN_MILLIS = 60 * MINUTE_IN_MILLIS ;
1313const DAY_IN_MILLIS = 24 * HOUR_IN_MILLIS ;
1414const MONTH_IN_MILLIS = 30 * DAY_IN_MILLIS ;
1515const YEAR_IN_MILLIS = 365 * DAY_IN_MILLIS ;
1616
17- export function humanizeDuration ( diffInMs ?: number ) {
18- if ( diffInMs === undefined ) {
17+ /**
18+ * Converts a number of milliseconds into a human-readable string with units, indicating a relative time in the past or future.
19+ *
20+ * @param relativeTimeMillis The duration in milliseconds. A negative number indicates a duration in the past. And a positive number is
21+ * the future.
22+ * @returns A humanized duration. For example, "in 2 minutes", "2 minutes ago", "yesterday", or "tomorrow".
23+ */
24+ export function humanizeRelativeTime ( relativeTimeMillis ?: number ) {
25+ if ( relativeTimeMillis === undefined ) {
1926 return '' ;
2027 }
2128
22- if ( Math . abs ( diffInMs ) < HOUR_IN_MILLIS ) {
23- return durationFormatter . format ( Math . floor ( diffInMs / MINUTE_IN_MILLIS ) , 'minute' ) ;
24- } else if ( Math . abs ( diffInMs ) < DAY_IN_MILLIS ) {
25- return durationFormatter . format ( Math . floor ( diffInMs / HOUR_IN_MILLIS ) , 'hour' ) ;
26- } else if ( Math . abs ( diffInMs ) < MONTH_IN_MILLIS ) {
27- return durationFormatter . format ( Math . floor ( diffInMs / DAY_IN_MILLIS ) , 'day' ) ;
28- } else if ( Math . abs ( diffInMs ) < YEAR_IN_MILLIS ) {
29- return durationFormatter . format ( Math . floor ( diffInMs / MONTH_IN_MILLIS ) , 'month' ) ;
29+ if ( Math . abs ( relativeTimeMillis ) < HOUR_IN_MILLIS ) {
30+ return durationFormatter . format ( Math . floor ( relativeTimeMillis / MINUTE_IN_MILLIS ) , 'minute' ) ;
31+ } else if ( Math . abs ( relativeTimeMillis ) < DAY_IN_MILLIS ) {
32+ return durationFormatter . format ( Math . floor ( relativeTimeMillis / HOUR_IN_MILLIS ) , 'hour' ) ;
33+ } else if ( Math . abs ( relativeTimeMillis ) < MONTH_IN_MILLIS ) {
34+ return durationFormatter . format ( Math . floor ( relativeTimeMillis / DAY_IN_MILLIS ) , 'day' ) ;
35+ } else if ( Math . abs ( relativeTimeMillis ) < YEAR_IN_MILLIS ) {
36+ return durationFormatter . format ( Math . floor ( relativeTimeMillis / MONTH_IN_MILLIS ) , 'month' ) ;
3037 } else {
31- return durationFormatter . format ( Math . floor ( diffInMs / YEAR_IN_MILLIS ) , 'year' ) ;
38+ return durationFormatter . format ( Math . floor ( relativeTimeMillis / YEAR_IN_MILLIS ) , 'year' ) ;
3239 }
3340}
3441
35- function createFormatter ( unit : string ) {
36- return Intl . NumberFormat ( 'en-US' , {
37- style : 'unit' ,
38- unit,
39- unitDisplay : 'long'
40- } ) ;
41- }
42-
43- export function humanizeUnit ( diffInMs ?: number ) : string {
42+ /**
43+ * Converts a number of milliseconds into a human-readable string with units, indicating an amount of time.
44+ * Negative numbers have no meaning and are considered to be "Less than a minute".
45+ *
46+ * @param millis The number of milliseconds to convert.
47+ * @returns A humanized duration. For example, "2 minutes", "2 hours", "2 days", or "2 months".
48+ */
49+ export function humanizeUnit ( millis ?: number ) : string {
4450 // assume a blank or empty string is a zero
4551 // assume anything less than 0 is a zero
46- if ( ! diffInMs || diffInMs < MINUTE_IN_MILLIS ) {
52+ if ( ! millis || millis < MINUTE_IN_MILLIS ) {
4753 return 'Less than a minute' ;
4854 }
4955 let unit : string ;
5056 let unitDiff : number ;
51- if ( diffInMs < HOUR_IN_MILLIS ) {
57+ if ( millis < HOUR_IN_MILLIS ) {
5258 unit = 'minute' ;
53- unitDiff = Math . floor ( diffInMs / MINUTE_IN_MILLIS ) ;
54- } else if ( diffInMs < DAY_IN_MILLIS ) {
59+ unitDiff = Math . floor ( millis / MINUTE_IN_MILLIS ) ;
60+ } else if ( millis < DAY_IN_MILLIS ) {
5561 unit = 'hour' ;
56- unitDiff = Math . floor ( diffInMs / HOUR_IN_MILLIS ) ;
57- } else if ( diffInMs < MONTH_IN_MILLIS ) {
62+ unitDiff = Math . floor ( millis / HOUR_IN_MILLIS ) ;
63+ } else if ( millis < MONTH_IN_MILLIS ) {
5864 unit = 'day' ;
59- unitDiff = Math . floor ( diffInMs / DAY_IN_MILLIS ) ;
60- } else if ( diffInMs < YEAR_IN_MILLIS ) {
65+ unitDiff = Math . floor ( millis / DAY_IN_MILLIS ) ;
66+ } else if ( millis < YEAR_IN_MILLIS ) {
6167 unit = 'month' ;
62- unitDiff = Math . floor ( diffInMs / MONTH_IN_MILLIS ) ;
68+ unitDiff = Math . floor ( millis / MONTH_IN_MILLIS ) ;
6369 } else {
6470 unit = 'year' ;
65- unitDiff = Math . floor ( diffInMs / YEAR_IN_MILLIS ) ;
71+ unitDiff = Math . floor ( millis / YEAR_IN_MILLIS ) ;
6672 }
6773
6874 return createFormatter ( unit ) . format ( unitDiff ) ;
6975}
76+
77+ function createFormatter ( unit : string ) {
78+ return Intl . NumberFormat ( 'en-US' , {
79+ style : 'unit' ,
80+ unit,
81+ unitDisplay : 'long'
82+ } ) ;
83+ }
0 commit comments