Skip to content

Commit 0845552

Browse files
committed
Use Array-destructuring when computing MIN/MAX in AFSimple_Calculate
This appears to work fine with QuickJS, as evident by the added unit-test, and allows us to remove more `Array.prototype.reduce` usage.
1 parent 6cc37c8 commit 0845552

File tree

2 files changed

+112
-4
lines changed

2 files changed

+112
-4
lines changed

src/scripting_api/aform.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,8 @@ class AForm {
392392
AVG: args => args.reduce((acc, value) => acc + value, 0) / args.length,
393393
SUM: args => args.reduce((acc, value) => acc + value, 0),
394394
PRD: args => args.reduce((acc, value) => acc * value, 1),
395-
MIN: args =>
396-
args.reduce((acc, value) => Math.min(acc, value), Number.MAX_VALUE),
397-
MAX: args =>
398-
args.reduce((acc, value) => Math.max(acc, value), Number.MIN_VALUE),
395+
MIN: args => Math.min(...args),
396+
MAX: args => Math.max(...args),
399397
};
400398

401399
if (!(cFunction in actions)) {

test/unit/scripting_spec.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,116 @@ describe("Scripting", function () {
13941394
formattedValue: null,
13951395
});
13961396
});
1397+
1398+
it("should compute the max of several fields", async () => {
1399+
const refIds = [0, 1, 2, 3, 4].map(_ => getId());
1400+
const data = {
1401+
objects: {
1402+
field1: [
1403+
{
1404+
id: refIds[0],
1405+
value: "",
1406+
actions: {},
1407+
type: "text",
1408+
},
1409+
],
1410+
field2: [
1411+
{
1412+
id: refIds[1],
1413+
value: "",
1414+
actions: {},
1415+
type: "text",
1416+
},
1417+
],
1418+
field3: [
1419+
{
1420+
id: refIds[2],
1421+
value: "",
1422+
actions: {},
1423+
type: "text",
1424+
},
1425+
],
1426+
field4: [
1427+
{
1428+
id: refIds[3],
1429+
value: "",
1430+
actions: {
1431+
Calculate: [
1432+
`AFSimple_Calculate("MAX", ["field1", "field2", "field3", "unknown"]);`,
1433+
],
1434+
},
1435+
type: "text",
1436+
},
1437+
],
1438+
field5: [
1439+
{
1440+
id: refIds[4],
1441+
value: "",
1442+
actions: {
1443+
Calculate: [
1444+
`AFSimple_Calculate("MAX", "field1, field2, field3, unknown");`,
1445+
],
1446+
},
1447+
type: "text",
1448+
},
1449+
],
1450+
},
1451+
appInfo: { language: "en-US", platform: "Linux x86_64" },
1452+
calculationOrder: [refIds[3], refIds[4]],
1453+
dispatchEventName: "_dispatchMe",
1454+
};
1455+
1456+
sandbox.createSandbox(data);
1457+
await sandbox.dispatchEventInSandbox({
1458+
id: refIds[0],
1459+
value: "1",
1460+
name: "Keystroke",
1461+
willCommit: true,
1462+
});
1463+
expect(send_queue.has(refIds[3])).toEqual(true);
1464+
expect(send_queue.get(refIds[3])).toEqual({
1465+
id: refIds[3],
1466+
siblings: null,
1467+
value: 1,
1468+
formattedValue: null,
1469+
});
1470+
1471+
await sandbox.dispatchEventInSandbox({
1472+
id: refIds[1],
1473+
value: "2",
1474+
name: "Keystroke",
1475+
willCommit: true,
1476+
});
1477+
expect(send_queue.has(refIds[3])).toEqual(true);
1478+
expect(send_queue.get(refIds[3])).toEqual({
1479+
id: refIds[3],
1480+
siblings: null,
1481+
value: 2,
1482+
formattedValue: null,
1483+
});
1484+
1485+
await sandbox.dispatchEventInSandbox({
1486+
id: refIds[2],
1487+
value: "3",
1488+
name: "Keystroke",
1489+
willCommit: true,
1490+
});
1491+
expect(send_queue.has(refIds[3])).toEqual(true);
1492+
expect(send_queue.get(refIds[3])).toEqual({
1493+
id: refIds[3],
1494+
siblings: null,
1495+
value: 3,
1496+
formattedValue: null,
1497+
});
1498+
1499+
expect(send_queue.has(refIds[4])).toEqual(true);
1500+
expect(send_queue.get(refIds[4])).toEqual({
1501+
id: refIds[4],
1502+
siblings: null,
1503+
value: 3,
1504+
formattedValue: null,
1505+
});
1506+
});
13971507
});
13981508

13991509
describe("AFSpecial_KeystrokeEx", function () {

0 commit comments

Comments
 (0)