From 0f98ffee64f9bf06f429c909b3014ddbcd414f3a Mon Sep 17 00:00:00 2001 From: Sam Seay Date: Tue, 12 Mar 2024 12:06:53 +0800 Subject: [PATCH] Remove redundant dependency on moment in the csv-export package (#45410) --- .../changelog/45410-dev-remove-moment-csv-export | 4 ++++ packages/js/csv-export/package.json | 3 +-- packages/js/csv-export/src/index.ts | 15 +++++++++++++-- packages/js/csv-export/src/test/index.ts | 13 +++++-------- pnpm-lock.yaml | 5 +---- 5 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 packages/js/csv-export/changelog/45410-dev-remove-moment-csv-export diff --git a/packages/js/csv-export/changelog/45410-dev-remove-moment-csv-export b/packages/js/csv-export/changelog/45410-dev-remove-moment-csv-export new file mode 100644 index 00000000000..aed97f40c0a --- /dev/null +++ b/packages/js/csv-export/changelog/45410-dev-remove-moment-csv-export @@ -0,0 +1,4 @@ +Significance: minor +Type: dev + +Remove moment dependency from the csv-export package. \ No newline at end of file diff --git a/packages/js/csv-export/package.json b/packages/js/csv-export/package.json index 33fe63d49f7..7c4655aef1b 100644 --- a/packages/js/csv-export/package.json +++ b/packages/js/csv-export/package.json @@ -49,8 +49,7 @@ }, "dependencies": { "@types/node": "^16.18.68", - "browser-filesaver": "^1.1.1", - "moment": "^2.29.4" + "browser-filesaver": "^1.1.1" }, "devDependencies": { "@babel/core": "^7.23.5", diff --git a/packages/js/csv-export/src/index.ts b/packages/js/csv-export/src/index.ts index 5bef351999c..a267ce88c97 100644 --- a/packages/js/csv-export/src/index.ts +++ b/packages/js/csv-export/src/index.ts @@ -1,7 +1,6 @@ /** * External dependencies */ -import moment from 'moment'; import { saveAs } from 'browser-filesaver'; // TODO: Replace this with https://www.npmjs.com/package/file-saver since browser-filesaver is not maintained anymore. export type Header = { @@ -82,6 +81,18 @@ export function generateCSVDataFromTable( headers: Header[], rows: Rows ) { .join( '\n' ); } +/** + * Today's date in the format YYYY-MM-DD + * + * @return {string} the formatted date. + */ +function todayDateStr() { + const date = new Date(); + const dateStr = date.toISOString().split( 'T' )[ 0 ]; + + return dateStr; +} + /** * Generates a file name for CSV files based on the provided name, the current date * and the provided params, which are all appended with hyphens. @@ -96,7 +107,7 @@ export function generateCSVFileName( ) { const fileNameSections = [ name.toLowerCase().replace( /[^a-z0-9]/g, '-' ), - moment().format( 'YYYY-MM-DD' ), + todayDateStr(), Object.keys( params ) .map( ( key ) => diff --git a/packages/js/csv-export/src/test/index.ts b/packages/js/csv-export/src/test/index.ts index ec6a4c19760..155c7d12b05 100644 --- a/packages/js/csv-export/src/test/index.ts +++ b/packages/js/csv-export/src/test/index.ts @@ -2,7 +2,6 @@ /** * External dependencies */ -import moment from 'moment'; import { saveAs } from 'browser-filesaver'; /** @@ -65,16 +64,16 @@ describe( 'generateCSVDataFromTable', () => { } ); describe( 'generateCSVFileName', () => { + jest.useFakeTimers().setSystemTime( new Date( '2024-12-23' ) ); + it( 'should generate a file name with the date when no params are provided', () => { const fileName = generateCSVFileName(); - expect( fileName ).toBe( moment().format( 'YYYY-MM-DD' ) + '.csv' ); + expect( fileName ).toBe( '2024-12-23.csv' ); } ); it( 'should generate a file name with the `name` and the date', () => { const fileName = generateCSVFileName( 'Revenue table' ); - expect( fileName ).toBe( - 'revenue-table_' + moment().format( 'YYYY-MM-DD' ) + '.csv' - ); + expect( fileName ).toBe( 'revenue-table_2024-12-23.csv' ); } ); it( 'should generate a file name with the `name` and `params`', () => { @@ -83,9 +82,7 @@ describe( 'generateCSVFileName', () => { order: 'desc', } ); expect( fileName ).toBe( - 'revenue-table_' + - moment().format( 'YYYY-MM-DD' ) + - '_orderby-revenue_order-desc.csv' + 'revenue-table_2024-12-23_orderby-revenue_order-desc.csv' ); } ); } ); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 505b3c43501..0dfd8002824 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -945,9 +945,6 @@ importers: browser-filesaver: specifier: ^1.1.1 version: 1.1.1 - moment: - specifier: ^2.29.4 - version: 2.29.4 devDependencies: '@babel/core': specifier: ^7.23.5 @@ -52999,4 +52996,4 @@ packages: resolution: {tarball: https://codeload.github.com/woocommerce/sourcebuster-js/tar.gz/411d8dffaa37d7602d678c2ec02baacee93c12d5} name: sourcebuster version: 1.1.2 - dev: false \ No newline at end of file + dev: false