refactor: extract coerce and stressorToString into util.js
This commit is contained in:
@@ -1,16 +1,8 @@
|
|||||||
|
import { coerce } from './util.js';
|
||||||
|
|
||||||
const LINE_RE = /^(\d{2}) (\S+(?:\.\S+)*) ?(.*)/;
|
const LINE_RE = /^(\d{2}) (\S+(?:\.\S+)*) ?(.*)/;
|
||||||
const DEBUG_RE = /^\d{2} # DEBUG/;
|
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) {
|
export function parseAstLog(text) {
|
||||||
const root = { slot: 'root', parentSlot: null, depth: -1, positionals: [], props: {}, children: [] };
|
const root = { slot: 'root', parentSlot: null, depth: -1, positionals: [], props: {}, children: [] };
|
||||||
const stack = [root];
|
const stack = [root];
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ import { ObjectExtended } from './ObjectExtended.js';
|
|||||||
import { EnvelopeEditor } from './EnvelopeEditor.js';
|
import { EnvelopeEditor } from './EnvelopeEditor.js';
|
||||||
import { ShapeEditor } from './ShapeEditor.js';
|
import { ShapeEditor } from './ShapeEditor.js';
|
||||||
import { LinkedInstrumentModal } from './LinkedInstrumentModal.js';
|
import { LinkedInstrumentModal } from './LinkedInstrumentModal.js';
|
||||||
import { stressorToString } from '../exporter.js';
|
import { coerce, stressorToString } from '../util.js';
|
||||||
import { coerce } from '../ast-parser.js';
|
|
||||||
|
|
||||||
const H4 = { style: 'margin:0 0 0.5rem' };
|
const H4 = { style: 'margin:0 0 0.5rem' };
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// RFC-compliant YAML serializer for Sompyler instrument blocks.
|
import { stressorToString } from './util.js';
|
||||||
// Operates on the model produced by ast-parser.js buildModel().
|
|
||||||
|
|
||||||
// ── Shape ──────────────────────────────────────────────────────────────────
|
// ── Shape ──────────────────────────────────────────────────────────────────
|
||||||
// RFC §1.3.4.5: SHAPE = [PREFIX (":" / ";")] Node 1*(";" Node)
|
// RFC §1.3.4.5: SHAPE = [PREFIX (":" / ";")] Node 1*(";" Node)
|
||||||
@@ -241,10 +240,6 @@ function patchInstrumentHeader(text, instruments) {
|
|||||||
return result.join('\n');
|
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) {
|
function patchBarMeta(doc, bar) {
|
||||||
const props = [];
|
const props = [];
|
||||||
|
|||||||
13
static/util.js
Normal file
13
static/util.js
Normal file
@@ -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(';');
|
||||||
|
}
|
||||||
@@ -4,7 +4,8 @@
|
|||||||
|
|
||||||
import { readFileSync } from 'fs';
|
import { readFileSync } from 'fs';
|
||||||
import { parseAstLog, buildModel } from './static/ast-parser.js';
|
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 FIXTURE = new URL('./fixtures/ast.log', import.meta.url);
|
||||||
const text = readFileSync(FIXTURE, 'utf8');
|
const text = readFileSync(FIXTURE, 'utf8');
|
||||||
|
|||||||
Reference in New Issue
Block a user