UNPKG

hyperformula-dc

Version:

HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas

203 lines (170 loc) 9.04 kB
import "core-js/modules/web.dom-collections.for-each.js"; import "core-js/modules/es.array.slice.js"; import "core-js/modules/es.object.to-string.js"; import "core-js/modules/es.function.name.js"; import "core-js/modules/es.array.from.js"; import "core-js/modules/es.string.iterator.js"; import "core-js/modules/es.symbol.js"; import "core-js/modules/es.symbol.description.js"; import "core-js/modules/es.symbol.iterator.js"; import "core-js/modules/es.array.iterator.js"; import "core-js/modules/web.dom-collections.iterator.js"; function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } /** * @license * Copyright (c) 2021 Handsoncode. All rights reserved. */ import { AbsoluteCellRange } from './AbsoluteCellRange'; import { absolutizeDependencies } from './absolutizeDependencies'; import { CellError, ErrorType } from './Cell'; import { ContentChanges } from './ContentChanges'; import { ArrayVertex, RangeVertex } from './DependencyGraph'; import { FormulaVertex } from './DependencyGraph/FormulaCellVertex'; import { InterpreterState } from './interpreter/InterpreterState'; import { EmptyValue, getRawValue } from './interpreter/InterpreterValue'; import { SimpleRangeValue } from './interpreter/SimpleRangeValue'; import { StatType } from './statistics'; export var Evaluator = /*#__PURE__*/function () { function Evaluator(config, stats, interpreter, lazilyTransformingAstService, dependencyGraph, columnSearch) { _classCallCheck(this, Evaluator); this.config = config; this.stats = stats; this.interpreter = interpreter; this.lazilyTransformingAstService = lazilyTransformingAstService; this.dependencyGraph = dependencyGraph; this.columnSearch = columnSearch; } _createClass(Evaluator, [{ key: "run", value: function run() { var _this = this; this.stats.start(StatType.TOP_SORT); var _this$dependencyGraph = this.dependencyGraph.topSortWithScc(), sorted = _this$dependencyGraph.sorted, cycled = _this$dependencyGraph.cycled; this.stats.end(StatType.TOP_SORT); this.stats.measure(StatType.EVALUATION, function () { _this.recomputeFormulas(cycled, sorted); }); } }, { key: "partialRun", value: function partialRun(vertices) { var _this2 = this; var changes = ContentChanges.empty(); this.stats.measure(StatType.EVALUATION, function () { _this2.dependencyGraph.graph.getTopSortedWithSccSubgraphFrom(vertices, function (vertex) { if (vertex instanceof FormulaVertex) { var currentValue = vertex.isComputed() ? vertex.getCellValue() : undefined; var newCellValue = _this2.recomputeFormulaVertexValue(vertex); if (newCellValue !== currentValue) { var address = vertex.getAddress(_this2.lazilyTransformingAstService); changes.addChange(newCellValue, address); _this2.columnSearch.change(getRawValue(currentValue), getRawValue(newCellValue), address); return true; } return false; } else if (vertex instanceof RangeVertex) { vertex.clearCache(); return true; } else { return true; } }, function (vertex) { if (vertex instanceof RangeVertex) { vertex.clearCache(); } else if (vertex instanceof FormulaVertex) { var address = vertex.getAddress(_this2.lazilyTransformingAstService); _this2.columnSearch.remove(getRawValue(vertex.valueOrUndef()), address); var error = new CellError(ErrorType.CYCLE, undefined, vertex); vertex.setCellValue(error); changes.addChange(error, address); } }); }); return changes; } }, { key: "runAndForget", value: function runAndForget(ast, address, dependencies) { var _this3 = this; var tmpRanges = []; var _iterator = _createForOfIteratorHelper(absolutizeDependencies(dependencies, address)), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var dep = _step.value; if (dep instanceof AbsoluteCellRange) { var range = dep; if (this.dependencyGraph.getRange(range.start, range.end) === undefined) { var rangeVertex = new RangeVertex(range); this.dependencyGraph.rangeMapping.setRange(rangeVertex); tmpRanges.push(rangeVertex); } } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } var ret = this.evaluateAstToCellValue(ast, new InterpreterState(address, this.config.useArrayArithmetic)); tmpRanges.forEach(function (rangeVertex) { _this3.dependencyGraph.rangeMapping.removeRange(rangeVertex); }); return ret; } /** * Recalculates formulas in the topological sort order */ }, { key: "recomputeFormulas", value: function recomputeFormulas(cycled, sorted) { var _this4 = this; cycled.forEach(function (vertex) { if (vertex instanceof FormulaVertex) { vertex.setCellValue(new CellError(ErrorType.CYCLE, undefined, vertex)); } }); sorted.forEach(function (vertex) { if (vertex instanceof FormulaVertex) { var newCellValue = _this4.recomputeFormulaVertexValue(vertex); var address = vertex.getAddress(_this4.lazilyTransformingAstService); _this4.columnSearch.add(getRawValue(newCellValue), address); } else if (vertex instanceof RangeVertex) { vertex.clearCache(); } }); } }, { key: "recomputeFormulaVertexValue", value: function recomputeFormulaVertexValue(vertex) { var address = vertex.getAddress(this.lazilyTransformingAstService); if (vertex instanceof ArrayVertex && (vertex.array.size.isRef || !this.dependencyGraph.isThereSpaceForArray(vertex))) { return vertex.setNoSpace(); } else { var formula = vertex.getFormula(this.lazilyTransformingAstService); var newCellValue = this.evaluateAstToCellValue(formula, new InterpreterState(address, this.config.useArrayArithmetic, vertex)); return vertex.setCellValue(newCellValue); } } }, { key: "evaluateAstToCellValue", value: function evaluateAstToCellValue(ast, state) { var interpreterValue = this.interpreter.evaluateAst(ast, state); if (interpreterValue instanceof SimpleRangeValue) { return interpreterValue; } else if (interpreterValue === EmptyValue && this.config.evaluateNullToZero) { return 0; } else { return interpreterValue; } } }]); return Evaluator; }();