UNPKG

tlojs

Version:

The Last One - The last npm package you'll need to install

78 lines (77 loc) 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TableQuery = void 0; var exceptions_1 = require("./exceptions"); var query_results_1 = require("./query-results"); var TableQuery = /** @class */ (function () { function TableQuery(query, rows) { this.query = query; this.rows = rows; this.parse(); } TableQuery.prototype.exec = function () { var _a; if (!this.start) { return new query_results_1.TableQueryResults([]); } var start = this.start; var selected = []; if (this.start.isFullCol) { var cells = this.rows.map(function (x) { return x.getCol(start.col); }); selected = selected.concat(cells); } if (((_a = this.end) === null || _a === void 0 ? void 0 : _a.isFullCol) && start.isFullCol) { var end = this.end; var totalCols = end.col - start.col; var spot_1 = 0; while (spot_1 < totalCols && totalCols > 0) { var cells = this.rows.map(function (x) { return x.getCol(start.col + spot_1 + 1); }); selected = selected.concat(cells); spot_1++; } } return new query_results_1.TableQueryResults(selected); }; TableQuery.prototype.parse = function () { if (!this.query) { throw new Error(exceptions_1.INVALID_RANGE_REFERENCE); } if (this.query.includes(':')) { var split = this.query.split(':'); var start = split[0]; var end = split[1]; this.start = this.parseCellReference(start); this.end = this.parseCellReference(end); this.end.isLastRow = true; } else { var start = this.query; this.start = this.parseCellReference(start); } }; TableQuery.prototype.parseCellReference = function (ref) { if (!ref) { throw new Error(exceptions_1.INVALID_RANGE_REFERENCE); } var results = { col: 0, row: undefined, isFullCol: true }; var startIndex = 'A'.charCodeAt(0); var split = ref.toUpperCase().split(''); var colCode = split[0].charCodeAt(0); results.col = colCode - startIndex; if (split.length > 1) { var num = Number(split[1]); if (isNaN(num)) { throw new Error(exceptions_1.INVALID_RANGE_REFERENCE); } results.row = num; } results.isFullCol = typeof results.row !== 'number'; return results; }; return TableQuery; }()); exports.TableQuery = TableQuery;