From df6ade0ba00a536c2db56955c43d0e933f05328c Mon Sep 17 00:00:00 2001 From: Nils Streijffert Date: Thu, 4 Feb 2021 16:35:15 +0100 Subject: [PATCH 1/5] Add luxon and remove moment --- plugins/catalog/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/catalog/package.json b/plugins/catalog/package.json index c806c58e67..9e2d206467 100644 --- a/plugins/catalog/package.json +++ b/plugins/catalog/package.json @@ -42,7 +42,7 @@ "@types/react": "^16.9", "classnames": "^2.2.6", "git-url-parse": "^11.4.4", - "moment": "^2.26.0", + "luxon": "^1.25.0", "react": "^16.13.1", "react-dom": "^16.13.1", "react-helmet": "6.1.0", From e8532a062198a322822a6f975deba016c9ead75f Mon Sep 17 00:00:00 2001 From: Nils Streijffert Date: Thu, 4 Feb 2021 16:36:39 +0100 Subject: [PATCH 2/5] 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', () => { From e5da858d7a945a6fe160a3f2d17141e4f2237365 Mon Sep 17 00:00:00 2001 From: Nils Streijffert Date: Thu, 4 Feb 2021 16:38:17 +0100 Subject: [PATCH 3/5] Add changeset --- .changeset/pretty-fireants-taste.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/pretty-fireants-taste.md diff --git a/.changeset/pretty-fireants-taste.md b/.changeset/pretty-fireants-taste.md new file mode 100644 index 0000000000..b3168a7671 --- /dev/null +++ b/.changeset/pretty-fireants-taste.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog': patch +--- + +Migrated the package from using moment to Luxon. #4278 From 3b54bf8e64e8973b01e1331ebae93f85986c83de Mon Sep 17 00:00:00 2001 From: Nils Streijffert Date: Thu, 11 Feb 2021 09:33:17 +0100 Subject: [PATCH 4/5] Remove unused functions and luxon --- .changeset/pretty-fireants-taste.md | 2 +- plugins/catalog/package.json | 1 - .../components/CatalogPage/utils/timeUtil.js | 26 ------------------- .../CatalogPage/utils/timeUtil.test.js | 19 -------------- 4 files changed, 1 insertion(+), 47 deletions(-) diff --git a/.changeset/pretty-fireants-taste.md b/.changeset/pretty-fireants-taste.md index b3168a7671..db18c73b2f 100644 --- a/.changeset/pretty-fireants-taste.md +++ b/.changeset/pretty-fireants-taste.md @@ -2,4 +2,4 @@ '@backstage/plugin-catalog': patch --- -Migrated the package from using moment to Luxon. #4278 +Removed unused functions and the moment library. #4278 diff --git a/plugins/catalog/package.json b/plugins/catalog/package.json index 9e2d206467..2ca0b59539 100644 --- a/plugins/catalog/package.json +++ b/plugins/catalog/package.json @@ -42,7 +42,6 @@ "@types/react": "^16.9", "classnames": "^2.2.6", "git-url-parse": "^11.4.4", - "luxon": "^1.25.0", "react": "^16.13.1", "react-dom": "^16.13.1", "react-helmet": "6.1.0", diff --git a/plugins/catalog/src/components/CatalogPage/utils/timeUtil.js b/plugins/catalog/src/components/CatalogPage/utils/timeUtil.js index 4c5e98d97c..e464b1d8fb 100644 --- a/plugins/catalog/src/components/CatalogPage/utils/timeUtil.js +++ b/plugins/catalog/src/components/CatalogPage/utils/timeUtil.js @@ -14,36 +14,10 @@ * limitations under the License. */ -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'; -/** - * Validates that a date is a valid ISO string. - * - * @param date A date string - * @returns {bool} Whether the date is valid or not. - */ -export function isValidDate(date) { - 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 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 DateTime.fromFormat(date, format).isValid; -} - -export function relativeTime(timestamp) { - return DateTime.fromISO(timestamp).toRelative(); -} - // Select a large random integer at startup, to prevent the greetings to change every time the user // navigates. const greetingRandomSeed = Math.floor(Math.random() * 1000000); diff --git a/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js b/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js index 809d2aeac4..81bcf57ae7 100644 --- a/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js +++ b/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js @@ -19,25 +19,6 @@ import { 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-01 00:00:00', - 'yyyy-MM-dd hh:mm:ss', - ); - const invalid = isValidDateAndFormat( - '1970-01-01T00:00:00', - 'yyyy-MM-ddThh:mm:ss', - ); - expect(valid).toBeTruthy(); - expect(invalid).toBeFalsy(); -}); it('has greeting and language', () => { const greeting = getTimeBasedGreeting(); From f2a17ac8906a5a873c2c59bef9b5a883a88e9a59 Mon Sep 17 00:00:00 2001 From: Nils Streijffert Date: Thu, 11 Feb 2021 09:34:55 +0100 Subject: [PATCH 5/5] Removed old imports --- .../src/components/CatalogPage/utils/timeUtil.test.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js b/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js index 81bcf57ae7..a95273325e 100644 --- a/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js +++ b/plugins/catalog/src/components/CatalogPage/utils/timeUtil.test.js @@ -14,11 +14,7 @@ * limitations under the License. */ -import { - isValidDate, - isValidDateAndFormat, - getTimeBasedGreeting, -} from './timeUtil'; +import { getTimeBasedGreeting } from './timeUtil'; it('has greeting and language', () => { const greeting = getTimeBasedGreeting();