From a189626e8b4f482d2e6b9abb09fa8f87dd1bef6c Mon Sep 17 00:00:00 2001 From: Alex McLean Date: Tue, 23 Apr 2024 22:37:21 +0100 Subject: [PATCH] Fix stepjoin (#1067) * sort fractions properly - fixes #1066 --- packages/core/pattern.mjs | 2 +- packages/core/test/pattern.test.mjs | 11 ++++++++++- packages/core/util.mjs | 8 +++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index 439050d82..911748575 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -43,7 +43,7 @@ export class Pattern { constructor(query, tactus = undefined) { this.query = query; this._Pattern = true; // this property is used to detect if a pattern that fails instanceof Pattern is an instance of another Pattern - this.__tactus = tactus; // in terms of number of beats per cycle + this.__tactus = Fraction(tactus); // in terms of number of steps per cycle } get tactus() { diff --git a/packages/core/test/pattern.test.mjs b/packages/core/test/pattern.test.mjs index b9848885a..9c17d8be3 100644 --- a/packages/core/test/pattern.test.mjs +++ b/packages/core/test/pattern.test.mjs @@ -1146,7 +1146,7 @@ describe('Pattern', () => { expect(sameFirst(sequence(0, 1, 2, 3, 4).s_taper(-1, 5), sequence(0, 0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4))); }); }); - describe('s_add and s_sub, left', () => { + describe('s_add and s_sub', () => { it('can add from the left', () => { expect(sameFirst(sequence(0, 1, 2, 3, 4).s_add(2), sequence(0, 1))); }); @@ -1159,5 +1159,14 @@ describe('Pattern', () => { it('can sub to the right', () => { expect(sameFirst(sequence(0, 1, 2, 3, 4).s_sub(-2), sequence(2, 3, 4))); }); + it('can subtract nothing', () => { + expect(sameFirst(pure('a').s_sub(0), pure('a'))); + }); + it('can subtract nothing, repeatedly', () => { + expect(sameFirst(pure('a').s_sub(0, 0), fastcat('a', 'a'))); + for (var i = 0; i < 100; ++i) { + expect(sameFirst(pure('a').s_sub(...Array(i).fill(0)), fastcat(...Array(i).fill('a')))); + } + }); }); }); diff --git a/packages/core/util.mjs b/packages/core/util.mjs index e3ec16172..03f8b4104 100644 --- a/packages/core/util.mjs +++ b/packages/core/util.mjs @@ -319,9 +319,11 @@ export function uniqsort(a) { // rational version export function uniqsortr(a) { - return a.sort().filter(function (item, pos, ary) { - return !pos || item.ne(ary[pos - 1]); - }); + return a + .sort((x, y) => x.compare(y)) + .filter(function (item, pos, ary) { + return !pos || item.ne(ary[pos - 1]); + }); } // code hashing helpers