Skip to content

Commit 5bad696

Browse files
committed
Minor Chances, Code improvement and Handling
1. made the updatePluginObj code shorter and combined categories making it easier to maintain in the long term, also ensured that the structure exists 2. minor improvements in mixedNumber function
1 parent 2d81543 commit 5bad696

File tree

1 file changed

+48
-90
lines changed

1 file changed

+48
-90
lines changed

js/utils/utils.js

Lines changed: 48 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -831,66 +831,50 @@ const processRawPluginData = (activity, rawData) => {
831831
* @param {object} obj - The processed plugin data object.
832832
*/
833833
const updatePluginObj = (activity, obj) => {
834-
if (obj === null) {
834+
if (!obj) {
835835
return;
836836
}
837837

838-
for (const name in obj["PALETTEPLUGINS"]) {
839-
activity.pluginObjs["PALETTEPLUGINS"][name] = obj["PALETTEPLUGINS"][name];
840-
}
841-
842-
for (const name in obj["PALETTEFILLCOLORS"]) {
843-
activity.pluginObjs["PALETTEFILLCOLORS"][name] = obj["PALETTEFILLCOLORS"][name];
844-
}
845-
846-
for (const name in obj["PALETTESTROKECOLORS"]) {
847-
activity.pluginObjs["PALETTESTROKECOLORS"][name] = obj["PALETTESTROKECOLORS"][name];
848-
}
849-
850-
for (const name in obj["PALETTEHIGHLIGHTCOLORS"]) {
851-
activity.pluginObjs["PALETTEHIGHLIGHTCOLORS"][name] = obj["PALETTEHIGHLIGHTCOLORS"][name];
852-
}
853-
854-
for (const flow in obj["FLOWPLUGINS"]) {
855-
activity.pluginObjs["FLOWPLUGINS"][flow] = obj["FLOWPLUGINS"][flow];
856-
}
857-
858-
for (const arg in obj["ARGPLUGINS"]) {
859-
activity.pluginObjs["ARGPLUGINS"][arg] = obj["ARGPLUGINS"][arg];
860-
}
838+
// All are defined together
839+
const categories = [
840+
"PALETTEPLUGINS",
841+
"PALETTEFILLCOLORS",
842+
"PALETTESTROKECOLORS",
843+
"PALETTEHIGHLIGHTCOLORS",
844+
"FLOWPLUGINS",
845+
"ARGPLUGINS",
846+
"BLOCKPLUGINS",
847+
"ONLOAD",
848+
"ONSTART",
849+
"ONSTOP",
850+
];
861851

862-
for (const block in obj["BLOCKPLUGINS"]) {
863-
activity.pluginObjs["BLOCKPLUGINS"][block] = obj["BLOCKPLUGINS"][block];
864-
}
852+
categories.forEach((category) => {
853+
if (obj[category]) {
854+
if (!activity.pluginObjs[category]) {
855+
activity.pluginObjs[category] = {};
856+
}
857+
Object.assign(activity.pluginObjs[category], obj[category]); // Merge objects
858+
}
859+
});
865860

866-
if ("MACROPLUGINS" in obj) {
867-
for (const macro in obj["MACROPLUGINS"]) {
868-
activity.pluginObjs["MACROPLUGINS"][macro] = obj["MACROPLUGINS"][macro];
861+
if (obj["MACROPLUGINS"]) {
862+
if (!activity.pluginObjs["MACROPLUGINS"]) {
863+
activity.pluginObjs["MACROPLUGINS"] = {};
869864
}
865+
Object.assign(activity.pluginObjs["MACROPLUGINS"], obj["MACROPLUGINS"]);
870866
}
871867

872-
if ("GLOBALS" in obj) {
868+
if (obj["GLOBALS"]) {
873869
if (!("GLOBALS" in activity.pluginObjs)) {
874870
activity.pluginObjs["GLOBALS"] = "";
875871
}
876872
activity.pluginObjs["GLOBALS"] += obj["GLOBALS"];
877873
}
878874

879-
if ("IMAGES" in obj) {
875+
if (obj["IMAGES"]) {
880876
activity.pluginObjs["IMAGES"] = obj["IMAGES"];
881877
}
882-
883-
for (const name in obj["ONLOAD"]) {
884-
activity.pluginObjs["ONLOAD"][name] = obj["ONLOAD"][name];
885-
}
886-
887-
for (const name in obj["ONSTART"]) {
888-
activity.pluginObjs["ONSTART"][name] = obj["ONSTART"][name];
889-
}
890-
891-
for (const name in obj["ONSTOP"]) {
892-
activity.pluginObjs["ONSTOP"][name] = obj["ONSTOP"][name];
893-
}
894878
};
895879

896880
/**
@@ -1218,27 +1202,20 @@ let mixedNumber = (d) => {
12181202

12191203
if (typeof d === "number") {
12201204
const floor = Math.floor(d);
1221-
if (d > floor) {
1205+
const isFractional = d > floor;
1206+
1207+
if (isFractional) {
12221208
const obj = rationalToFraction(d - floor);
1223-
if (floor === 0) {
1224-
return obj[0] + "/" + obj[1];
1225-
} else {
1226-
if (obj[0] === 1 && obj[1] === 1) {
1227-
return floor + 1;
1228-
} else {
1229-
if (obj[1] > 99) {
1230-
return d.toFixed(2);
1231-
} else {
1232-
return floor + " " + obj[0] + "/" + obj[1];
1233-
}
1234-
}
1235-
}
1236-
} else {
1237-
return d.toString() + "/1";
1209+
if (obj[1] > 99) return d.toFixed(2); // Limit denominator size.
1210+
1211+
const fractionPart = `${obj[0]}/${obj[1]}`;
1212+
return floor === 0 ? fractionPart : `${floor} ${fractionPart}`;
12381213
}
1239-
} else {
1240-
return d;
1214+
1215+
return `${d}/1`; // Whole numbers.
12411216
}
1217+
1218+
return d.toString(); // Non-numeric inputs.
12421219
};
12431220

12441221
/**
@@ -1264,35 +1241,16 @@ let rationalSum = (a, b) => {
12641241
}
12651242

12661243
// Make sure a and b components are integers.
1267-
let obja0, objb0, obja1, objb1;
1268-
if (Math.floor(a[0]) !== a[0]) {
1269-
obja0 = rationalToFraction(a[0]);
1270-
} else {
1271-
obja0 = [a[0], 1];
1272-
}
1273-
1274-
if (Math.floor(b[0]) !== b[0]) {
1275-
objb0 = rationalToFraction(b[0]);
1276-
} else {
1277-
objb0 = [b[0], 1];
1278-
}
1279-
1280-
if (Math.floor(a[1]) !== a[1]) {
1281-
obja1 = rationalToFraction(a[1]);
1282-
} else {
1283-
obja1 = [a[1], 1];
1284-
}
1285-
1286-
if (Math.floor(b[1]) !== b[1]) {
1287-
objb1 = rationalToFraction(b[1]);
1288-
} else {
1289-
objb1 = [b[1], 1];
1290-
}
1244+
const normalize = (arr) => {
1245+
if (!Number.isInteger(arr[0]) || !Number.isInteger(arr[1])) {
1246+
const fraction = rationalToFraction(arr[0] / arr[1]);
1247+
return [fraction[0], fraction[1]];
1248+
}
1249+
return arr;
1250+
};
12911251

1292-
a[0] = obja0[0] * obja1[1];
1293-
a[1] = obja0[1] * obja1[0];
1294-
b[0] = objb0[0] * objb1[1];
1295-
b[1] = objb0[1] * objb1[0];
1252+
a = normalize(a);
1253+
b = normalize(b);
12961254

12971255
// Find the least common denomenator
12981256
const lcd = LCD(a[1], b[1]);

0 commit comments

Comments
 (0)