Skip to content

Commit fe1aab9

Browse files
Update song key when changing modifier (#1599)
When changing a song's modifier, the song key should also be changed to make sure chords a correctly rendered.
1 parent 94c4cf8 commit fe1aab9

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/chord_sheet/song.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,14 @@ class Song extends MetadataAccessors {
343343
* @returns {Song} the changed song
344344
*/
345345
useModifier(modifier: Modifier): Song {
346-
return this.mapChordLyricsPairs((pair) => pair.useModifier(modifier));
346+
const { currentKey } = this;
347+
let changedSong = this.mapChordLyricsPairs((pair) => pair.useModifier(modifier));
348+
349+
if (currentKey && currentKey.modifier !== modifier) {
350+
changedSong = changedSong.changeKey(currentKey.useModifier(modifier));
351+
}
352+
353+
return changedSong;
347354
}
348355

349356
/**
@@ -373,10 +380,14 @@ class Song extends MetadataAccessors {
373380
return this.mapChordLyricsPairs((pair) => pair.changeChord(func));
374381
}
375382

383+
get currentKey(): Key | null {
384+
return Key.wrap(this.key);
385+
}
386+
376387
requireCurrentKey(): Key {
377-
const wrappedKey = Key.wrap(this.key);
388+
const { currentKey } = this;
378389

379-
if (!wrappedKey) {
390+
if (!currentKey) {
380391
throw new Error(`
381392
Cannot change song key, the original key is unknown.
382393
@@ -387,7 +398,7 @@ Or set the song key before changing key:
387398
\`song.setKey('C');\``.substring(1));
388399
}
389400

390-
return wrappedKey;
401+
return currentKey;
391402
}
392403

393404
/**

test/chord_sheet/song.test.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { ChordLyricsPair, ChordSheetSerializer, Tag } from '../../src';
22

33
import {
4+
chordLyricsPair,
45
createChordDefinition,
56
createChordLyricsPair,
67
createLine,
7-
createSong, createTag,
8+
createSong,
9+
createSongFromAst,
10+
createTag,
11+
tag,
812
} from '../utilities';
913

1014
import { exampleSongSolfege, exampleSongSymbol } from '../fixtures/song';
@@ -481,4 +485,30 @@ describe('Song', () => {
481485
expect((normalizedSong.paragraphs[0].lines[0].items[0] as ChordLyricsPair).chords).toEqual('E(9)');
482486
});
483487
});
488+
489+
describe('#useModifier', () => {
490+
it('changes the modifier of the chords in a song', () => {
491+
const song = createSongFromAst([
492+
[
493+
chordLyricsPair('G#', 'let it'),
494+
chordLyricsPair('F#', 'it'),
495+
],
496+
]);
497+
498+
const modifiedSong = song.useModifier('b');
499+
500+
expect((modifiedSong.paragraphs[0].lines[0].items[0] as ChordLyricsPair).chords).toEqual('Ab');
501+
expect((modifiedSong.paragraphs[0].lines[0].items[1] as ChordLyricsPair).chords).toEqual('Gb');
502+
});
503+
504+
it('updates the key of the song', () => {
505+
const song = createSongFromAst([
506+
[tag('key', 'F#')],
507+
]);
508+
509+
const modifiedSong = song.useModifier('b');
510+
511+
expect(modifiedSong.key).toEqual('Gb');
512+
});
513+
});
484514
});

0 commit comments

Comments
 (0)