From e8532a062198a322822a6f975deba016c9ead75f Mon Sep 17 00:00:00 2001 From: Nils Streijffert Date: Thu, 4 Feb 2021 16:36:39 +0100 Subject: [PATCH] Port from moment to Luxon --- .../components/CatalogPage/utils/timeUtil.js | 10 ++++---- .../CatalogPage/utils/timeUtil.test.js | 24 +++++++++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/plugins/catalog/src/components/CatalogPage/utils/timeUtil.js b/plugins/catalog/src/components/CatalogPage/utils/timeUtil.js index 327f4177a8..4c5e98d97c 100644 --- a/plugins/catalog/src/components/CatalogPage/utils/timeUtil.js +++ b/plugins/catalog/src/components/CatalogPage/utils/timeUtil.js @@ -14,7 +14,7 @@ * limitations under the License. */ -import moment from 'moment'; +import { DateTime } from 'luxon'; import goodMorning from './locales/goodMorning.locales.json'; import goodAfternoon from './locales/goodAfternoon.locales.json'; import goodEvening from './locales/goodEvening.locales.json'; @@ -26,22 +26,22 @@ import goodEvening from './locales/goodEvening.locales.json'; * @returns {bool} Whether the date is valid or not. */ export function isValidDate(date) { - return moment(date).isValid(); + return DateTime.fromISO(date).isValid; } /** * Validates that a date is a valid ISO string and a specific format. * * @param date A date string - * @param format A format string or an array of format strings to validate against. + * @param format A format string to validate against. See https://moment.github.io/luxon/docs/manual/parsing.html#table-of-tokens for reference. * @returns {bool} Whether the date is valid or not according to the format. */ export function isValidDateAndFormat(date, format) { - return moment(date, format, true).isValid(); + return DateTime.fromFormat(date, format).isValid; } export function relativeTime(timestamp) { - return moment(timestamp).fromNow(); + return DateTime.fromISO(timestamp).toRelative(); } // Select a large random integer at startup, to prevent the greetings to change every time the user diff --git a/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js b/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js index d18b4a5b20..809d2aeac4 100644 --- a/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js +++ b/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js @@ -14,19 +14,29 @@ * limitations under the License. */ -import { isValidDateAndFormat, getTimeBasedGreeting } from './timeUtil'; +import { + isValidDate, + isValidDateAndFormat, + getTimeBasedGreeting, +} from './timeUtil'; +import { DateTime } from 'luxon'; + +it('validate isValidDate', () => { + expect(isValidDate(DateTime.local().toISO())).toBeTruthy(); + expect(isValidDate('2021-02-04 15:00:10')).toBeFalsy(); +}); it('validates time format', () => { const valid = isValidDateAndFormat( - '1970-01-01T00:00:00', - 'YYYY-MM-DD[T]HH:mm:ss', + '1970-01-01 00:00:00', + 'yyyy-MM-dd hh:mm:ss', ); const invalid = isValidDateAndFormat( - '1970/01/01T00:00:00', - 'YYYY-MM-DD[T]HH:mm:ss', + '1970-01-01T00:00:00', + 'yyyy-MM-ddThh:mm:ss', ); - expect(valid).toBe(true); - expect(invalid).toBe(false); + expect(valid).toBeTruthy(); + expect(invalid).toBeFalsy(); }); it('has greeting and language', () => {