Skip to content

Commit

Permalink
Ensure Song#getChords() returns unique chords (#1487)
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnversluis authored Dec 4, 2024
1 parent a9f2ae5 commit 723f45f
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 723f45f

Please sign in to comment.