Skip to content

Commit

Permalink
Merge pull request #33 from kilmc/add-guards-to-extensions
Browse files Browse the repository at this point in the history
[fix] Guard against missing qualities for extensions
  • Loading branch information
kilmc authored Nov 7, 2023
2 parents 0152aac + 609eec2 commit 1833159
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-rockets-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kilmc/music-fns": patch
---

Fix issue with missing qualities in chord extension maps
42 changes: 20 additions & 22 deletions src/converters/chords/chordInfoToBaseIntervals.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ChordQuality } from '../../consts/chords.js';
import { IntervalShorthand } from '../../consts/intervals.js';
import { ChordInfo } from '../../readers/chords/readChord.js';

Expand Down Expand Up @@ -38,46 +39,43 @@ const thirteenthMap: Record<string, IntervalShorthand[]> = {
minor: ['m7', 'M9', 'M13'],
};

const isSupported = (
obj: Record<string, IntervalShorthand[]>,
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

if (type === 'fourth') return ['P4', 'm7', 'm10'];
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];
Expand Down

0 comments on commit 1833159

Please sign in to comment.