From de15d79edf39fea50159ec990c268c8aa56fcde3 Mon Sep 17 00:00:00 2001 From: Aria Date: Tue, 5 Aug 2025 22:29:59 -0500 Subject: [PATCH] Add doscstrings and move stringifyValues into its own function for reuse --- packages/core/hap.mjs | 9 ++------- packages/core/pattern.mjs | 19 ++++++++++++++++++- packages/core/test/pattern.test.mjs | 4 ++-- packages/core/util.mjs | 10 ++++++++++ 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/packages/core/hap.mjs b/packages/core/hap.mjs index a6e3c55ad..5f820d644 100644 --- a/packages/core/hap.mjs +++ b/packages/core/hap.mjs @@ -4,6 +4,7 @@ Copyright (C) 2022 Strudel contributors - see . */ import Fraction from './fraction.mjs'; +import { stringifyValues } from './util.mjs'; export class Hap { /* @@ -148,13 +149,7 @@ export class Hap { } showWhole(compact = false) { - return `${this.whole == undefined ? '~' : this.whole.show()}: ${ - typeof this.value === 'object' - ? compact - ? JSON.stringify(this.value).slice(1, -1).replaceAll('"', '').replaceAll(',', ' ') - : JSON.stringify(this.value) - : this.value - }`; + return `${this.whole == undefined ? '~' : this.whole.show()}: ${stringifyValues(this.value, compact)}`; } combineContext(b) { diff --git a/packages/core/pattern.mjs b/packages/core/pattern.mjs index d724bb853..f64761f4f 100644 --- a/packages/core/pattern.mjs +++ b/packages/core/pattern.mjs @@ -21,6 +21,7 @@ import { numeralArgs, parseNumeral, pairs, + stringifyValues, } from './util.mjs'; import drawLine from './drawLine.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 })) { return this.onTrigger((...args) => { logger(func(...args), undefined, getData(...args)); }, 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)); } diff --git a/packages/core/test/pattern.test.mjs b/packages/core/test/pattern.test.mjs index b8e7de504..b82d39b9d 100644 --- a/packages/core/test/pattern.test.mjs +++ b/packages/core/test/pattern.test.mjs @@ -1329,7 +1329,7 @@ describe('Pattern', () => { describe('logValues', () => { it('logs values to console', () => { 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); // Force a trigger @@ -1338,7 +1338,7 @@ describe('Pattern', () => { }); 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', ); mockConsoleLog.mockRestore(); diff --git a/packages/core/util.mjs b/packages/core/util.mjs index 756fac8e8..2e7c6e026 100644 --- a/packages/core/util.mjs +++ b/packages/core/util.mjs @@ -487,3 +487,13 @@ export function getCurrentKeyboardState() { // } // 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; +}