Skip to content

Commit 85589b5

Browse files
committed
chore: remove circular dependencies
1 parent ca4ba91 commit 85589b5

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

docs/js/docma-web.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/core/Note.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { Sharp, Sharps, Flats, Enharmonics, DiatonicNote, DiatonicNotes, NoteSym
1515
import { isDefined, isNumber, isUndefined } from '../utils/types-guards';
1616
import { PlayaError } from '../utils/error';
1717
import { Interval, Semitones } from '../constants';
18-
import { fromSemitones } from '../tools/interval';
1918

2019
export type NoteLike = string | number | Note | NoteSymbol;
2120

@@ -515,9 +514,15 @@ export class Note {
515514
const base = findCMidiByOctave(octave);
516515
const c = new Note('C', base);
517516

518-
const interval = fromSemitones(midi - base)?.[0] as Interval;
517+
const semitones = midi - base;
518+
let interval: Interval | undefined;
519519

520-
return Note.transposeUp(c, interval);
520+
for (const [interv, semit] of Object.entries(Semitones)) {
521+
if (semit === semitones) {
522+
interval = interv as Interval;
523+
}
524+
}
525+
return Note.transposeUp(c, interval!);
521526
}
522527

523528
/**
@@ -536,6 +541,19 @@ export class Note {
536541
return Array.from({ length: h.midi - l.midi }).map((_, i) => new Note(l.midi + i).ensureType(NoteType.Sharp));
537542
}
538543

544+
static isNoteLike = (note: unknown): note is NoteLike => {
545+
try {
546+
new Note(<NoteLike>note);
547+
548+
return true;
549+
} catch {
550+
return false;
551+
}
552+
};
553+
554+
static isNoteSymbol = (note: NoteLike): note is NoteSymbol =>
555+
!(note instanceof Note) && typeof note !== 'number' && !isPitch(note);
556+
539557
/**
540558
* Tries to find the enharmonic of the note
541559
* @private

lib/tools/distance.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Note, NoteLike } from '../core/Note';
22
import { DiatonicNotes, DiatonicNote } from '../constants';
3-
import { isNoteSymbol } from '../utils/note';
43

54
/**
65
* Note distance functions
@@ -48,7 +47,7 @@ export const semitones = (a: NoteLike, b: NoteLike): number => {
4847
posB = 12 + (posB % 12);
4948
}
5049

51-
if (isNoteSymbol(a) && isNoteSymbol(b)) {
50+
if (Note.isNoteSymbol(a) && Note.isNoteSymbol(b)) {
5251
posA = Note.position(noteA);
5352
posB = Note.position(noteB);
5453
}

lib/tools/interval.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as R from 'ramda';
22
import { Interval, Semitones } from '../constants';
33
import { Note, NoteLike } from '../core/Note';
4-
import { isNoteLike } from '../utils/note';
54
import { isNotNull } from '../utils/types-guards';
65
import { naturalPosition, semitones } from './distance';
76

@@ -214,7 +213,7 @@ export const between = (a: NoteLike, b: NoteLike): Interval | null => {
214213
* @return {string}
215214
*/
216215
export const detect = (notes: string): Interval[] | null => {
217-
const arr = notes.split(' ').filter((note) => isNoteLike(note)) as NoteLike[];
216+
const arr = notes.split(' ').filter((note) => Note.isNoteLike(note)) as NoteLike[];
218217

219218
if (arr.some((note) => !note)) {
220219
return [];

lib/utils/note.ts

-12
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,6 @@ export const stripOctave = (note: string): NoteSymbol => <NoteSymbol>note.replac
3636

3737
export const isPitch = (note: Exclude<NoteLike, Note | number>): boolean => new RegExp(OCTAVE_REGEXP).test(note);
3838

39-
export const isNoteSymbol = (note: NoteLike): note is NoteSymbol =>
40-
!(note instanceof Note) && typeof note !== 'number' && !isPitch(note);
41-
42-
export const isNoteLike = (note: unknown): note is NoteLike => {
43-
try {
44-
new Note(<NoteLike>note);
45-
46-
return true;
47-
} catch {
48-
return false;
49-
}
50-
};
5139

5240
interface ParsedNote {
5341
note: NoteSymbol;

0 commit comments

Comments
 (0)