From a5b602250614afce28e71d0f5ea93fe258f554a7 Mon Sep 17 00:00:00 2001 From: Kaspars Date: Tue, 7 May 2024 23:56:39 +0200 Subject: [PATCH 1/5] - fix scale() to allow both n() and note() at the same time - fix scale() to allow scale names without tonic and then default it to C --- packages/tonal/test/tonal.test.mjs | 15 +++++++++++++++ packages/tonal/tonal.mjs | 7 ++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/tonal/test/tonal.test.mjs b/packages/tonal/test/tonal.test.mjs index 8dd0e1861..968534c80 100644 --- a/packages/tonal/test/tonal.test.mjs +++ b/packages/tonal/test/tonal.test.mjs @@ -30,6 +30,14 @@ 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, 5) + .scale('C major') + .firstCycleValues.map((h) => h.note), + ).toEqual(['F3', 'G3', 'A3']); + }); it('scale with colon', () => { expect( n(0, 1, 2) @@ -37,6 +45,13 @@ describe('tonal', () => { .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) diff --git a/packages/tonal/tonal.mjs b/packages/tonal/tonal.mjs index 4c25d23ea..012a0fc20 100644 --- a/packages/tonal/tonal.mjs +++ b/packages/tonal/tonal.mjs @@ -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}"`); @@ -199,10 +199,7 @@ export const scale = register( pat .fmap((value) => { const isObject = typeof value === 'object'; - let step = isObject ? value.n : value; - if (isObject) { - delete value.n; // remove n so it won't cause trouble - } + let step = isObject ? (value.note ?? value.n) : value; if (isNote(step)) { // legacy.. return pure(step); From 4e6d39666a7474f44e28c8e56b9c4f719f88b198 Mon Sep 17 00:00:00 2001 From: Kaspars Date: Wed, 8 May 2024 00:00:58 +0200 Subject: [PATCH 2/5] codeformat --- packages/tonal/test/tonal.test.mjs | 14 +++++++------- packages/tonal/tonal.mjs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/tonal/test/tonal.test.mjs b/packages/tonal/test/tonal.test.mjs index 968534c80..647fed807 100644 --- a/packages/tonal/test/tonal.test.mjs +++ b/packages/tonal/test/tonal.test.mjs @@ -32,10 +32,10 @@ describe('tonal', () => { }); it('scale with n and note values', () => { expect( - n(0, 1, 2) - .note(3, 4, 5) - .scale('C major') - .firstCycleValues.map((h) => h.note), + n(0, 1, 2) + .note(3, 4, 5) + .scale('C major') + .firstCycleValues.map((h) => h.note), ).toEqual(['F3', 'G3', 'A3']); }); it('scale with colon', () => { @@ -47,9 +47,9 @@ describe('tonal', () => { }); it('scale without tonic', () => { expect( - n(0, 1, 2) - .scale('major') - .firstCycleValues.map((h) => h.note), + n(0, 1, 2) + .scale('major') + .firstCycleValues.map((h) => h.note), ).toEqual(['C3', 'D3', 'E3']); }); it('scale with mininotation colon', () => { diff --git a/packages/tonal/tonal.mjs b/packages/tonal/tonal.mjs index 012a0fc20..29691d192 100644 --- a/packages/tonal/tonal.mjs +++ b/packages/tonal/tonal.mjs @@ -199,7 +199,7 @@ export const scale = register( pat .fmap((value) => { const isObject = typeof value === 'object'; - let step = isObject ? (value.note ?? value.n) : value; + let step = isObject ? value.note ?? value.n : value; if (isNote(step)) { // legacy.. return pure(step); From 33a73c37bc5caf29c7e1f04dbe26501b5727d5f9 Mon Sep 17 00:00:00 2001 From: Kaspars Date: Wed, 8 May 2024 00:46:27 +0200 Subject: [PATCH 3/5] fix tests --- packages/tonal/tonal.mjs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/tonal/tonal.mjs b/packages/tonal/tonal.mjs index 29691d192..acf35a596 100644 --- a/packages/tonal/tonal.mjs +++ b/packages/tonal/tonal.mjs @@ -199,7 +199,15 @@ export const scale = register( pat .fmap((value) => { const isObject = typeof value === 'object'; - let step = isObject ? value.note ?? value.n : value; + let step = value; + if (isObject) { + if (value.note) { + step = value.note; + } else { + step = value.n; + delete value.n; // remove n so it won't cause trouble + } + } if (isNote(step)) { // legacy.. return pure(step); From a4f10d533947f0e6b735e3cfaa40e87439d7e4ee Mon Sep 17 00:00:00 2001 From: Kaspars Date: Wed, 8 May 2024 10:38:06 +0200 Subject: [PATCH 4/5] bug fix --- packages/tonal/test/tonal.test.mjs | 6 +++--- packages/tonal/tonal.mjs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/tonal/test/tonal.test.mjs b/packages/tonal/test/tonal.test.mjs index 647fed807..73c4de151 100644 --- a/packages/tonal/test/tonal.test.mjs +++ b/packages/tonal/test/tonal.test.mjs @@ -33,10 +33,10 @@ describe('tonal', () => { it('scale with n and note values', () => { expect( n(0, 1, 2) - .note(3, 4, 5) + .note(3, 4, 0) .scale('C major') - .firstCycleValues.map((h) => h.note), - ).toEqual(['F3', 'G3', 'A3']); + .firstCycleValues.map((h) => [h.n, h.note]), + ).toEqual([[0, 'F3'], [1, 'G3'], [2, 'C3']]); }); it('scale with colon', () => { expect( diff --git a/packages/tonal/tonal.mjs b/packages/tonal/tonal.mjs index acf35a596..dd723c2da 100644 --- a/packages/tonal/tonal.mjs +++ b/packages/tonal/tonal.mjs @@ -201,7 +201,7 @@ export const scale = register( const isObject = typeof value === 'object'; let step = value; if (isObject) { - if (value.note) { + if (typeof value.note !== 'undefined') { step = value.note; } else { step = value.n; From 8d8b8ab99ed1a18c7b43c6368c1aefcb65988f0e Mon Sep 17 00:00:00 2001 From: Kaspars Date: Wed, 8 May 2024 10:42:06 +0200 Subject: [PATCH 5/5] codeformat --- packages/tonal/test/tonal.test.mjs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/tonal/test/tonal.test.mjs b/packages/tonal/test/tonal.test.mjs index 73c4de151..755b16324 100644 --- a/packages/tonal/test/tonal.test.mjs +++ b/packages/tonal/test/tonal.test.mjs @@ -36,7 +36,11 @@ describe('tonal', () => { .note(3, 4, 0) .scale('C major') .firstCycleValues.map((h) => [h.n, h.note]), - ).toEqual([[0, 'F3'], [1, 'G3'], [2, 'C3']]); + ).toEqual([ + [0, 'F3'], + [1, 'G3'], + [2, 'C3'], + ]); }); it('scale with colon', () => { expect(