From 4824e81dd4fe7fb09cad68854c78d4a8afa10cc6 Mon Sep 17 00:00:00 2001 From: Miika Alonen Date: Sat, 4 Nov 2023 15:43:07 +0200 Subject: [PATCH] Insane array mangling for scale arrays --- src/Utils/Generic.ts | 20 +++++++++++--------- src/classes/SoundEvent.ts | 1 - 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Utils/Generic.ts b/src/Utils/Generic.ts index d6e91e6..69e5da1 100644 --- a/src/Utils/Generic.ts +++ b/src/Utils/Generic.ts @@ -6,9 +6,15 @@ * @returns {Record[]} Array of objects * */ -export function objectWithArraysToArrayOfObjects(input: Record, ignoredKeys: string[]): Record[] { - ignoredKeys = ignoredKeys.map((k) => Array.isArray(input[k]) ? undefined : k).filter((k) => k !== undefined) as string[]; - const keys = Object.keys(input).filter((k) => !ignoredKeys.includes(k)); +export function objectWithArraysToArrayOfObjects(input: Record, arraysToArrays: string[]): Record[] { + arraysToArrays.forEach((k) => { + // Transform single array to array of arrays and keep array of arrays as is + if (Array.isArray(input[k]) && !Array.isArray(input[k][0])) { + input[k] = [input[k]]; + } + }); + const keys = Object.keys(input); + const maxLength = Math.max( ...keys.map((k) => Array.isArray(input[k]) ? (input[k] as any[]).length : 1 @@ -20,14 +26,10 @@ export function objectWithArraysToArrayOfObjects(input: Record, ign for (let i = 0; i < maxLength; i++) { const event: Record = {}; for (const k of keys) { - if (ignoredKeys.includes(k)) { - event[k] = input[k]; - } else { - if (Array.isArray(input[k])) { + if (Array.isArray(input[k])) { event[k] = (input[k] as any[])[i % (input[k] as any[]).length]; - } else { + } else { event[k] = input[k]; - } } } output.push(event); diff --git a/src/classes/SoundEvent.ts b/src/classes/SoundEvent.ts index 098515b..2d34c12 100644 --- a/src/classes/SoundEvent.ts +++ b/src/classes/SoundEvent.ts @@ -375,7 +375,6 @@ export class SoundEvent extends AudibleEvent { const events = objectWithArraysToArrayOfObjects(filteredValues, [ "parsedScale", ]); - events.forEach((event) => { const [note, _] = noteFromPc( (event.key as number) || "C4",