From 9a2e76ba4fb441aee94f9cbebb36317b8d069281 Mon Sep 17 00:00:00 2001 From: c0dev0id Date: Mon, 6 Jul 2026 21:09:36 +0200 Subject: [PATCH] refactor: extract coerce and stressorToString into util.js --- static/ast-parser.js | 12 ++---------- static/components/PaneFO.js | 3 +-- static/exporter.js | 7 +------ static/util.js | 13 +++++++++++++ test-parser.mjs | 3 ++- 5 files changed, 19 insertions(+), 19 deletions(-) create mode 100644 static/util.js diff --git a/static/ast-parser.js b/static/ast-parser.js index e85bd8e..2327832 100644 --- a/static/ast-parser.js +++ b/static/ast-parser.js @@ -1,16 +1,8 @@ +import { coerce } from './util.js'; + const LINE_RE = /^(\d{2}) (\S+(?:\.\S+)*) ?(.*)/; const DEBUG_RE = /^\d{2} # DEBUG/; - -export function coerce(s) { - if (s === 'True' || s === 'Y' || s === 'on' || s === 'true') return true; - if (s === 'False' || s === 'N' || s === 'off' || s === 'false') return false; - if (s === '') return s; - const n = Number(s); - if (!isNaN(n)) return n; - return s; -} - export function parseAstLog(text) { const root = { slot: 'root', parentSlot: null, depth: -1, positionals: [], props: {}, children: [] }; const stack = [root]; diff --git a/static/components/PaneFO.js b/static/components/PaneFO.js index ea9fd61..b069826 100644 --- a/static/components/PaneFO.js +++ b/static/components/PaneFO.js @@ -3,8 +3,7 @@ import { ObjectExtended } from './ObjectExtended.js'; import { EnvelopeEditor } from './EnvelopeEditor.js'; import { ShapeEditor } from './ShapeEditor.js'; import { LinkedInstrumentModal } from './LinkedInstrumentModal.js'; -import { stressorToString } from '../exporter.js'; -import { coerce } from '../ast-parser.js'; +import { coerce, stressorToString } from '../util.js'; const H4 = { style: 'margin:0 0 0.5rem' }; diff --git a/static/exporter.js b/static/exporter.js index 38f73ca..fae3558 100644 --- a/static/exporter.js +++ b/static/exporter.js @@ -1,5 +1,4 @@ -// RFC-compliant YAML serializer for Sompyler instrument blocks. -// Operates on the model produced by ast-parser.js buildModel(). +import { stressorToString } from './util.js'; // ── Shape ────────────────────────────────────────────────────────────────── // RFC §1.3.4.5: SHAPE = [PREFIX (":" / ";")] Node 1*(";" Node) @@ -241,10 +240,6 @@ function patchInstrumentHeader(text, instruments) { return result.join('\n'); } -export function stressorToString(s) { - if (!s?.groups?.length) return ''; - return s.groups.map(g => g.join(',')).join(';'); -} function patchBarMeta(doc, bar) { const props = []; diff --git a/static/util.js b/static/util.js new file mode 100644 index 0000000..5696f74 --- /dev/null +++ b/static/util.js @@ -0,0 +1,13 @@ +export function coerce(s) { + if (s === 'True' || s === 'Y' || s === 'on' || s === 'true') return true; + if (s === 'False' || s === 'N' || s === 'off' || s === 'false') return false; + if (s === '') return s; + const n = Number(s); + if (!isNaN(n)) return n; + return s; +} + +export function stressorToString(s) { + if (!s?.groups?.length) return ''; + return s.groups.map(g => g.join(',')).join(';'); +} diff --git a/test-parser.mjs b/test-parser.mjs index 599e694..9b35550 100644 --- a/test-parser.mjs +++ b/test-parser.mjs @@ -4,7 +4,8 @@ import { readFileSync } from 'fs'; import { parseAstLog, buildModel } from './static/ast-parser.js'; -import { exportInstrument, patchScore, stressorToString } from './static/exporter.js'; +import { exportInstrument, patchScore } from './static/exporter.js'; +import { stressorToString } from './static/util.js'; const FIXTURE = new URL('./fixtures/ast.log', import.meta.url); const text = readFileSync(FIXTURE, 'utf8');