From 3c42d3b73ca9c43703d6fcefaf295712bd5adab1 Mon Sep 17 00:00:00 2001 From: Martijn Versluis Date: Wed, 4 Dec 2024 20:13:07 +0100 Subject: [PATCH] Ensure Song#getChords() returns unique chords Parse chords and return the stringified version so whitespace is ignored and similar chords are grouped. Related to https://github.com/martijnversluis/ChordSheetJS/issues/1446 --- src/chord_sheet/song.ts | 7 ++++++- test/chord_sheet/song.test.ts | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/chord_sheet/song.ts b/src/chord_sheet/song.ts index d5c622d1..bd509b64 100644 --- a/src/chord_sheet/song.ts +++ b/src/chord_sheet/song.ts @@ -17,6 +17,7 @@ import Tag, { } from './tag'; import SongBuilder from '../song_builder'; import ChordDefinition from './chord_pro/chord_definition'; +import Chord from '../chord'; type EachItemCallback = (_item: Item) => void; @@ -435,7 +436,11 @@ Or set the song key before changing key: const itemChords = (item as ChordLyricsPair).chords; if (itemChords && itemChords.length > 0) { - chords.add(itemChords); + const parsedChord = Chord.parse(itemChords); + + if (parsedChord) { + chords.add(parsedChord.toString()); + } } }); diff --git a/test/chord_sheet/song.test.ts b/test/chord_sheet/song.test.ts index a9e60704..7f0a9941 100644 --- a/test/chord_sheet/song.test.ts +++ b/test/chord_sheet/song.test.ts @@ -308,15 +308,18 @@ describe('Song', () => { createChordLyricsPair('CM7', 'let'), createChordLyricsPair('', 'it'), createChordLyricsPair('Dm7', ''), + createChordLyricsPair('Dm7 ', ''), ]), createLine([]), createLine([ createChordLyricsPair('F#', 'be'), + createChordLyricsPair('d#', 'be'), + createChordLyricsPair(' F#', 'be'), createChordLyricsPair('', 'changed'), ]), ]); - expect(song.getChords()).toEqual(['CM7', 'Dm7', 'F#']); + expect(song.getChords()).toEqual(['CM7', 'Dm7', 'F#', 'D#']); }); it('returns an empty array if there are no chords in the song', () => {