Skip to content

Commit

Permalink
refactor(songParser): Refactor songParser to handle songs without cor…
Browse files Browse the repository at this point in the history
…rect ID.

This commit refactors the songParser module to handle songs that do not have a correct ID. It updates the parse function to check if the ID is unset and generate a unique ID if necessary. This change ensures that songs without a correct ID can still be parsed correctly.

This refactor improves the flexibility and usability of the songParser module by allowing it to handle a wider range of song formats.
  • Loading branch information
ioanlucut committed Nov 13, 2023
1 parent fd64ff7 commit 1ab9d0c
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 1 deletion.
5 changes: 5 additions & 0 deletions mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export const SIMPLE_SONG_MOCK_FILE_CONTENT = fs
.readFileSync(path.resolve(__dirname, SIMPLE_SONG_MOCK_FILE_NAME))
.toString();

export const SIMPLE_SONG_WO_ID_MOCK_FILE_NAME = 'simpleSongWOID.mock.txt';
export const SIMPLE_SONG_WO_ID__MOCK_FILE_CONTENT = fs
.readFileSync(path.resolve(__dirname, SIMPLE_SONG_WO_ID_MOCK_FILE_NAME))
.toString();

export const SONG_WITH_SUBSECTIONS_MOCK_FILE_NAME =
'songWithSubsections.mock.txt';
export const SONG_WITH_SUBSECTIONS_MOCK_FILE_CONTENT = fs
Expand Down
41 changes: 41 additions & 0 deletions mocks/simpleSongWOID.mock.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[title]
My main title {alternative: { alternative 1; alternative 2 }, composer: {composer 1; composer 2}, writer: {writer 1; writer 2}, arranger: {arranger 1;arranger 2}, interpreter: {interpreter 1;interpreter 2}, band: {band 1;band 2}, key: {*}, tempo: {*}, tags: {tags 1; tags 2}, version: {ii}, genre: {genre 1; genre 2}, rcId: {*}, id: {*}, contentHash: {655954}}

[sequence]
v1,v2,v3,p,p2,p3,c,c2,c3,b,b2,b3

[v1]
Row for v1

[v2]
Row for v2

[v3]
Row for v3

[p]
Row for p

[p2]
Row for p2

[p3]
Row for p3

[c]
Row for c

[c2]
Row for c2

[c3]
Row for c3

[b]
Row for b

[b2]
Row for b2

[b3]
Row for b3
111 changes: 111 additions & 0 deletions src/songParser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parse } from './songParser.js';
import {
SIMPLE_SONG_MOCK_FILE_CONTENT,
SIMPLE_SONG_WO_ID__MOCK_FILE_CONTENT,
SONG_WITH_MISMATCHING_CONTENT_MOCK_FILE_CONTENT,
SONG_WITH_MISMATCHING_SEQUENCE_MOCK_FILE_CONTENT,
SONG_WITH_SUBSECTIONS_MOCK_FILE_CONTENT,
Expand Down Expand Up @@ -118,6 +119,116 @@ describe('Song parser', () => {
`);
});

it('should parse a song (w/o correct ID) correctly', () => {
expect(
parse(SIMPLE_SONG_WO_ID__MOCK_FILE_CONTENT)
).toMatchInlineSnapshot(`
{
"alternative": "alternative 1; alternative 2",
"arranger": "arranger 1; arranger 2",
"band": "band 1; band 2",
"composer": "composer 1; composer 2",
"contentHash": "0173a1",
"genre": "genre 1; genre 2",
"id": "iKi6xsDjgpFEVwrfdumTA4",
"interpreter": "interpreter 1; interpreter 2",
"key": "*",
"rcId": "*",
"sectionOrder": [
"[v1]",
"[v2]",
"[v3]",
"[p]",
"[p2]",
"[p3]",
"[c]",
"[c2]",
"[c3]",
"[b]",
"[b2]",
"[b3]",
],
"sectionsMap": {
"[b2]": {
"content": "Row for b2",
"sectionIdentifier": "[b2]",
},
"[b3]": {
"content": "Row for b3",
"sectionIdentifier": "[b3]",
},
"[b]": {
"content": "Row for b",
"sectionIdentifier": "[b]",
},
"[c2]": {
"content": "Row for c2",
"sectionIdentifier": "[c2]",
},
"[c3]": {
"content": "Row for c3",
"sectionIdentifier": "[c3]",
},
"[c]": {
"content": "Row for c",
"sectionIdentifier": "[c]",
},
"[p2]": {
"content": "Row for p2",
"sectionIdentifier": "[p2]",
},
"[p3]": {
"content": "Row for p3",
"sectionIdentifier": "[p3]",
},
"[p]": {
"content": "Row for p",
"sectionIdentifier": "[p]",
},
"[sequence]": {
"content": "v1,v2,v3,p,p2,p3,c,c2,c3,b,b2,b3",
"sectionIdentifier": "[sequence]",
},
"[title]": {
"content": "My main title {alternative: { alternative 1; alternative 2 }, composer: {composer 1; composer 2}, writer: {writer 1; writer 2}, arranger: {arranger 1;arranger 2}, interpreter: {interpreter 1;interpreter 2}, band: {band 1;band 2}, key: {*}, tempo: {*}, tags: {tags 1; tags 2}, version: {ii}, genre: {genre 1; genre 2}, rcId: {*}, id: {*}, contentHash: {655954}}",
"sectionIdentifier": "[title]",
},
"[v1]": {
"content": "Row for v1",
"sectionIdentifier": "[v1]",
},
"[v2]": {
"content": "Row for v2",
"sectionIdentifier": "[v2]",
},
"[v3]": {
"content": "Row for v3",
"sectionIdentifier": "[v3]",
},
},
"sequence": [
"v1",
"v2",
"v3",
"p",
"p2",
"p3",
"c",
"c2",
"c3",
"b",
"b2",
"b3",
],
"tags": "tags 1; tags 2",
"tempo": "*",
"title": "My main title",
"version": "ii",
"writer": "writer 1; writer 2",
}
`);
});

it('should parse a song (w/ subsections) correctly', () => {
expect(parse(SONG_WITH_SUBSECTIONS_MOCK_FILE_CONTENT))
.toMatchInlineSnapshot(`
Expand Down
3 changes: 2 additions & 1 deletion src/songParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
multiToSingle,
} from './core.js';
import { COMMA, EMPTY_STRING, UNSET_META } from './constants.js';
import { isEqual } from 'lodash-es';

/**
* Parses the content of a song to its basic AST structure.
Expand Down Expand Up @@ -96,7 +97,7 @@ export const parse = (
songAST.version = version || UNSET_META;

songAST.rcId = rcId || UNSET_META;
songAST.id = id || getUniqueId();
songAST.id = id && !isEqual(id, UNSET_META) ? id : getUniqueId();
}

if ([SongSection.SEQUENCE].includes(sectionIdentifier)) {
Expand Down

0 comments on commit 1ab9d0c

Please sign in to comment.