Skip to content

Commit

Permalink
feat: add helper function to convert seconds to timeunits (#28)
Browse files Browse the repository at this point in the history
* feat: add helper function to convert seconds to timeunits

* chore: add spec

* chore: refactor

* chore: improve spec coverage

* chore: Publish v0.0.22

---------

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
  • Loading branch information
vishnu-narayanan and muhsin-k committed Feb 22, 2024
1 parent 1203c4d commit d2aa147
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@chatwoot/utils",
"version": "0.0.21",
"version": "0.0.22",
"description": "Chatwoot utils",
"private": false,
"license": "MIT",
Expand Down
24 changes: 24 additions & 0 deletions src/helpers.ts
Expand Up @@ -96,3 +96,27 @@ export const trimContent = (
}
return trimmedContent;
};

/**
* @name convertSecondsToTimeUnit
* @description Convert seconds to time unit
* @param seconds number
* @param unitNames object
* @returns time and unit
* @example
* convertToUnit(60, { minute: 'm', hour: 'h', day: 'd' }); // { time: 1, unit: 'm' }
* convertToUnit(60, { minute: 'Minutes', hour: 'Hours', day: 'Days' }); // { time: 1, unit: 'Minutes' }
*/

export const convertSecondsToTimeUnit = (
seconds: number,
unitNames: { minute: string; hour: string; day: string }
) => {
if (seconds === null || seconds === 0)
return { time: null, unit: unitNames.minute };
if (seconds < 3600)
return { time: Number((seconds / 60).toFixed(1)), unit: unitNames.minute };
if (seconds < 86400)
return { time: Number((seconds / 3600).toFixed(1)), unit: unitNames.hour };
return { time: Number((seconds / 86400).toFixed(1)), unit: unitNames.day };
};
46 changes: 46 additions & 0 deletions test/helpers.test.ts
@@ -0,0 +1,46 @@
import { convertSecondsToTimeUnit } from '../src/helpers';

describe('#convertSecondsToTimeUnit', () => {
it("it should return { time: 1, unit: 'm' } if 60 seconds passed", () => {
expect(
convertSecondsToTimeUnit(60, { minute: 'm', hour: 'h', day: 'd' })
).toEqual({ time: 1, unit: 'm' });
});
it("it should return { time: 1, unit: 'Minutes' } if 60 seconds passed", () => {
expect(
convertSecondsToTimeUnit(60, {
minute: 'Minutes',
hour: 'Hours',
day: 'Days',
})
).toEqual({ time: 1, unit: 'Minutes' });
});
it("it should return { time: 1, unit: 'h' } if 3600 seconds passed", () => {
expect(
convertSecondsToTimeUnit(3600, { minute: 'm', hour: 'h', day: 'd' })
).toEqual({ time: 1, unit: 'h' });
});
it("it should return { time: 1, unit: 'Hours' } if 3600 seconds passed", () => {
expect(
convertSecondsToTimeUnit(3600, {
minute: 'Minutes',
hour: 'Hours',
day: 'Days',
})
).toEqual({ time: 1, unit: 'Hours' });
});
it("it should return { time: 1, unit: 'd' } if 86400 seconds passed", () => {
expect(
convertSecondsToTimeUnit(86400, { minute: 'm', hour: 'h', day: 'd' })
).toEqual({ time: 1, unit: 'd' });
});
it("it should return { time: 1, unit: 'Days' } if 86400 seconds passed", () => {
expect(
convertSecondsToTimeUnit(86400, {
minute: 'Minutes',
hour: 'Hours',
day: 'Days',
})
).toEqual({ time: 1, unit: 'Days' });
});
});

0 comments on commit d2aa147

Please sign in to comment.