UNPKG

maycur-business

Version:

maycur business react components of web

198 lines (160 loc) 6.54 kB
"use strict"; function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var funcs = _interopRequireWildcard(require("./formularFunction/functions")); var _compile = _interopRequireDefault(require("./formularFunction/compile")); var _compatDivide = _interopRequireDefault(require("./formularFunction/compatDivide")); var _isfunction = _interopRequireDefault(require("./formularFunction/isfunction")); var _istext = _interopRequireDefault(require("./formularFunction/istext")); var _error = require("./formularFunction/error"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var insideFnList = Object.keys(funcs); function has() { var obj = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var key = arguments.length > 1 ? arguments[1] : undefined; return obj.hasOwnProperty(key); } function decimalFix(value) { if (value !== null && value !== '' && !isNaN(Number(value))) { return Number(Number(value).toFixed(8)); } else { return value; } } var variableNameReg = new RegExp('^[a-zA-Z_$][a-zA-Z_$0-9]*$'); var Formula = function Formula() { var _this = this; var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, Formula); this.getVariable = function (name, scope) { if ((0, _istext["default"])(scope)) { return _this.variables[scope] ? _this.variables[scope][name] : undefined; } if (!has(_this.variables, name)) { return _error.ERRORTYPES.ud; } else { return _this.variables[name]; } }; this.setVariable = function (name, value) { var result = { status: 'fail', message: '' }; if (name && (0, _istext["default"])(name)) { if (variableNameReg.test(name)) { _this.variables[name] = value; result.status = 'success'; } else { console.warn(name + ' 变量名不符合规范'); result.status = 'fail'; result.message = '变量名不符合规范'; } } return result; }; this.setFunction = function (name, fn) { var result = { status: 'fail', message: '' }; if (name && (0, _istext["default"])(name) && (0, _isfunction["default"])(fn)) { if (insideFnList.includes(name)) { console.warn(name + ' 内部函数,无法重新定义'); result.status = 'fail'; result.message = '内部函数,无法重新定义'; } else { if (variableNameReg.test(name)) { _this.functions[name] = fn; result.status = 'success'; } else { console.warn(name + ' 变量名不符合规范'); result.status = 'fail'; result.message = '变量名不符合规范'; } } } return result; }; this.removeVariable = function (name) { var result = { status: '', message: '' }; if (name && has(_this.variables, name)) { delete _this.variables[name]; result.status = 'success'; } else { result.status = 'fail'; result.message = '变量不存在'; } return result; }; this.removeFunction = function (name) { var result = { status: '', message: '' }; if (name) { if (insideFnList.includes(name)) { result.status = 'fail'; result.message = '内部函数,无法删除'; } else { if (has(_this.functions, name)) { delete _this.functions[name]; result.status = 'success'; } else { result.status = 'fail'; result.message = '函数不存在'; } } } else { result.status = 'fail'; result.message = '函数不存在'; } return result; }; this.parse = function (exp) { try { if ((0, _isfunction["default"])(exp)) return exp(_this.variables, _this.functions);else if (!(0, _istext["default"])(exp)) return _error.ERRORTYPES.na; var compiled = (0, _compile["default"])(exp); var result = compiled({ get: _this.getVariable }, _this.functions); if (result instanceof _error.ERRORTYPES.FormulaError) { return { error: result.name, result: null }; } else { return { error: null, result: decimalFix(result) }; } } catch (err) { return { error: '#ERROR!', result: err }; } }; var _props$compat = props.compat, compat = _props$compat === void 0 ? true : _props$compat; this.variables = {}; this.insideFnList = insideFnList; this.functions = Object.assign({}, funcs); /* 兼容模式,在正式计算公司中,如果分母为0,则用1替换,但是表单设计中,比如判断常量0作为分母,需要判断为非法公式 */ if (compat) { this.functions.DIVIDE = _compatDivide["default"]; this.functions.divide = _compatDivide["default"]; } }; var _default = Formula; exports["default"] = _default;