Browse Source

Rename and refactor logicalOperator to BooleanOperator

pull/6419/head
mohamedsalem401 2 years ago
parent
commit
847ce26c89
  1. 25
      packages/core/src/data_sources/model/conditional_variables/operators/BooleanOperator.ts
  2. 28
      packages/core/src/data_sources/model/conditional_variables/operators/LogicalOperator.ts

25
packages/core/src/data_sources/model/conditional_variables/operators/BooleanOperator.ts

@ -0,0 +1,25 @@
import { Operator } from './BaseOperator';
export enum BooleanOperation {
and = 'and',
or = 'or',
xor = 'xor',
}
export class BooleanOperator extends Operator<BooleanOperation> {
evaluate(statements: boolean[]): boolean {
if (!statements.length) throw new Error('Expected one or more statments, got none');
switch (this.operation) {
case BooleanOperation.and:
return statements.every(Boolean);
case BooleanOperation.or:
return statements.some(Boolean);
case BooleanOperation.xor:
return statements.filter(Boolean).length === 1;
default:
this.em.logError(`Unsupported logical operation: ${this.operation}`);
return false;
}
}
}

28
packages/core/src/data_sources/model/conditional_variables/operators/LogicalOperator.ts

@ -1,28 +0,0 @@
import { Operator } from '.';
export enum LogicalOperation {
and = 'and',
or = 'or',
xor = 'xor',
}
export class LogicalOperator extends Operator {
constructor(private operator: LogicalOperation) {
super();
}
evaluate(statements: boolean[]): boolean {
if (!statements.length) throw new Error('Expected one or more statments, got none');
switch (this.operator) {
case LogicalOperation.and:
return statements.every(Boolean);
case LogicalOperation.or:
return statements.some(Boolean);
case LogicalOperation.xor:
return statements.filter(Boolean).length === 1;
default:
throw new Error(`Unsupported logical operator: ${this.operator}`);
}
}
}
Loading…
Cancel
Save