Skip to content

Commit

Permalink
[REF] functions: create helper for AND and OR
Browse files Browse the repository at this point in the history
This commit is a step to introduce the spreadsheet pivot. The functions
AND and OR are used in the pivot to compute the values when the measure
is a boolean.

Task: 3748717
Part-of: #4025
Co-authored-by: rrahir <rar@odoo.com>
Co-authored-by: Pierre Rousseau <pro@odoo.com>
  • Loading branch information
pro-odoo and rrahir committed May 15, 2024
1 parent 51336c1 commit 9bd97d9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
30 changes: 30 additions & 0 deletions src/functions/helper_logical.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Arg } from "../types";
import { conditionalVisitBoolean } from "./helpers";

export function boolAnd(args: Arg[]) {
let foundBoolean = false;
let acc = true;
conditionalVisitBoolean(args, (arg) => {
foundBoolean = true;
acc = acc && arg;
return acc;
});
return {
foundBoolean,
result: acc,
};
}

export function boolOr(args: Arg[]) {
let foundBoolean = false;
let acc = false;
conditionalVisitBoolean(args, (arg) => {
foundBoolean = true;
acc = acc || arg;
return !acc;
});
return {
foundBoolean,
result: acc,
};
}
21 changes: 5 additions & 16 deletions src/functions/module_logical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { _t } from "../translation";
import { AddFunctionDescription, Arg, FPayload, Maybe } from "../types";
import { CellErrorType, EvaluationError } from "../types/errors";
import { arg } from "./arguments";
import { boolAnd, boolOr } from "./helper_logical";
import { assert, conditionalVisitBoolean, isEvaluationError, toBoolean } from "./helpers";

// -----------------------------------------------------------------------------
Expand All @@ -23,15 +24,9 @@ export const AND = {
],
returns: ["BOOLEAN"],
compute: function (...logicalExpressions: Arg[]): boolean {
let foundBoolean = false;
let acc = true;
conditionalVisitBoolean(logicalExpressions, (arg) => {
foundBoolean = true;
acc = acc && arg;
return acc;
});
const { result, foundBoolean } = boolAnd(logicalExpressions);
assert(() => foundBoolean, _t("[[FUNCTION_NAME]] has no valid input data."));
return acc;
return result;
},
isExported: true,
} satisfies AddFunctionDescription;
Expand Down Expand Up @@ -227,15 +222,9 @@ export const OR = {
],
returns: ["BOOLEAN"],
compute: function (...logicalExpressions: Arg[]): boolean {
let foundBoolean = false;
let acc = false;
conditionalVisitBoolean(logicalExpressions, (arg) => {
foundBoolean = true;
acc = acc || arg;
return !acc;
});
const { result, foundBoolean } = boolOr(logicalExpressions);
assert(() => foundBoolean, _t("[[FUNCTION_NAME]] has no valid input data."));
return acc;
return result;
},
isExported: true,
} satisfies AddFunctionDescription;
Expand Down

0 comments on commit 9bd97d9

Please sign in to comment.