From 9898c67ea096f34f34b650ba7bde2931d9fa9419 Mon Sep 17 00:00:00 2001 From: Kilian McMahon Date: Wed, 8 Nov 2023 00:06:11 +0100 Subject: [PATCH 1/2] fix: guard against missing qualities for extensions --- .../chords/chordInfoToBaseIntervals.ts | 42 +++++++++---------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/converters/chords/chordInfoToBaseIntervals.ts b/src/converters/chords/chordInfoToBaseIntervals.ts index 810e79c..cd61802 100644 --- a/src/converters/chords/chordInfoToBaseIntervals.ts +++ b/src/converters/chords/chordInfoToBaseIntervals.ts @@ -1,3 +1,4 @@ +import { ChordQuality } from '../../consts/chords.js'; import { IntervalShorthand } from '../../consts/intervals.js'; import { ChordInfo } from '../../readers/chords/readChord.js'; @@ -38,10 +39,19 @@ const thirteenthMap: Record = { minor: ['m7', 'M9', 'M13'], }; +const isSupported = ( + obj: Record, + quality: ChordQuality +) => { + return obj[quality] !== undefined; +}; + export const chordInfoToBaseIntervals = ( chordInfo: ChordInfo ): IntervalShorthand[] => { - const { type, quality } = chordInfo; + const { type, quality: rawQuality } = chordInfo; + + const quality = rawQuality === undefined ? 'dominant' : rawQuality; // second handled in createAddNotes as they're aliases @@ -49,35 +59,23 @@ export const chordInfoToBaseIntervals = ( if (type === 'fifth') return ['P5', 'P8']; if (type === 'sixth') { - return [...qualityMap[quality === undefined ? 'dominant' : quality], 'M6']; + return [...qualityMap[quality], 'M6']; } - if (type === 'seventh') { - return [ - ...qualityMap[quality === undefined ? 'dominant' : quality], - ...seventhMap[quality === undefined ? 'dominant' : quality], - ]; + if (type === 'seventh' && isSupported(seventhMap, quality)) { + return [...qualityMap[quality], ...seventhMap[quality]]; } - if (type === 'ninth') { - return [ - ...qualityMap[quality === undefined ? 'dominant' : quality], - ...ninthMap[quality === undefined ? 'dominant' : quality], - ]; + if (type === 'ninth' && isSupported(ninthMap, quality)) { + return [...qualityMap[quality], ...ninthMap[quality]]; } - if (type === 'eleventh') { - return [ - ...qualityMap[quality === undefined ? 'dominant' : quality], - ...eleventhMap[quality === undefined ? 'dominant' : quality], - ]; + if (type === 'eleventh' && isSupported(eleventhMap, quality)) { + return [...qualityMap[quality], ...eleventhMap[quality]]; } - if (type === 'thirteenth') { - return [ - ...qualityMap[quality === undefined ? 'dominant' : quality], - ...thirteenthMap[quality === undefined ? 'dominant' : quality], - ]; + if (type === 'thirteenth' && isSupported(thirteenthMap, quality)) { + return [...qualityMap[quality], ...thirteenthMap[quality]]; } if (quality === 'major') return qualityMap[quality]; From 609eec24ee576cbf25d83064b6ae97f6e3b83996 Mon Sep 17 00:00:00 2001 From: Kilian McMahon Date: Wed, 8 Nov 2023 00:11:06 +0100 Subject: [PATCH 2/2] chore: add changeset --- .changeset/short-rockets-press.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/short-rockets-press.md diff --git a/.changeset/short-rockets-press.md b/.changeset/short-rockets-press.md new file mode 100644 index 0000000..e655898 --- /dev/null +++ b/.changeset/short-rockets-press.md @@ -0,0 +1,5 @@ +--- +"@kilmc/music-fns": patch +--- + +Fix issue with missing qualities in chord extension maps