Skip to content

Commit

Permalink
Merge pull request #25 from kilmc/add-more-functionality
Browse files Browse the repository at this point in the history
[feat] Add scale types
  • Loading branch information
kilmc authored Nov 3, 2023
2 parents ad07676 + 3fea6a5 commit 27b8ab4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-trains-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kilmc/music-fns": minor
---

Add scale types
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export {
melodicMinorModalKeyNames,
} from './public/consts.js';

export { scaleTypes } from './consts/scales.js';

// Types
// ----------------------------------------------------------------------------

Expand All @@ -24,3 +26,4 @@ export type { Scale } from './public/getScale/getScale.js';
export type { ScaleInfo } from './public/readScale.js';
export type { Note } from './consts/notes.js';
export type { TransposedNote } from './public/transposeNote.js';
export type { ScaleType } from './consts/scales.js';
1 change: 1 addition & 0 deletions src/readers/chords/isSupportedChord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ describe('isSupportedChord', () => {
'Cdimm',
'F#augg',
'F#mm',
'Caug19',
];
it.each(invalidChords)('considers %s invalid', (chordName) => {
expect(isSupportedChord(chordName)).toBe(false);
Expand Down
42 changes: 27 additions & 15 deletions src/readers/chords/isSupportedChord.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
import { chordQualityRegex, noteRegex } from '../../consts/regexes.js';

const isValidAddChord = (input: string) => /add(2|4|9|11|13|)$/.test(input);
const isValidSusChord = (input: string) => /sus(2|4|9|)$/.test(input);
const isValidAlteredNoteChord = (input: string) =>
const validAddChord = (input: string) => /add(2|4|9|11|13|)$/.test(input);
const validSusChord = (input: string) => /sus(2|4|9|)$/.test(input);
const validAlteredNoteChord = (input: string) =>
/(b|#)(4|5|6|7|9|11|13)/.test(input);
const validExtendedNoSymbol = (input: string) =>
new RegExp(`${noteRegex.source}(2|4|5|6|7|9|11|13)`).test(input);
const validExtendedWithSymbol = (input: string) =>
new RegExp(
`${noteRegex.source}(${chordQualityRegex.source})(2|4|5|6|7|9|11|13)$`
).test(input);

const validQualityOnlyChord = (input: string) =>
new RegExp(`^${noteRegex.source}(${chordQualityRegex.source})$`).test(input);
const validQualityChord = (input: string) =>
new RegExp(`^${noteRegex.source}(${chordQualityRegex.source})`).test(input);

export const isSupportedChord = (input: string) => {
const hasQualityRegex = new RegExp(`${chordQualityRegex}`);

if (/\d+/.test(input)) {
if (/(b|#)\d+/.test(input)) return isValidAlteredNoteChord(input);
if (/add/.test(input)) return isValidAddChord(input);
if (/sus/.test(input)) return isValidSusChord(input);
if (/(b|#)\d+/.test(input)) return validAlteredNoteChord(input);
if (/add/.test(input)) return validAddChord(input);
if (/sus/.test(input)) return validSusChord(input);
if (chordQualityRegex.test(input)) {
if (validExtendedNoSymbol(input)) return true;
if (/(mmaj7|mM7|mΔ7|-Δ7)/.test(input)) return noteRegex.test(input);
if (validQualityChord(input)) return validExtendedWithSymbol(input);

return /(2|4|5|6|7|9|11|13)/.test(input);
return validQualityChord(input);
}
return validExtendedNoSymbol(input);
}

if (/add/.test(input)) return isValidAddChord(input);
if (/sus/.test(input)) return isValidSusChord(input);
if (/add/.test(input)) return validAddChord(input);
if (/sus/.test(input)) return validSusChord(input);

if (hasQualityRegex.test(input)) {
const isValidHasQuality = new RegExp(
`^${noteRegex.source}(${chordQualityRegex.source})$`
);
return isValidHasQuality.test(input);
}
if (hasQualityRegex.test(input)) return validQualityOnlyChord(input);

const majorRegex = new RegExp(`^${noteRegex.source}$`);
return majorRegex.test(input);
Expand Down

0 comments on commit 27b8ab4

Please sign in to comment.