Skip to content

Commit

Permalink
feat(functions): add secondsToTime function and test file
Browse files Browse the repository at this point in the history
  • Loading branch information
xGooddevilx authored and ASafaeirad committed Nov 4, 2024
1 parent 25bd6b8 commit a4322c7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/pages/function/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"not": "not",
"sleep": "sleep",
"throttle": "throttle",
"try-or": "tryOr"
"try-or": "tryOr",
"seconds-to-time": "seconds-to-time"
}
26 changes: 26 additions & 0 deletions src/function/secondsToTIme.spec.ts
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);
});
});
50 changes: 50 additions & 0 deletions src/function/secondsToTime.ts
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'.`,
);
}
}

0 comments on commit a4322c7

Please sign in to comment.