Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tonal: 2 small fixes #1092

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/tonal/test/tonal.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,32 @@ describe('tonal', () => {
.firstCycleValues.map((h) => h.note),
).toEqual(['C3', 'D3', 'E3']);
});
it('scale with n and note values', () => {
expect(
n(0, 1, 2)
.note(3, 4, 0)
.scale('C major')
.firstCycleValues.map((h) => [h.n, h.note]),
).toEqual([
[0, 'F3'],
[1, 'G3'],
[2, 'C3'],
]);
});
it('scale with colon', () => {
expect(
n(0, 1, 2)
.scale('C:major')
.firstCycleValues.map((h) => h.note),
).toEqual(['C3', 'D3', 'E3']);
});
it('scale without tonic', () => {
expect(
n(0, 1, 2)
.scale('major')
.firstCycleValues.map((h) => h.note),
).toEqual(['C3', 'D3', 'E3']);
});
it('scale with mininotation colon', () => {
expect(
n(0, 1, 2)
Expand Down
11 changes: 8 additions & 3 deletions packages/tonal/tonal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function scaleStep(step, scale) {
scale = scale.replaceAll(':', ' ');
step = Math.ceil(step);
let { intervals, tonic, empty } = Scale.get(scale);
if ((empty && isNote(scale)) || (!empty && !tonic)) {
if ((empty && isNote(scale)) || (empty && !tonic)) {
throw new Error(`incomplete scale. Make sure to use ":" instead of spaces, example: .scale("C:major")`);
} else if (empty) {
throw new Error(`invalid scale "${scale}"`);
Expand Down Expand Up @@ -199,9 +199,14 @@ export const scale = register(
pat
.fmap((value) => {
const isObject = typeof value === 'object';
let step = isObject ? value.n : value;
let step = value;
if (isObject) {
delete value.n; // remove n so it won't cause trouble
if (typeof value.note !== 'undefined') {
step = value.note;
} else {
step = value.n;
delete value.n; // remove n so it won't cause trouble
}
}
if (isNote(step)) {
// legacy..
Expand Down
Loading