Skip to content

Commit

Permalink
feat: improve isPitchClass function to handle multiple sharps and flats
Browse files Browse the repository at this point in the history
  • Loading branch information
Seanitzel committed Nov 12, 2022
1 parent 5a51497 commit 362fc2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/utilities/PureMusicUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
FLAT_CLASS_NOTES, INTERVALS, NOTE_DURATIONS_AS_SIZE_IN_MEASURE, NUMBER_OF_PITCH_CLASSES, PITCH_CLASSES,
PITCH_CLASS_LETTERS,
SHARP_CLASS_NOTES,
} from '../Constants.js';
import { firstToUpper, isNumberAsString, mapString, occurrencesInString } from './GeneralFunctions.js';
Expand Down Expand Up @@ -175,7 +176,10 @@ export function noteToObject(note: Note): NoteAsObject {
* @returns {boolean}
*/
export function isPitchClass(str: string): boolean {
return PITCH_CLASSES.includes(str as RawPitchClass);
const letter = str[0];
if(!PITCH_CLASS_LETTERS.includes(letter as PitchClassLetter)) { return false; }
const accidentals = str.slice(1);
return accidentals.split('').every(accidental => ['b', '#', 'x'].includes(accidental));
}

export function isNote(str: string): boolean {
Expand Down
13 changes: 12 additions & 1 deletion test/utilities/pure-music-utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,23 @@ describe('#PureMusicUtils', () => {
});
});

describe('#isPitchClass', () => {
describe.only('#isPitchClass', () => {
it('should return true when string is a pitch class', () => {
expect(isPitchClass('C')).to.equal(true);
});
it('should return true when containing flats', () => {
expect(isPitchClass('Cb')).to.equal(true);
expect(isPitchClass('Cbb')).to.equal(true);
});
it('should return true when containing sharps', () => {
expect(isPitchClass('C#')).to.equal(true);
expect(isPitchClass('Cx')).to.equal(true);
expect(isPitchClass('Cx#')).to.equal(true);
});
it('should return false when string is note a pitch class', () => {
expect(isPitchClass('NOT PITCH CLASS')).to.equal(false);
expect(isPitchClass('Ce5')).to.equal(false);
expect(isPitchClass('Bbbk')).to.equal(false);
});
});

Expand Down

0 comments on commit 362fc2a

Please sign in to comment.