Skip to content

Commit

Permalink
preserveOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
jazz-soft committed Nov 10, 2024
1 parent e668b5f commit fc72733
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

coming soon...

see: https://www.w3.org/2021/06/musicxml40
see: https://w3c.github.io/musicxml
42 changes: 19 additions & 23 deletions jazz-mxml.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const FXP = require('fast-xml-parser');
const specs = require('./specs.js');

const parser = new FXP.XMLParser({ ignoreAttributes: false });
const builder = new FXP.XMLBuilder({ ignoreAttributes: false, format: true });
const parser = new FXP.XMLParser({ ignoreAttributes: false, preserveOrder: true, ignorePiTags: true });
const builder = new FXP.XMLBuilder({ ignoreAttributes: false, preserveOrder: true, format: true });

function MXML(s) {
if (!s || FXP.XMLValidator.validate(s) !== true) return;
var xml = parser.parse(s);
if (!xml['score-partwise'] && !xml['score-timewise'] && !xml['opus'] && !xml['mei']) return;
if (xml.length != 1 || !xml[0]['score-partwise'] && !xml[0]['score-timewise'] && !xml[0]['opus'] && !xml[0]['mei']) return;
this.txt = s;
this.xml = parser.parse(s);
this.xml = xml;
}
MXML.prototype.isValid = function() { return !!this.xml; };
MXML.prototype.isPartwise = function() { return this.isValid() && !!this.xml['score-partwise']; };
MXML.prototype.isTimewise = function() { return this.isValid() && !!this.xml['score-timewise']; };
MXML.prototype.isOpus = function() { return this.isValid() && !!this.xml['opus']; };
MXML.prototype.isMei = function() { return this.isValid() && !!this.xml['mei']; };
MXML.prototype.isPartwise = function() { return this.isValid() && !!this.xml[0]['score-partwise']; };
MXML.prototype.isTimewise = function() { return this.isValid() && !!this.xml[0]['score-timewise']; };
MXML.prototype.isOpus = function() { return this.isValid() && !!this.xml[0]['opus']; };
MXML.prototype.isMei = function() { return this.isValid() && !!this.xml[0]['mei']; };
MXML.prototype.part2time = function() {};
MXML.prototype.time2part = MXML.prototype.part2time;
MXML.prototype.format = function () { return builder.build(this.xml); };
Expand Down Expand Up @@ -83,8 +83,8 @@ MXML.unzip = async function(data) {
var a, k, s, s0, s1, s2, x;
if (FF[meta_inf]) {
a = [];
x = parse(decompress(data, FF[meta_inf]));
traverse(x, find('rootfile', function(x) { if (x['@_full-path']) a.push(x['@_full-path']); }));
x = parse(await decompress(data, FF[meta_inf]));
traverse(x, find('rootfile', function(x) { if (x[':@'] && x[':@']['@_full-path']) a.push(x[':@']['@_full-path']); }));
if (a.length) FFF = a;
}
for (k of FFF) {
Expand All @@ -94,10 +94,10 @@ MXML.unzip = async function(data) {
s = String(s);
if (!s0) s0 = s;
x = parse(s);
if (!x) continue;
if (!x || !x.length) continue;
if (!s1) s1 = s;
if (x['score-partwise'] || x['score-timewise']) return s;
if (x['opus'] && !s2) s2 = s;
if (x[0]['score-partwise'] || x[0]['score-timewise']) return s;
if (x[0]['opus'] && !s2) s2 = s;
}
return s2 || s1 || s0;
}
Expand Down Expand Up @@ -227,18 +227,14 @@ function parse(xml) {
}
catch (e) {/**/}
}
function find(n, f) { return function(x) { if (x[n]) f(x); }; }
function traverse(x, f) {
if (Array.isArray(x)) for (var y of x) traverse(y, f);
if (typeof x != 'object') return;
f(x);
for (var k of Object.keys(x)) traverse(x[k], f);
}
function find(n, f) {
return function(x) {
x = x[n];
if (Array.isArray(x)) for (var y of x) f(y);
else if (typeof x == 'object') f(x);
};
else {
if (typeof x != 'object') return;
f(x);
for (var k of Object.keys(x)) traverse(x[k], f);
}
}
var T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, Ta, Tb, Tc, Td, Te, Tf;
(function() {
Expand Down
8 changes: 4 additions & 4 deletions test/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const assert = require('assert');
const MXML = require('..');
const specs = require('../specs.js');

const partwise = '<?xml version="1.0" encoding="UTF-8" standalone="no"?><score-partwise version="4.0"><part-list><score-part id="P1"><part-name>Music</part-name></score-part></part-list><part id="P1"><measure number="1"><attributes><divisions>1</divisions><key><fifths>0</fifths></key><time><beats>4</beats><beat-type>4</beat-type></time><clef><sign>G</sign><line>2</line></clef></attributes><note><pitch><step>C</step><octave>4</octave></pitch><duration>4</duration><type>whole</type></note></measure></part></score-partwise>';
const timewise = '<?xml version="1.0" encoding="UTF-8" standalone="no"?><score-timewise version="4.0"><part-list><score-part id="P1"><part-name>Music</part-name></score-part></part-list><measure number="1"><part id="P1"><attributes><divisions>1</divisions><key><fifths>0</fifths></key><time><beats>4</beats><beat-type>4</beat-type></time><clef><sign>G</sign><line>2</line></clef></attributes><note><pitch><step>C</step><octave>4</octave></pitch><duration>4</duration><type>whole</type></note></part></measure></score-timewise>';
const partwise = '<?xml version="1.0" encoding="UTF-8"?><score-partwise version="4.0"><part-list><score-part id="P1"><part-name>Music</part-name></score-part></part-list><part id="P1"><measure number="1"><attributes><divisions>1</divisions><key><fifths>0</fifths></key><time><beats>4</beats><beat-type>4</beat-type></time><clef><sign>G</sign><line>2</line></clef></attributes><note><pitch><step>C</step><octave>4</octave></pitch><duration>4</duration><type>whole</type></note></measure></part></score-partwise>';
const timewise = '<?xml version="1.0" encoding="UTF-8"?><score-timewise version="4.0"><part-list><score-part id="P1"><part-name>Music</part-name></score-part></part-list><measure number="1"><part id="P1"><attributes><divisions>1</divisions><key><fifths>0</fifths></key><time><beats>4</beats><beat-type>4</beat-type></time><clef><sign>G</sign><line>2</line></clef></attributes><note><pitch><step>C</step><octave>4</octave></pitch><duration>4</duration><type>whole</type></note></part></measure></score-timewise>';

describe('constructor', function() {
it('empty', function() {
Expand Down Expand Up @@ -101,8 +101,8 @@ describe('utils', function() {
});
it('format', function() {
var X = new MXML(partwise);
assert.equal(X.txt.length, 508);
assert.equal(X.format().length, 754);
assert.equal(X.txt.length, 492);
assert.equal(X.format().length, 699);
});
it('part2time', function() {
var X = new MXML(partwise);
Expand Down

0 comments on commit fc72733

Please sign in to comment.