Skip to content

Commit

Permalink
fix: csv with spaces not handled (#252)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
NBS-LLC authored Oct 21, 2023
2 parents d75bd4f + 59cfde5 commit c2ea4df
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ insert_final_newline = true
trim_trailing_whitespace = true

[*.ts]
indent_size = 4
quote_type = single

[*.md]
Expand Down
5 changes: 5 additions & 0 deletions e2e/test/assets/playlists/CsvWithSpaces.csv
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions src/app/csvPlaylist.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
4 changes: 2 additions & 2 deletions src/app/csvPlaylist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit c2ea4df

Please sign in to comment.