File tree 2 files changed +46
-5
lines changed
2 files changed +46
-5
lines changed Original file line number Diff line number Diff line change @@ -343,7 +343,14 @@ class Song extends MetadataAccessors {
343
343
* @returns {Song } the changed song
344
344
*/
345
345
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 ;
347
354
}
348
355
349
356
/**
@@ -373,10 +380,14 @@ class Song extends MetadataAccessors {
373
380
return this . mapChordLyricsPairs ( ( pair ) => pair . changeChord ( func ) ) ;
374
381
}
375
382
383
+ get currentKey ( ) : Key | null {
384
+ return Key . wrap ( this . key ) ;
385
+ }
386
+
376
387
requireCurrentKey ( ) : Key {
377
- const wrappedKey = Key . wrap ( this . key ) ;
388
+ const { currentKey } = this ;
378
389
379
- if ( ! wrappedKey ) {
390
+ if ( ! currentKey ) {
380
391
throw new Error ( `
381
392
Cannot change song key, the original key is unknown.
382
393
@@ -387,7 +398,7 @@ Or set the song key before changing key:
387
398
\`song.setKey('C');\`` . substring ( 1 ) ) ;
388
399
}
389
400
390
- return wrappedKey ;
401
+ return currentKey ;
391
402
}
392
403
393
404
/**
Original file line number Diff line number Diff line change 1
1
import { ChordLyricsPair , ChordSheetSerializer , Tag } from '../../src' ;
2
2
3
3
import {
4
+ chordLyricsPair ,
4
5
createChordDefinition ,
5
6
createChordLyricsPair ,
6
7
createLine ,
7
- createSong , createTag ,
8
+ createSong ,
9
+ createSongFromAst ,
10
+ createTag ,
11
+ tag ,
8
12
} from '../utilities' ;
9
13
10
14
import { exampleSongSolfege , exampleSongSymbol } from '../fixtures/song' ;
@@ -481,4 +485,30 @@ describe('Song', () => {
481
485
expect ( ( normalizedSong . paragraphs [ 0 ] . lines [ 0 ] . items [ 0 ] as ChordLyricsPair ) . chords ) . toEqual ( 'E(9)' ) ;
482
486
} ) ;
483
487
} ) ;
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
+ } ) ;
484
514
} ) ;
You can’t perform that action at this time.
0 commit comments