Add doscstrings and move stringifyValues into its own function for reuse

This commit is contained in:
Aria 2025-08-05 22:29:59 -05:00
parent 23e5358ae4
commit de15d79edf
4 changed files with 32 additions and 10 deletions

View file

@ -4,6 +4,7 @@ Copyright (C) 2022 Strudel contributors - see <https://codeberg.org/uzu/strudel/
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import Fraction from './fraction.mjs'; import Fraction from './fraction.mjs';
import { stringifyValues } from './util.mjs';
export class Hap { export class Hap {
/* /*
@ -148,13 +149,7 @@ export class Hap {
} }
showWhole(compact = false) { showWhole(compact = false) {
return `${this.whole == undefined ? '~' : this.whole.show()}: ${ return `${this.whole == undefined ? '~' : this.whole.show()}: ${stringifyValues(this.value, compact)}`;
typeof this.value === 'object'
? compact
? JSON.stringify(this.value).slice(1, -1).replaceAll('"', '').replaceAll(',', ' ')
: JSON.stringify(this.value)
: this.value
}`;
} }
combineContext(b) { combineContext(b) {

View file

@ -21,6 +21,7 @@ import {
numeralArgs, numeralArgs,
parseNumeral, parseNumeral,
pairs, pairs,
stringifyValues,
} from './util.mjs'; } from './util.mjs';
import drawLine from './drawLine.mjs'; import drawLine from './drawLine.mjs';
import { logger } from './logger.mjs'; import { logger } from './logger.mjs';
@ -852,13 +853,29 @@ export class Pattern {
); );
} }
/**
* Writes the content of the current event to the console, which is visible in the side menu
* or as the developer console.
* @name log
* @memberof Pattern
* @example
* s("bd sd").log()
*/
log(func = (hap) => `[hap] ${hap.showWhole(true)}`, getData = (hap) => ({ hap })) { log(func = (hap) => `[hap] ${hap.showWhole(true)}`, getData = (hap) => ({ hap })) {
return this.onTrigger((...args) => { return this.onTrigger((...args) => {
logger(func(...args), undefined, getData(...args)); logger(func(...args), undefined, getData(...args));
}, false); }, false);
} }
logValues(func = id) { /**
* A simplified version of `log` which writes all "values" (various configurable parameters)
* within the event to the console, which is visible in the side menu or as the developer console.
* @name logValues
* @memberof Pattern
* @example
* s("bd sd").gain("0.25 0.5 1").n("2 1 0").logValues()
*/
logValues(func = (value) => `${stringifyValues(value, true)}`) {
return this.log((hap) => func(hap.value)); return this.log((hap) => func(hap.value));
} }

View file

@ -1329,7 +1329,7 @@ describe('Pattern', () => {
describe('logValues', () => { describe('logValues', () => {
it('logs values to console', () => { it('logs values to console', () => {
const mockConsoleLog = vi.spyOn(console, 'log').mockImplementation(() => {}); const mockConsoleLog = vi.spyOn(console, 'log').mockImplementation(() => {});
const pattern = pure('a').note('c#').log(); const pattern = pure('a').note('c#').logValues();
const haps = pattern.queryArc(0, 1); const haps = pattern.queryArc(0, 1);
// Force a trigger // Force a trigger
@ -1338,7 +1338,7 @@ describe('Pattern', () => {
}); });
expect(mockConsoleLog).toHaveBeenCalledWith( expect(mockConsoleLog).toHaveBeenCalledWith(
'%c[hap] 0/1 → 1/1: value:a note:c#', '%cvalue:a note:c#',
'background-color: black;color:white;border-radius:15px', 'background-color: black;color:white;border-radius:15px',
); );
mockConsoleLog.mockRestore(); mockConsoleLog.mockRestore();

View file

@ -487,3 +487,13 @@ export function getCurrentKeyboardState() {
// } // }
// return lcm((x * y) / gcd(x, y), ...z); // return lcm((x * y) / gcd(x, y), ...z);
// }; // };
// Takes values -- typically derived from events, i.e. `hap`s -- and renders them
// into a readable format
export function stringifyValues(value, compact = false) {
return typeof value === 'object'
? compact
? JSON.stringify(value).slice(1, -1).replaceAll('"', '').replaceAll(',', ' ')
: JSON.stringify(value)
: value;
}