From 03e9befe7abfbfdf514e6ccc2953ad530505e70f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christophe=20No=C3=ABl?= Date: Wed, 24 Jan 2024 21:53:57 +0100 Subject: [PATCH] do not repeat last directives before the next section --- packages/chord-mark/src/parser/helper/songs.js | 13 +++++++++++++ .../tests/unit/parser/helpers/song.spec.js | 7 ++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/chord-mark/src/parser/helper/songs.js b/packages/chord-mark/src/parser/helper/songs.js index e71d4aa1..f6bd0cce 100644 --- a/packages/chord-mark/src/parser/helper/songs.js +++ b/packages/chord-mark/src/parser/helper/songs.js @@ -1,4 +1,5 @@ import _cloneDeep from 'lodash/cloneDeep'; +import _last from 'lodash/last'; import lineTypes from '../lineTypes'; @@ -85,5 +86,17 @@ export function getNthOfLabel(allLines, label, n) { selected.push(line); } }); + + // remmove trailing directive lines + // because they likely apply to the next section + /**/ + while ( + _last(selected) && + (_last(selected).type === lineTypes.KEY_DECLARATION || + _last(selected).type === lineTypes.TIME_SIGNATURE) + ) { + selected.pop(); + } + /** */ return selected; } diff --git a/packages/chord-mark/tests/unit/parser/helpers/song.spec.js b/packages/chord-mark/tests/unit/parser/helpers/song.spec.js index d42d5c22..ed9f67b4 100644 --- a/packages/chord-mark/tests/unit/parser/helpers/song.spec.js +++ b/packages/chord-mark/tests/unit/parser/helpers/song.spec.js @@ -152,7 +152,7 @@ describe('getNthOfLabel', () => { expect(getNthOfLabel).toBeInstanceOf(Function); }); - test('Should return the section identified by its label and index', () => { + test('Should return the section identified by its label and index, without trailing directives', () => { const song2 = ` #v Verse1-line1 @@ -160,12 +160,16 @@ Verse1-line2 Verse1-line3 Verse1-line4 +4/4 #v Verse2-line1 Verse2-line2 Verse2-line3 Verse2-line4 + +key B +3/4 #v Verse3-line1 Verse3-line2 @@ -186,6 +190,7 @@ Verse3-line4`; 'Verse2-line3', 'Verse2-line4', '', + '', ]; const v3 = [ 'Verse3-line1',