UNPKG

@sunzhongmou/math

Version:
44 lines 1.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Expression = exports.Operations = void 0; var Operations; (function (Operations) { Operations[Operations["ADD"] = 0] = "ADD"; Operations[Operations["SUB"] = 1] = "SUB"; })(Operations = exports.Operations || (exports.Operations = {})); class Expression { constructor(destOperand, operation, operand) { this.destinationOperand = destOperand; this.operationSets = []; this.addOperationSet(operation, operand); } addOperationSet(operation, operand) { this.operationSets.push({ operand, operation }); } sameValueAs(other) { return (this.destinationOperand === other.destinationOperand && this.operationSets === other.operationSets); } getOperationSetsRaw() { return this.operationSets.reduce((raw, oSet) => `${raw}${oSet.operation === Operations.ADD ? ' + ' : ' - '}${oSet.operand}`, ''); } getRaw() { return `${this.destinationOperand}${this.getOperationSetsRaw()} =`; } execute() { let result = this.destinationOperand; for (const oSet of this.operationSets) { switch (oSet.operation) { case Operations.ADD: result += oSet.operand; break; case Operations.SUB: result -= oSet.operand; break; } } return result; } } exports.Expression = Expression; //# sourceMappingURL=expression.js.map