diff --git a/js/utils/utils.js b/js/utils/utils.js index 0c11303cc..4992676c4 100644 --- a/js/utils/utils.js +++ b/js/utils/utils.js @@ -831,66 +831,50 @@ const processRawPluginData = (activity, rawData) => { * @param {object} obj - The processed plugin data object. */ const updatePluginObj = (activity, obj) => { - if (obj === null) { + if (!obj) { return; } - for (const name in obj["PALETTEPLUGINS"]) { - activity.pluginObjs["PALETTEPLUGINS"][name] = obj["PALETTEPLUGINS"][name]; - } - - for (const name in obj["PALETTEFILLCOLORS"]) { - activity.pluginObjs["PALETTEFILLCOLORS"][name] = obj["PALETTEFILLCOLORS"][name]; - } - - for (const name in obj["PALETTESTROKECOLORS"]) { - activity.pluginObjs["PALETTESTROKECOLORS"][name] = obj["PALETTESTROKECOLORS"][name]; - } - - for (const name in obj["PALETTEHIGHLIGHTCOLORS"]) { - activity.pluginObjs["PALETTEHIGHLIGHTCOLORS"][name] = obj["PALETTEHIGHLIGHTCOLORS"][name]; - } - - for (const flow in obj["FLOWPLUGINS"]) { - activity.pluginObjs["FLOWPLUGINS"][flow] = obj["FLOWPLUGINS"][flow]; - } - - for (const arg in obj["ARGPLUGINS"]) { - activity.pluginObjs["ARGPLUGINS"][arg] = obj["ARGPLUGINS"][arg]; - } + // All are defined together + const categories = [ + "PALETTEPLUGINS", + "PALETTEFILLCOLORS", + "PALETTESTROKECOLORS", + "PALETTEHIGHLIGHTCOLORS", + "FLOWPLUGINS", + "ARGPLUGINS", + "BLOCKPLUGINS", + "ONLOAD", + "ONSTART", + "ONSTOP", + ]; - for (const block in obj["BLOCKPLUGINS"]) { - activity.pluginObjs["BLOCKPLUGINS"][block] = obj["BLOCKPLUGINS"][block]; - } + categories.forEach((category) => { + if (obj[category]) { + if (!activity.pluginObjs[category]) { + activity.pluginObjs[category] = {}; + } + Object.assign(activity.pluginObjs[category], obj[category]); // Merge objects + } + }); - if ("MACROPLUGINS" in obj) { - for (const macro in obj["MACROPLUGINS"]) { - activity.pluginObjs["MACROPLUGINS"][macro] = obj["MACROPLUGINS"][macro]; + if (obj["MACROPLUGINS"]) { + if (!activity.pluginObjs["MACROPLUGINS"]) { + activity.pluginObjs["MACROPLUGINS"] = {}; } + Object.assign(activity.pluginObjs["MACROPLUGINS"], obj["MACROPLUGINS"]); } - if ("GLOBALS" in obj) { + if (obj["GLOBALS"]) { if (!("GLOBALS" in activity.pluginObjs)) { activity.pluginObjs["GLOBALS"] = ""; } activity.pluginObjs["GLOBALS"] += obj["GLOBALS"]; } - if ("IMAGES" in obj) { + if (obj["IMAGES"]) { activity.pluginObjs["IMAGES"] = obj["IMAGES"]; } - - for (const name in obj["ONLOAD"]) { - activity.pluginObjs["ONLOAD"][name] = obj["ONLOAD"][name]; - } - - for (const name in obj["ONSTART"]) { - activity.pluginObjs["ONSTART"][name] = obj["ONSTART"][name]; - } - - for (const name in obj["ONSTOP"]) { - activity.pluginObjs["ONSTOP"][name] = obj["ONSTOP"][name]; - } }; /** @@ -1218,27 +1202,20 @@ let mixedNumber = (d) => { if (typeof d === "number") { const floor = Math.floor(d); - if (d > floor) { + const isFractional = d > floor; + + if (isFractional) { const obj = rationalToFraction(d - floor); - if (floor === 0) { - return obj[0] + "/" + obj[1]; - } else { - if (obj[0] === 1 && obj[1] === 1) { - return floor + 1; - } else { - if (obj[1] > 99) { - return d.toFixed(2); - } else { - return floor + " " + obj[0] + "/" + obj[1]; - } - } - } - } else { - return d.toString() + "/1"; + if (obj[1] > 99) return d.toFixed(2); // Limit denominator size. + + const fractionPart = `${obj[0]}/${obj[1]}`; + return floor === 0 ? fractionPart : `${floor} ${fractionPart}`; } - } else { - return d; + + return `${d}/1`; // Whole numbers. } + + return d.toString(); // Non-numeric inputs. }; /** @@ -1264,35 +1241,16 @@ let rationalSum = (a, b) => { } // Make sure a and b components are integers. - let obja0, objb0, obja1, objb1; - if (Math.floor(a[0]) !== a[0]) { - obja0 = rationalToFraction(a[0]); - } else { - obja0 = [a[0], 1]; - } - - if (Math.floor(b[0]) !== b[0]) { - objb0 = rationalToFraction(b[0]); - } else { - objb0 = [b[0], 1]; - } - - if (Math.floor(a[1]) !== a[1]) { - obja1 = rationalToFraction(a[1]); - } else { - obja1 = [a[1], 1]; - } - - if (Math.floor(b[1]) !== b[1]) { - objb1 = rationalToFraction(b[1]); - } else { - objb1 = [b[1], 1]; - } + const normalize = (arr) => { + if (!Number.isInteger(arr[0]) || !Number.isInteger(arr[1])) { + const fraction = rationalToFraction(arr[0] / arr[1]); + return [fraction[0], fraction[1]]; + } + return arr; + }; - a[0] = obja0[0] * obja1[1]; - a[1] = obja0[1] * obja1[0]; - b[0] = objb0[0] * objb1[1]; - b[1] = objb0[1] * objb1[0]; + a = normalize(a); + b = normalize(b); // Find the least common denomenator const lcd = LCD(a[1], b[1]);