diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index 226be24f..4bd55395 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -534,6 +534,58 @@ export class Pattern { return this.filterHaps((hap) => hap.whole); } + /** + * Combines adjacent haps with the same value and whole. Only + * intended for use in tests. + */ + defragmentHaps() { + // remove continuous haps + const pat = this.discreteOnly(); + + return pat.withHaps((haps) => { + const result = []; + for (var i=0; i < haps.length; ++i) { + var searching = true; + var a = haps[i]; + while (searching) { + const a_value = JSON.stringify(haps[i].value); + var found = false; + + for(var j=i+1; j { expect(sequence(1, 2).add.squeeze(4, 5).firstCycle()).toStrictEqual(sequence(5, 6, 6, 7).firstCycle()); }); }); + describe('defragmentHaps', () => { + it('Can merge two touching haps with same whole and value', () => { + expect(stack(pure('a').mask(1,0), pure('a').mask(0,1)).defragmentHaps().firstCycle().length) + .toStrictEqual(1); + }); + it('Doesnt merge two overlapping haps', () => { + expect(stack(pure('a').mask(1,1,0), pure('a').mask(0,1)).defragmentHaps().firstCycle().length) + .toStrictEqual(2); + }); + it('Doesnt merge two touching haps with different values', () => { + expect(stack(pure('a').mask(1,0), pure('b').mask(0,1)).defragmentHaps().firstCycle().length) + .toStrictEqual(2); + }); + it('Doesnt merge two touching haps with different wholes', () => { + expect(stack(sequence('a', silence), pure('a').mask(0,1)).defragmentHaps().firstCycle().length) + .toStrictEqual(2); + }); + }); });