Skip to content

Commit

Permalink
flatten notes input
Browse files Browse the repository at this point in the history
  • Loading branch information
dragazo committed Nov 8, 2024
1 parent f51c7a5 commit 5587fd0
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions extensions/BeatBlox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@
}
})();

function flatten(arr) {
const res = [];
function f(v) {
if (Array.isArray(v)) {
for (const u of v) f(u);
} else {
res.push(v);
}
}
f(arr);
return res;
}

function snapify(value) {
if (typeof (value.map) === 'function') {
return new List((Array.isArray(value) ? value : Array.from(value)).map(x => snapify(x)));
Expand Down Expand Up @@ -351,6 +364,7 @@
notes = parseNote(notes);
if (!Array.isArray(notes)) notes = [notes];
if (notes.length === 0) notes = [parseNote('Rest')];
notes = flatten(notes);

if (duration.contents !== undefined) duration = duration.contents;
if (!Array.isArray(duration)) duration = notes.map(() => duration);
Expand Down Expand Up @@ -382,6 +396,7 @@
notes = parseDrumNote(notes);
if (!Array.isArray(notes)) notes = [notes];
if (notes.length === 0) notes = [parseDrumNote('Rest')];
notes = flatten(notes);

if (DURATIONS[duration] === undefined) throw Error(`unknown note duration: "${duration}"`);

Expand Down

0 comments on commit 5587fd0

Please sign in to comment.