diff --git a/mocks/index.ts b/mocks/index.ts index fd3effb39..e00b19fb4 100644 --- a/mocks/index.ts +++ b/mocks/index.ts @@ -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 diff --git a/mocks/simpleSongWOID.mock.txt b/mocks/simpleSongWOID.mock.txt new file mode 100644 index 000000000..416788050 --- /dev/null +++ b/mocks/simpleSongWOID.mock.txt @@ -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 diff --git a/src/songParser.spec.ts b/src/songParser.spec.ts index f7a730a09..c275344f4 100644 --- a/src/songParser.spec.ts +++ b/src/songParser.spec.ts @@ -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, @@ -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(` diff --git a/src/songParser.ts b/src/songParser.ts index 37906a3bc..d2baa5044 100644 --- a/src/songParser.ts +++ b/src/songParser.ts @@ -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. @@ -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)) {