From 59cfde5ebf5e1859a30bdd50aad645e119e1d9b2 Mon Sep 17 00:00:00 2001 From: Nick Derevjanik Date: Sat, 21 Oct 2023 11:32:20 -0700 Subject: [PATCH] fix: csv with spaces not handled * Updated the editor config to use 4 spaces for TS (to keep code style consistent). * Added a test CSV file to assets for manual testing. * Added a unit test that reproduces the issue. * Added trim calls to the CSV parser's header and value transforms. Refs: #221 --- .editorconfig | 1 + e2e/test/assets/playlists/CsvWithSpaces.csv | 5 +++++ src/app/csvPlaylist.spec.ts | 12 ++++++++++++ src/app/csvPlaylist.ts | 4 ++-- 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 e2e/test/assets/playlists/CsvWithSpaces.csv diff --git a/.editorconfig b/.editorconfig index 59d9a3a..71848ca 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,6 +9,7 @@ insert_final_newline = true trim_trailing_whitespace = true [*.ts] +indent_size = 4 quote_type = single [*.md] diff --git a/e2e/test/assets/playlists/CsvWithSpaces.csv b/e2e/test/assets/playlists/CsvWithSpaces.csv new file mode 100644 index 0000000..afbbdbd --- /dev/null +++ b/e2e/test/assets/playlists/CsvWithSpaces.csv @@ -0,0 +1,5 @@ +Title, Artist +Electric Love [Oliver Remix], BØRNS +XXX 88, MØ +Twilight vs Breathe (Jack Trades Remix), Adam K & Soha +No Place, Rüfüs Du Sol diff --git a/src/app/csvPlaylist.spec.ts b/src/app/csvPlaylist.spec.ts index b64eb7d..e80e8aa 100644 --- a/src/app/csvPlaylist.spec.ts +++ b/src/app/csvPlaylist.spec.ts @@ -48,5 +48,17 @@ describe('CsvPlaylist', () => { const songs = playlist.getSongs(); expect(songs.length).toEqual(2); }); + + it('should handle csv with spaces', () => { + const csv = + 'Title, Artist\n' + + 'Some Song, Some Artist'; + + const playlist = new CsvPlaylist(csv, 'Unit Test - CSV With Spaces Playlist'); + const songs = playlist.getSongs(); + expect(songs.length).toEqual(1); + expect(songs[0].title).toEqual('Some Song'); + expect(songs[0].artist).toEqual('Some Artist'); + }); }); }); diff --git a/src/app/csvPlaylist.ts b/src/app/csvPlaylist.ts index 3101a0a..518f610 100644 --- a/src/app/csvPlaylist.ts +++ b/src/app/csvPlaylist.ts @@ -17,8 +17,8 @@ export class CsvPlaylist implements Playlist { const options: ParseConfig = { header: true, skipEmptyLines: true, - transformHeader: header => header.toUpperCase(), - transform: value => decode(value), + transformHeader: header => header.trim().toUpperCase(), + transform: value => decode(value.trim()), }; this.csv = parse(playlistCsv, options);