Skip to content

Commit

Permalink
Ensure Song#getChords() returns unique chords
Browse files Browse the repository at this point in the history
Parse chords and return the stringified version so whitespace is ignored
and similar chords are grouped.

Related to #1446
  • Loading branch information
martijnversluis committed Dec 4, 2024
1 parent a9f2ae5 commit 3c42d3b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/chord_sheet/song.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}
}
});

Expand Down
5 changes: 4 additions & 1 deletion test/chord_sheet/song.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 3c42d3b

Please sign in to comment.