feat: add call duration
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
||||
dynamicTime,
|
||||
dateFormat,
|
||||
shortTimestamp,
|
||||
formatDuration,
|
||||
getDayDifferenceFromNow,
|
||||
hasOneDayPassed,
|
||||
} from 'shared/helpers/timeHelper';
|
||||
@@ -93,6 +94,24 @@ describe('#shortTimestamp', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#formatDuration', () => {
|
||||
it('returns empty string for invalid values', () => {
|
||||
expect(formatDuration(null)).toEqual('');
|
||||
expect(formatDuration(undefined)).toEqual('');
|
||||
expect(formatDuration(-1)).toEqual('');
|
||||
expect(formatDuration('abc')).toEqual('');
|
||||
});
|
||||
|
||||
it('formats short durations as mm:ss', () => {
|
||||
expect(formatDuration(0)).toEqual('00:00');
|
||||
expect(formatDuration(133)).toEqual('02:13');
|
||||
});
|
||||
|
||||
it('formats long durations as hh:mm:ss', () => {
|
||||
expect(formatDuration(3733)).toEqual('01:02:13');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getDayDifferenceFromNow', () => {
|
||||
it('returns 0 for timestamps from today', () => {
|
||||
// Mock current date: May 5, 2023
|
||||
|
||||
@@ -93,6 +93,32 @@ export const shortTimestamp = (time, withAgo = false) => {
|
||||
return convertToShortTime;
|
||||
};
|
||||
|
||||
/**
|
||||
* Formats a duration in seconds into mm:ss or hh:mm:ss.
|
||||
* @param {number|string} durationInSeconds - Duration in seconds.
|
||||
* @returns {string} Formatted duration string.
|
||||
*/
|
||||
export const formatDuration = durationInSeconds => {
|
||||
if (durationInSeconds === null || durationInSeconds === undefined) return '';
|
||||
|
||||
const totalSeconds = Number(durationInSeconds);
|
||||
if (Number.isNaN(totalSeconds) || totalSeconds < 0) return '';
|
||||
|
||||
const hours = Math.floor(totalSeconds / 3600);
|
||||
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||
const seconds = totalSeconds % 60;
|
||||
|
||||
if (hours > 0) {
|
||||
return `${hours.toString().padStart(2, '0')}:${minutes
|
||||
.toString()
|
||||
.padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
return `${minutes.toString().padStart(2, '0')}:${seconds
|
||||
.toString()
|
||||
.padStart(2, '0')}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculates the difference in days between now and a given timestamp.
|
||||
* @param {Date} now - Current date/time.
|
||||
|
||||
Reference in New Issue
Block a user