refactor: underscore-prefix all non-exported functions

This commit is contained in:
c0dev0id
2026-07-06 21:14:36 +02:00
parent 3beaa1d2ec
commit 884955195a
4 changed files with 120 additions and 120 deletions

View File

@@ -5,7 +5,7 @@ import { stressorToString } from './util.js';
// Node = x "," y ["*" z] ["!"]
// PREFIX+colon is the duration/resolution; optional START+semicolon follows.
function serializeShape(shape) {
function _serializeShape(shape) {
if (!shape) return null;
const nodes = shape.coords.map(c => {
let s = `${c.x},${c.y}`;
@@ -22,10 +22,10 @@ function serializeShape(shape) {
// ── FM / AM modulation ─────────────────────────────────────────────────────
// RFC §3.2.1.1.6-7: FM = FREQUENCY ["f"/"F"] ["@" OSC] ["[" SHAPE "]"] ";" MOD ":" BASE
function serializeModulation(m) {
function _serializeModulation(m) {
let s = String(m.frequency ?? '');
if (m.oscillator) s += `@${m.oscillator}`;
if (m.shape) s += `[${serializeShape(m.shape)}]`;
if (m.shape) s += `[${_serializeShape(m.shape)}]`;
s += `;${m.mod_share ?? ''}:${m.base_share ?? ''}`;
if (m.init_phase != null) s += m.init_phase >= 0 ? `+${m.init_phase}` : String(m.init_phase);
return s;
@@ -35,18 +35,18 @@ function serializeModulation(m) {
// RFC §3.2.1.1: O, A, S, R, FM go directly in the variation MAPPING.
// Returns array of YAML lines at 0 indent.
function basicPropLines(bp) {
function _basicPropLines(bp) {
if (!bp) return [];
const lines = [];
if (bp.oscillator) lines.push(`O: ${bp.oscillator}`);
const a = serializeShape(bp.A);
const a = _serializeShape(bp.A);
if (a) lines.push(`A: "${a}"`);
const s = serializeShape(bp.S);
const s = _serializeShape(bp.S);
if (s) lines.push(`S: "${s}"`);
const r = serializeShape(bp.R);
const r = _serializeShape(bp.R);
if (r) lines.push(`R: "${r}"`);
for (const fm of (bp.fmModulations ?? [])) lines.push(`FM: "${serializeModulation(fm)}"`);
for (const am of (bp.amModulations ?? [])) lines.push(`AM: "${serializeModulation(am)}"`);
for (const fm of (bp.fmModulations ?? [])) lines.push(`FM: "${_serializeModulation(fm)}"`);
for (const am of (bp.amModulations ?? [])) lines.push(`AM: "${_serializeModulation(am)}"`);
return lines;
}
@@ -54,8 +54,8 @@ function basicPropLines(bp) {
// RFC §3.2.1.2: label name (3+ lowercase chars) is the MAPPING KEY directly.
// Returns array of YAML lines at 0 indent.
function labelSpecLines(ls) {
const inner = basicPropLines(ls.basicProperties);
function _labelSpecLines(ls) {
const inner = _basicPropLines(ls.basicProperties);
if (!inner.length) return [`${ls.label}:`];
return [`${ls.label}:`, ...inner.map(l => ` ${l}`)];
}
@@ -64,20 +64,20 @@ function labelSpecLines(ls) {
// Returns YAML lines for one variation MAPPING (no leading "- ").
// RFC §3.2.1.3: VOLUMES, TIMBRE are variation properties, not instrument-level.
function variationLines(v) {
function _variationLines(v) {
const lines = [];
if (v.dependsOn) lines.push(`ATTR: ${v.dependsOn}`);
lines.push(...basicPropLines(v.basicProperties));
for (const ls of (v.labelSpecs ?? [])) lines.push(...labelSpecLines(ls));
lines.push(..._basicPropLines(v.basicProperties));
for (const ls of (v.labelSpecs ?? [])) lines.push(..._labelSpecLines(ls));
if (v.spread?.length) lines.push(`SPREAD: [${v.spread.join(', ')}]`);
if (v.railsbackCurve) { const rc = serializeShape(v.railsbackCurve); if (rc) lines.push(`RAILSBACK_CURVE: "${rc}"`); }
const vol = serializeShape(v.volumes);
if (v.railsbackCurve) { const rc = _serializeShape(v.railsbackCurve); if (rc) lines.push(`RAILSBACK_CURVE: "${rc}"`); }
const vol = _serializeShape(v.volumes);
if (vol) lines.push(`VOLUMES: "${vol}"`);
const timbre = serializeShape(v.timbre);
const timbre = _serializeShape(v.timbre);
if (timbre) lines.push(`TIMBRE: "${timbre}"`);
for (const fm of (v.fmModulations ?? [])) lines.push(`FM: "${serializeModulation(fm)}"`);
for (const am of (v.amModulations ?? [])) lines.push(`AM: "${serializeModulation(am)}"`);
for (const sv of (v.subvariations ?? [])) lines.push(...variationLines(sv));
for (const fm of (v.fmModulations ?? [])) lines.push(`FM: "${_serializeModulation(fm)}"`);
for (const am of (v.amModulations ?? [])) lines.push(`AM: "${_serializeModulation(am)}"`);
for (const sv of (v.subvariations ?? [])) lines.push(..._variationLines(sv));
return lines;
}
@@ -87,7 +87,7 @@ function variationLines(v) {
// is implicit when no character: wrapper exists). Promote them into a synthetic
// root variation here so the export structure is RFC-correct.
function instrCharacterLines(instr) {
function _instrCharacterLines(instr) {
const variations = instr.variations ?? [];
const hasRootProps = instr.basicProperties || instr.volumes || instr.timbre ||
(instr.fmModulations ?? []).length > 0 ||
@@ -110,14 +110,14 @@ function instrCharacterLines(instr) {
];
if (allVariations.length <= 1) {
const vLines = allVariations.length ? variationLines(allVariations[0]) : [];
const vLines = allVariations.length ? _variationLines(allVariations[0]) : [];
return vLines.map(l => ` ${l}`);
}
// Multiple variations — RFC MAYBE_LIST<VARIATION> as YAML sequence.
const result = [];
for (const v of allVariations) {
const vLines = variationLines(v);
const vLines = _variationLines(v);
if (!vLines.length) continue;
result.push(` - ${vLines[0]}`);
for (const l of vLines.slice(1)) result.push(` ${l}`);
@@ -132,33 +132,33 @@ export function exportInstrument(instr) {
const lines = [`instrument ${instr.name}:`];
if (instr.notChangedSince) lines.push(` NOT_CHANGED_SINCE: ${instr.notChangedSince}`);
lines.push(` character:`);
lines.push(...instrCharacterLines(instr));
lines.push(..._instrCharacterLines(instr));
return lines.join('\n');
}
// ── Articles ───────────────────────────────────────────────────────────────
// RFC §4.3: articles: MAPPING { LABEL: { ATTR: VALUE ... } ... }
function serializeArticleValue(v) {
function _serializeArticleValue(v) {
if (typeof v === 'boolean') return String(v);
if (typeof v === 'number') return String(v);
const s = String(v);
return /[:#\[\]{}&*!,|>'"%@`]/.test(s) ? JSON.stringify(s) : s;
}
function buildArticlesBlock(articles) {
function _buildArticlesBlock(articles) {
const lines = ['articles:'];
for (const art of articles) {
const props = (art.properties ?? []).filter(p => p.name);
if (!props.length) continue;
lines.push(` ${art.name}:`);
for (const p of props) lines.push(` ${p.name}: ${serializeArticleValue(p.value)}`);
for (const p of props) lines.push(` ${p.name}: ${_serializeArticleValue(p.value)}`);
}
return lines.length > 1 ? lines.join('\n') : null;
}
function patchArticles(text, articles) {
const newBlock = buildArticlesBlock(articles);
function _patchArticles(text, articles) {
const newBlock = _buildArticlesBlock(articles);
if (!newBlock) return text;
const lines = text.split('\n');
const artIdx = lines.findIndex(l => /^articles\s*:/.test(l));
@@ -180,7 +180,7 @@ function patchArticles(text, articles) {
const META_KEYS = ['title', 'composer', 'source', 'encrypter'];
function patchMetadata(text, info) {
function _patchMetadata(text, info) {
if (!info) return text;
const lines = text.split('\n');
const replaced = new Set();
@@ -203,7 +203,7 @@ function patchMetadata(text, info) {
return out.join('\n');
}
function patchInstrumentHeader(text, instruments) {
function _patchInstrumentHeader(text, instruments) {
const lines = text.split('\n');
const result = [];
const instrMap = {};
@@ -241,17 +241,17 @@ function patchInstrumentHeader(text, instruments) {
}
function patchBarMeta(doc, bar) {
function _patchBarMeta(doc, bar) {
const props = [];
const sp = stressorToString(bar.stressor);
if (sp) props.push(` stress_pattern: ${sp}`);
if (bar.tempoLevels != null)
props.push(` beats_per_minute: ${bar.tempoLevels}`);
const ub = serializeShape(bar.upperStressBound);
const ub = _serializeShape(bar.upperStressBound);
if (ub) props.push(` upper_stress_bound: ${ub}`);
const lb = serializeShape(bar.lowerStressBound);
const lb = _serializeShape(bar.lowerStressBound);
if (lb) props.push(` lower_stress_bound: ${lb}`);
if (bar.tempoShape) { const ts = serializeShape(bar.tempoShape); if (ts) props.push(` tempo_shape: "${ts}"`); }
if (bar.tempoShape) { const ts = _serializeShape(bar.tempoShape); if (ts) props.push(` tempo_shape: "${ts}"`); }
const lines = doc.split('\n');
const out = [];
@@ -278,7 +278,7 @@ function patchBarMeta(doc, bar) {
return out.join('\n');
}
function buildNewBarDoc(bar) {
function _buildNewBarDoc(bar) {
const lines = [`_id: ${bar.id}`, '_meta:'];
const sp = stressorToString(bar.stressor);
if (sp) lines.push(` stress_pattern: ${sp}`);
@@ -291,15 +291,15 @@ export function patchScore(rawScoreText, instruments, bars = [], info = null, ar
const SEP = '\n---\n';
const [header, ...barDocs] = rawScoreText.split(SEP);
let patchedHeader = patchMetadata(header, info);
let patchedHeader = _patchMetadata(header, info);
const dirtyArticles = articles.filter(a => a.isDirty);
if (dirtyArticles.length || flags.articlesModified) {
patchedHeader = patchArticles(patchedHeader, articles);
patchedHeader = _patchArticles(patchedHeader, articles);
for (const a of dirtyArticles) log.push({ level: 'changed', path: `articles / ${a.name}` });
}
patchedHeader = patchInstrumentHeader(patchedHeader, instruments);
patchedHeader = _patchInstrumentHeader(patchedHeader, instruments);
for (const i of instruments) {
if (i.deleted) log.push({ level: 'changed', path: `instrument / ${i.name} (deleted)` });
else if (i.isDirty) log.push({ level: 'changed', path: `instrument / ${i.name}` });
@@ -309,7 +309,7 @@ export function patchScore(rawScoreText, instruments, bars = [], info = null, ar
for (const bar of bars) barMap[bar.id] = bar;
if (!barDocs.length) {
const newBarDocs = bars.filter(b => b.isNew && !b.deleted).map(b => buildNewBarDoc(b));
const newBarDocs = bars.filter(b => b.isNew && !b.deleted).map(b => _buildNewBarDoc(b));
for (const b of bars.filter(b => b.isNew && !b.deleted)) log.push({ level: 'changed', path: `bar / ${b.id}` });
return { text: [patchedHeader, ...newBarDocs].join(SEP), log };
}
@@ -324,11 +324,11 @@ export function patchScore(rawScoreText, instruments, bars = [], info = null, ar
if (bar.deleted) continue;
if (!bar.isDirty) { passedThrough++; patchedBarDocs.push(doc); continue; }
log.push({ level: 'changed', path: `bar / ${bar.id}` });
patchedBarDocs.push(patchBarMeta(doc, bar));
patchedBarDocs.push(_patchBarMeta(doc, bar));
}
for (const bar of bars.filter(b => b.isNew && !b.deleted)) {
patchedBarDocs.push(buildNewBarDoc(bar));
patchedBarDocs.push(_buildNewBarDoc(bar));
log.push({ level: 'changed', path: `bar / ${bar.id}` });
}