From d2aa1474fbe2da433b6f137051b89255aa124962 Mon Sep 17 00:00:00 2001 From: Vishnu Narayanan Date: Thu, 22 Feb 2024 22:47:24 +0530 Subject: [PATCH] feat: add helper function to convert seconds to timeunits (#28) * 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 --- package.json | 2 +- src/helpers.ts | 24 +++++++++++++++++++++++ test/helpers.test.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 test/helpers.test.ts diff --git a/package.json b/package.json index 3788cd4..df0c741 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@chatwoot/utils", - "version": "0.0.21", + "version": "0.0.22", "description": "Chatwoot utils", "private": false, "license": "MIT", diff --git a/src/helpers.ts b/src/helpers.ts index 4fc278f..8dcd591 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -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 }; +}; diff --git a/test/helpers.test.ts b/test/helpers.test.ts new file mode 100644 index 0000000..ce7488f --- /dev/null +++ b/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' }); + }); +});