UNPKG

hyperformula-dc

Version:

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

178 lines (141 loc) 6.59 kB
import "core-js/modules/web.dom-collections.for-each.js"; import "core-js/modules/es.array.iterator.js"; import "core-js/modules/es.map.js"; import "core-js/modules/es.object.to-string.js"; import "core-js/modules/es.string.iterator.js"; import "core-js/modules/web.dom-collections.iterator.js"; 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 { absolutizeDependencies } from './absolutizeDependencies'; import { ArraySize } from './ArraySize'; import { simpleCellAddress } from './Cell'; import { CellContent } from './CellContentParser'; import { ArrayVertex, FormulaCellVertex, ParsingErrorVertex, ValueCellVertex } from './DependencyGraph'; import { getRawValue } from './interpreter/InterpreterValue'; import { StatType } from './statistics'; /** * Service building the graph and mappings. */ export var GraphBuilder = /*#__PURE__*/function () { /** * Configures the building service. */ function GraphBuilder(dependencyGraph, columnSearch, parser, cellContentParser, stats, arraySizePredictor) { _classCallCheck(this, GraphBuilder); this.dependencyGraph = dependencyGraph; this.columnSearch = columnSearch; this.parser = parser; this.cellContentParser = cellContentParser; this.stats = stats; this.arraySizePredictor = arraySizePredictor; this.buildStrategy = new SimpleStrategy(dependencyGraph, columnSearch, parser, stats, cellContentParser, arraySizePredictor); } /** * Builds graph. */ _createClass(GraphBuilder, [{ key: "buildGraph", value: function buildGraph(sheets, stats) { var _this = this; var dependencies = stats.measure(StatType.COLLECT_DEPENDENCIES, function () { return _this.buildStrategy.run(sheets); }); this.dependencyGraph.getAndClearContentChanges(); stats.measure(StatType.PROCESS_DEPENDENCIES, function () { return _this.processDependencies(dependencies); }); } }, { key: "processDependencies", value: function processDependencies(dependencies) { var _this2 = this; dependencies.forEach(function (cellDependencies, endVertex) { _this2.dependencyGraph.processCellDependencies(cellDependencies, endVertex); }); } }]); return GraphBuilder; }(); export var SimpleStrategy = /*#__PURE__*/function () { function SimpleStrategy(dependencyGraph, columnIndex, parser, stats, cellContentParser, arraySizePredictor) { _classCallCheck(this, SimpleStrategy); this.dependencyGraph = dependencyGraph; this.columnIndex = columnIndex; this.parser = parser; this.stats = stats; this.cellContentParser = cellContentParser; this.arraySizePredictor = arraySizePredictor; } _createClass(SimpleStrategy, [{ key: "run", value: function run(sheets) { var _this3 = this; var dependencies = new Map(); for (var sheetName in sheets) { var sheetId = this.dependencyGraph.getSheetId(sheetName); var sheet = sheets[sheetName]; for (var i = 0; i < sheet.length; ++i) { var row = sheet[i]; var _loop = function _loop(j) { var cellContent = row[j]; var address = simpleCellAddress(sheetId, j, i); var parsedCellContent = _this3.cellContentParser.parse(cellContent); if (parsedCellContent instanceof CellContent.Formula) { var parseResult = _this3.stats.measure(StatType.PARSER, function () { return _this3.parser.parse(parsedCellContent.formula, address); }); if (parseResult.errors.length > 0) { _this3.shrinkArrayIfNeeded(address); var vertex = new ParsingErrorVertex(parseResult.errors, parsedCellContent.formula); _this3.dependencyGraph.addVertex(address, vertex); } else { _this3.shrinkArrayIfNeeded(address); var size = _this3.arraySizePredictor.checkArraySize(parseResult.ast, address); if (size.isScalar()) { var _vertex = new FormulaCellVertex(parseResult.ast, address, 0); dependencies.set(_vertex, absolutizeDependencies(parseResult.dependencies, address)); _this3.dependencyGraph.addVertex(address, _vertex); if (parseResult.hasVolatileFunction) { _this3.dependencyGraph.markAsVolatile(_vertex); } if (parseResult.hasStructuralChangeFunction) { _this3.dependencyGraph.markAsDependentOnStructureChange(_vertex); } } else { var _vertex2 = new ArrayVertex(parseResult.ast, address, new ArraySize(size.width, size.height)); dependencies.set(_vertex2, absolutizeDependencies(parseResult.dependencies, address)); _this3.dependencyGraph.addArrayVertex(address, _vertex2); } } } else if (parsedCellContent instanceof CellContent.Empty) { /* we don't care about empty cells here */ } else { _this3.shrinkArrayIfNeeded(address); var _vertex3 = new ValueCellVertex(parsedCellContent.value, cellContent); _this3.columnIndex.add(getRawValue(parsedCellContent.value), address); _this3.dependencyGraph.addVertex(address, _vertex3); } }; for (var j = 0; j < row.length; ++j) { _loop(j); } } } return dependencies; } }, { key: "shrinkArrayIfNeeded", value: function shrinkArrayIfNeeded(address) { var vertex = this.dependencyGraph.getCell(address); if (vertex instanceof ArrayVertex) { this.dependencyGraph.shrinkArrayToCorner(vertex); } } }]); return SimpleStrategy; }();