Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor changes with code optimization, enhancement, and better handling. #4073

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 48 additions & 90 deletions js/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
};

/**
Expand Down Expand Up @@ -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.
};

/**
Expand All @@ -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]);
Expand Down
Loading