From 15ae9ad9307fd0d19b76741f74095c0614b164f7 Mon Sep 17 00:00:00 2001 From: Mok Date: Sun, 11 Jul 2021 12:12:57 +0800 Subject: [PATCH] Added dayjs package --- CHANGELOG.md | 1 + package-lock.json | 5 +++++ package.json | 1 + src/types/date.d.ts | 3 +++ src/utils/date.ts | 10 ++++++++- test/utils/date.test.ts | 45 ++++++++++++++++++++++++++++++++--------- 6 files changed, 55 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cdb441..05e29c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - Updated declaration file extension so it will not be included in final build - Added multiple date format support for `expiry` field +- Added `dayjs` package ## 3.0.1 (July 11, 2021) diff --git a/package-lock.json b/package-lock.json index 033366b..2955e24 100644 --- a/package-lock.json +++ b/package-lock.json @@ -659,6 +659,11 @@ "which": "^2.0.1" } }, + "dayjs": { + "version": "1.10.6", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.10.6.tgz", + "integrity": "sha512-AztC/IOW4L1Q41A86phW5Thhcrco3xuAA+YX/BLpLWWjRcTj5TOt/QImBLmCKlrF7u7k47arTnOyL6GnbG8Hvw==" + }, "debug": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", diff --git a/package.json b/package.json index 959c03c..544779f 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ }, "dependencies": { "commander": "^8.0.0", + "dayjs": "^1.10.6", "lodash.get": "^4.4.2", "table": "^6.7.1" }, diff --git a/src/types/date.d.ts b/src/types/date.d.ts index 9d27fbc..38c00e4 100644 --- a/src/types/date.d.ts +++ b/src/types/date.d.ts @@ -1,4 +1,7 @@ export type DateAnalysis = { valid: boolean; expired?: boolean; + days?: number; + months?: number; + years?: number; }; diff --git a/src/utils/date.ts b/src/utils/date.ts index a05646f..940a633 100644 --- a/src/utils/date.ts +++ b/src/utils/date.ts @@ -1,3 +1,4 @@ +import dayjs from 'dayjs'; import { DateAnalysis } from 'src/types'; /** @@ -19,11 +20,18 @@ export function analyzeExpiry(expiry?: string | number, now: string | number = n if (!expiry) { return { valid: true }; } + if (!isValidDate(expiry) || !isValidDate(now)) { return { valid: false }; } + + const dayjsNow = dayjs(now); + return { valid: true, - expired: new Date(now).getTime() > new Date(expiry).getTime(), + expired: dayjsNow.isAfter(expiry), + days: dayjsNow.diff(expiry, 'days'), + months: dayjsNow.diff(expiry, 'months'), + years: dayjsNow.diff(expiry, 'years'), }; } diff --git a/test/utils/date.test.ts b/test/utils/date.test.ts index c94a934..05370c2 100644 --- a/test/utils/date.test.ts +++ b/test/utils/date.test.ts @@ -52,19 +52,46 @@ describe('Date utils', () => { it('should be able to analyze the given timestamp correctly', () => { // Only dates - expect(analyzeExpiry('2020-01-31', '2020-01-01')).to.deep.equal({ valid: true, expired: false }); - expect(analyzeExpiry('2020-01-31', '2020-01-31')).to.deep.equal({ valid: true, expired: false }); - expect(analyzeExpiry('2020-01-31', '2020-02-02')).to.deep.equal({ valid: true, expired: true }); + expect(analyzeExpiry('2020-01-31', '2020-01-01')).to.deep.equal({ valid: true, expired: false, days: -30, months: 0, years: 0 }); + expect(analyzeExpiry('2020-01-31', '2020-01-31')).to.deep.equal({ valid: true, expired: false, days: 0, months: 0, years: 0 }); + expect(analyzeExpiry('2020-01-31', '2020-02-02')).to.deep.equal({ valid: true, expired: true, days: 2, months: 0, years: 0 }); // Dates & time - expect(analyzeExpiry('1 March 2020 3:00 pm', '1 March 2020 2:59:00 pm')).to.deep.equal({ valid: true, expired: false }); - expect(analyzeExpiry('1 March 2020 3:00 pm', '1 March 2020 3:00:00 pm')).to.deep.equal({ valid: true, expired: false }); - expect(analyzeExpiry('1 March 2020 3:00 pm', '1 March 2020 3:00:01 pm')).to.deep.equal({ valid: true, expired: true }); + expect(analyzeExpiry('1 March 2020 3:00 pm', '1 March 2020 2:59:00 pm')).to.deep.equal({ + valid: true, + expired: false, + days: 0, + months: 0, + years: 0, + }); + expect(analyzeExpiry('1 March 2020 3:00 pm', '1 March 2020 3:00:00 pm')).to.deep.equal({ + valid: true, + expired: false, + days: 0, + months: 0, + years: 0, + }); + expect(analyzeExpiry('1 March 2020 3:00 pm', '1 March 2020 3:00:01 pm')).to.deep.equal({ + valid: true, + expired: true, + days: 0, + months: 0, + years: 0, + }); // Milliseconds - expect(analyzeExpiry(1327611110410, 1327611110409)).to.deep.equal({ valid: true, expired: false }); - expect(analyzeExpiry(1327611110410, 1327611110410)).to.deep.equal({ valid: true, expired: false }); - expect(analyzeExpiry(1327611110410, 1327611110411)).to.deep.equal({ valid: true, expired: true }); + expect(analyzeExpiry(1327611110410, 1327611110409)).to.deep.equal({ valid: true, expired: false, days: 0, months: 0, years: 0 }); + expect(analyzeExpiry(1327611110410, 1327611110410)).to.deep.equal({ valid: true, expired: false, days: 0, months: 0, years: 0 }); + expect(analyzeExpiry(1327611110410, 1327611110411)).to.deep.equal({ valid: true, expired: true, days: 0, months: 0, years: 0 }); + }); + + it('should be able to analyze the time difference correctly', () => { + expect(analyzeExpiry('2020-01-31', '2020-01-01')).to.deep.equal({ valid: true, expired: false, days: -30, months: 0, years: 0 }); + expect(analyzeExpiry('2020-01-31', '2020-02-02')).to.deep.equal({ valid: true, expired: true, days: 2, months: 0, years: 0 }); + expect(analyzeExpiry('2020-01-31', '2020-03-01')).to.deep.equal({ valid: true, expired: true, days: 30, months: 1, years: 0 }); + expect(analyzeExpiry('2020-01-31', '2021-01-01')).to.deep.equal({ valid: true, expired: true, days: 336, months: 11, years: 0 }); + expect(analyzeExpiry('2020-01-31', '2021-01-31')).to.deep.equal({ valid: true, expired: true, days: 366, months: 12, years: 1 }); + expect(analyzeExpiry('2020-01-31', '2025-10-10')).to.deep.equal({ valid: true, expired: true, days: 2079, months: 68, years: 5 }); }); }); });