From 780a6482e5a4832420863c098ddb55c0f5c5a0a2 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 25 Jan 2022 10:19:27 +0000 Subject: [PATCH] twiddles --- js/strudel.mjs | 41 ++++++++++++++++++++++++++++++----------- test/pattern.test.mjs | 22 +++++++++++++++++++--- 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/js/strudel.mjs b/js/strudel.mjs index c9d0344c..56cb045b 100644 --- a/js/strudel.mjs +++ b/js/strudel.mjs @@ -134,11 +134,25 @@ class Hap { return new Hap(this.whole, this.part, func(this.value)) } - has_onset() { + hasOnset() { // Test whether the event contains the onset, i.e that // the beginning of the part is the same as that of the whole timespan.""" return (this.whole != undefined) && (this.whole.begin.equals(this.part.begin)) } + + spanEquals(other) { + return((this.whole === undefined && other.whole === undefined) + || this.whole.equals(other.whole) + ) + } + + equals(other) { + return(this.spanEquals(other) + && this.part.equals(other.part) + // TODO would == be better ?? + && this.value === other.value + ) + } } class Pattern { @@ -146,8 +160,6 @@ class Pattern { this.query = query } - - splitQueries() { // Splits queries at cycle boundaries. This makes some calculations // easier to express, as all events are then constrained to happen within @@ -157,14 +169,20 @@ class Pattern { } return new Pattern(query) } -// def with_query_span(self, func): -// """ Returns a new pattern, with the function applied to the timespan of the query. """ -// return Pattern(lambda span: self.query(func(span))) -// def with_query_time(self, func): -// """ Returns a new pattern, with the function applied to both the begin -// and end of the the query timespan. """ -// return Pattern(lambda span: self.query(span.with_time(func))) + withQuerySpan(func) { + return new Pattern(span => this.query(func(span))) + } + + withQueryTime(func) { + // Returns a new pattern, with the function applied to both the begin + // and end of the the query timespan + return new Pattern(span => this.query(span.withTime(func))) + } + + //withEventSpan(func) { + + //} // def with_event_span(self, func): // """ Returns a new pattern, with the function applied to each event @@ -430,4 +448,5 @@ function pure(value) { return new Pattern(query) } -export {TimeSpan, Hap, Pattern, pure, Fraction} \ No newline at end of file +export {TimeSpan, Hap, Pattern, pure, Fraction} + diff --git a/test/pattern.test.mjs b/test/pattern.test.mjs index 5cd8e88d..b0372f01 100644 --- a/test/pattern.test.mjs +++ b/test/pattern.test.mjs @@ -5,7 +5,7 @@ import { strict as assert } from 'assert'; import {TimeSpan, Hap, Pattern, pure} from "../js/strudel.mjs"; describe('TimeSpan', function() { - describe('equal()', function() { + describe('equals()', function() { it('Should be equal to the same value', function() { assert.equal((new TimeSpan(0,4)).equals(new TimeSpan(0,4)), true); }); @@ -18,11 +18,27 @@ describe('TimeSpan', function() { }); describe('Hap', function() { - describe('has_onset()', function() { + describe('hasOnset()', function() { it('True if part includes onset from whole', function() { - assert.equal(new Hap(new TimeSpan(0,1), new TimeSpan(0,1), "thing").has_onset(), true); + assert.equal(new Hap(new TimeSpan(0,1), new TimeSpan(0,1), "thing").hasOnset(), true); }); }); + var a = new Hap(new TimeSpan(Fraction(0), Fraction(0.5)), new TimeSpan(Fraction(0), Fraction(0.5)), "a") + var b = new Hap(new TimeSpan(Fraction(0), Fraction(0.5)), new TimeSpan(Fraction(0), Fraction(0.5)), "b") + var c = new Hap(new TimeSpan(Fraction(0), Fraction(0.25)), new TimeSpan(Fraction(0), Fraction(0.5)), "c") + var d = new Hap(undefined, new TimeSpan(Fraction(0), Fraction(0.5)), "d") + var e = new Hap(undefined, new TimeSpan(Fraction(0), Fraction(0.5)), "e") + describe('spanEquals', function() { + it('True if two haps have the same whole and part', function() { + assert.equal(a.spanEquals(b), true) + }) + it('False if two haps don\'t the same whole and part', function() { + assert.equal(a.spanEquals(c), false) + }) + it('True if two haps have the same part and undefined wholes', function() { + assert.equal(d.spanEquals(e), true) + }) + }) }); describe('Pattern', function() {