-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(functions): add secondsToTime function and test file
- Loading branch information
1 parent
25bd6b8
commit a4322c7
Showing
3 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { secondsToTime } from './secondsToTime.ts'; | ||
|
||
describe('secondToTime', () => { | ||
it('should return zero when pass 0 seconds', () => { | ||
const format = 'mm:ss'; | ||
const sec = 0; | ||
|
||
expect(secondsToTime({ sec, format })).toBe('00:00'); | ||
}); | ||
|
||
it.todo('should throw an error when pass invalid format', () => { | ||
const inValidFormat = 'ss:ss:ss'; | ||
const sec = 120; | ||
|
||
// @ts-expect-error : not assignable type, ignored in order to have a test | ||
expect(secondsToTime({ sec, format: inValidFormat })).toThrow(); | ||
}); | ||
|
||
it.each([ | ||
[{ sec: 3661, format: 'hh:mm:ss' as const }, '01:01:01'], | ||
[{ sec: 3661, format: 'hh:mm' as const }, '01:01'], | ||
[{ sec: 61, format: 'mm:ss' as const }, '01:01'], | ||
])('should format seconds to %s for input %o', (input, expected) => { | ||
expect(secondsToTime(input)).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Formats seconds into the specified time format. | ||
* | ||
* @export | ||
* @param {Object} params - The input object containing: | ||
* @param {number} params.sec - The total number of seconds to be converted. | ||
* @param {'hh:mm:ss' | 'hh:mm' | 'mm:ss'} params.format - The desired output format. | ||
* @returns {string} - The formatted time string. | ||
* | ||
* @example | ||
* | ||
* secondsToTime({ sec: 3661, format: 'hh:mm:ss' }) // '01:01:01' | ||
* secondsToTime({ sec: 3661, format: 'hh:mm' }) // '01:01' | ||
* secondsToTime({ sec: 61, format: 'mm:ss' }) // '01:01' | ||
*/ | ||
|
||
function formatValue(val: number): string { | ||
return String(val).padStart(2, '0'); | ||
} | ||
|
||
function splitTime(sec: number) { | ||
return { | ||
hours: Math.trunc(sec / 3600), | ||
minutes: Math.trunc((sec % 3600) / 60), | ||
seconds: sec % 60, | ||
}; | ||
} | ||
|
||
export function secondsToTime({ | ||
sec, | ||
format, | ||
}: { | ||
sec: number; | ||
format: 'hh:mm:ss' | 'hh:mm' | 'mm:ss'; | ||
}): number | string { | ||
const { hours, minutes, seconds } = splitTime(sec); | ||
|
||
switch (format) { | ||
case 'hh:mm:ss': | ||
return `${formatValue(hours)}:${formatValue(minutes)}:${formatValue(seconds)}`; | ||
case 'hh:mm': | ||
return `${formatValue(hours)}:${formatValue(minutes)}`; | ||
case 'mm:ss': | ||
return `${formatValue(minutes)}:${formatValue(seconds)}`; | ||
default: | ||
throw new Error( | ||
`Invalid format: '${format as string}'. Accepted formats are 'hh:mm:ss', 'hh:mm', or 'mm:ss'.`, | ||
); | ||
} | ||
} |