@totvs-agro/core-mobile
Version:
Core Mobile Totvs Agro (Front-End) para utilização dos estilos do T-Faces
166 lines • 8.11 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import * as formulaParser from 'hot-formula-parser';
/**
* Object to execute some formula with given variables.
* @example
const runner = new FormulaRunner('SUM([D],[C])');
runner.addVariable({identifier: 'A', value: 10, formula: null});
runner.addVariable({identifier: 'B', value: 5, formula: null});
runner.addVariable({identifier: 'C', value: null, formula: '[A]+[B]'});
runner.addVariable({identifier: 'D', value: null, formula: '[C] * 2'});
runner.run()
.then(response => console.log(response.result))
*/
var FormulaRunner = /** @class */ (function () {
function FormulaRunner(_resultFormula) {
this._resultFormula = _resultFormula;
this._variables = new Map();
}
/**
* Add a FormulaVariable to the runner.
* @param variable FormulaVariable
*/
FormulaRunner.prototype.addVariable = function (variable) {
var variables = this._variables.get(variable.identifier) || [];
variables.push(variable);
this._variables.set(variable.identifier, variables);
};
/**
* Modifies value of variable with the given identifier (only if has only one variable with the identifier)
* @param identifier Identifier of the variable to modify value
* @param value Value to be set
*/
FormulaRunner.prototype.setValue = function (identifier, value) {
var variables = this._variables.get(identifier);
if (variables && variables.length == 1) {
variables[0].value = value;
}
};
/**
* Runs the expression and return the result for the given formula
*/
FormulaRunner.prototype.run = function () {
return __awaiter(this, void 0, void 0, function () {
var parser, translatedFormula, exec, message;
return __generator(this, function (_a) {
parser = new formulaParser.Parser();
this._variables.forEach(function (variables, identifier) {
var i = 0;
var _loop_1 = function (variable) {
if (variable.value) {
parser.setFunction(identifier + FormulaRunner.VAR_FUNCTION_SEPARATOR + i, function () { return variable.value; });
}
i++;
};
for (var _i = 0, variables_1 = variables; _i < variables_1.length; _i++) {
var variable = variables_1[_i];
_loop_1(variable);
}
});
try {
translatedFormula = this.translateFormula(new String(this._resultFormula));
exec = parser.parse(translatedFormula);
message = this.translateErrorMessage(exec.error) || 'Success';
return [2 /*return*/, {
isValid: exec.error == null,
message: message,
result: exec.result
}];
}
catch (error) {
return [2 /*return*/, {
isValid: false,
message: error,
result: null
}];
}
return [2 /*return*/];
});
});
};
/**
* Translates the formula written by user, to be readed inside de API
* @param formula
*/
FormulaRunner.prototype.translateFormula = function (formula) {
var formulaWithSpaceBetweenTokens = formula.replace(/\[/g, ' \[').replace(/\]/g, '\] ');
var tokens = formulaWithSpaceBetweenTokens.match(/\[\S+\]/g);
if (tokens) {
for (var _i = 0, tokens_1 = tokens; _i < tokens_1.length; _i++) {
var keyVar = tokens_1[_i];
var identifier = keyVar.replace('[', '').replace(']', '').toUpperCase(); //12.1.24
var variables = this._variables.get(identifier);
var variable = variables[0];
if (variable.value) {
var statement = variables.map(function (item, i, arr) { return item.identifier + '_' + i + '()'; }).join(',');
formula = formula.replace(keyVar, statement);
}
else if (variable.formula) {
formula = formula.replace(keyVar, "(" + this.translateFormula(variable.formula) + ")");
}
else {
throw new Error("Variable " + identifier + " has no value.");
}
}
}
return formula;
};
/**
* This method translates the error object from hot-parser-formula to an string message
* @param error
*/
FormulaRunner.prototype.translateErrorMessage = function (error) {
switch (error) {
case '#ERROR!':
return 'General error on expression.';
case '#DIV/0!':
return 'Divide by zero error.';
case '#NAME?':
return 'Not recognised variable name.';
case '#N/A':
return 'Some value is not available to the expression.';
case '#NUM!':
return 'Invalid number.';
case '#VALUE!':
return 'Some value is not available to the expression.';
}
};
FormulaRunner.VAR_FUNCTION_SEPARATOR = '_';
return FormulaRunner;
}());
export { FormulaRunner };
//# sourceMappingURL=formula-runner.js.map