From 4f55144232a0c89c779ea26f52df6203d690de48 Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Fri, 1 Mar 2024 00:31:33 +0100 Subject: [PATCH 1/2] nested controls poc --- packages/core/controls.mjs | 44 ++++++++++++++++------------ packages/core/test/controls.test.mjs | 4 +++ 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/packages/core/controls.mjs b/packages/core/controls.mjs index 671566be..aaa6421b 100644 --- a/packages/core/controls.mjs +++ b/packages/core/controls.mjs @@ -7,26 +7,32 @@ This program is free software: you can redistribute it and/or modify it under th import { Pattern, register, sequence } from './pattern.mjs'; export function createParam(names) { - const name = Array.isArray(names) ? names[0] : names; + let isMulti = Array.isArray(names); + names = !isMulti ? [names] : names; + const name = names[0]; - var withVal; - if (Array.isArray(names)) { - withVal = (xs) => { - if (Array.isArray(xs)) { - const result = {}; - xs.forEach((x, i) => { - if (i < names.length) { - result[names[i]] = x; - } - }); - return result; - } else { - return { [name]: xs }; - } - }; - } else { - withVal = (x) => ({ [name]: x }); - } + const withVal = (xs) => { + let bag; + // check if we have an object with an unnamed control (.value) + if (typeof xs === 'object' && xs.value !== undefined) { + bag = xs; // grab props that are already there + xs = xs.value; // grab the unnamed control for this one + delete bag.value; + } + if (isMulti && Array.isArray(xs)) { + const result = bag || {}; + xs.forEach((x, i) => { + if (i < names.length) { + result[names[i]] = x; + } + }); + return result; + } else if (bag) { + return { ...bag, [name]: xs }; + } else { + return { [name]: xs }; + } + }; const func = (...pats) => sequence(...pats).withValue(withVal); diff --git a/packages/core/test/controls.test.mjs b/packages/core/test/controls.test.mjs index aa66bf98..69d63645 100644 --- a/packages/core/test/controls.test.mjs +++ b/packages/core/test/controls.test.mjs @@ -25,4 +25,8 @@ describe('controls', () => { { s: 'sd', n: 4, gain: 0.5 }, ]); }); + it('should support nested controls', () => { + expect(s(mini('bd').pan(1)).firstCycleValues).toEqual([{ s: 'bd', pan: 1 }]); + expect(s(mini('bd:1').pan(1)).firstCycleValues).toEqual([{ s: 'bd', n: 1, pan: 1 }]); + }); }); From bf343eb499bb1c24cd83242d09a985493fb3b05d Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Fri, 1 Mar 2024 00:32:45 +0100 Subject: [PATCH 2/2] simplify _composeOp --- packages/core/pattern.mjs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index 13ac8cfd..00f20adf 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -896,16 +896,15 @@ addToPrototype('weaveWith', function (t, ...funcs) { ////////////////////////////////////////////////////////////////////// // compose matrix functions -// TODO - adopt value.mjs fully.. +function _nonArrayObject(x) { + return !Array.isArray(x) && typeof x === 'object'; +} function _composeOp(a, b, func) { - function _nonFunctionObject(x) { - return x instanceof Object && !(x instanceof Function); - } - if (_nonFunctionObject(a) || _nonFunctionObject(b)) { - if (!_nonFunctionObject(a)) { + if (_nonArrayObject(a) || _nonArrayObject(b)) { + if (!_nonArrayObject(a)) { a = { value: a }; } - if (!_nonFunctionObject(b)) { + if (!_nonArrayObject(b)) { b = { value: b }; } return unionWithObj(a, b, func);