tlojs
Version:
The Last One - The last npm package you'll need to install
83 lines (82 loc) • 2.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Table = void 0;
var query_1 = require("./query");
var row_1 = require("./row");
var Table = /** @class */ (function () {
function Table(options) {
this.options = options;
this.rows = row_1.TableRow.fromData(options.data);
}
/**
* Load data from a raw format into a table
* @param loader
* @param data
* @returns
*/
Table.load = function (loader, data) {
var instance = new loader();
return instance.load(data);
};
/**
* Select a range in an excel-like manner and return a query of the table
* @param range
* @returns
*/
Table.prototype.select = function (range) {
var query = new query_1.TableQuery(range, this.rows);
return query.exec();
};
/**
* Empty the data from the table
*/
Table.prototype.clear = function () {
this.rows.forEach(function (x) { return x.destroy(); });
this.rows = [];
};
/**
* Get a list of the headers
* @returns string[]
*/
Table.prototype.getHeaders = function () {
var _a;
var firstRow = this.rows.firstOrDefault();
var cells = (_a = firstRow === null || firstRow === void 0 ? void 0 : firstRow.cells) !== null && _a !== void 0 ? _a : [];
return cells.select(function (x) { return x.key; });
};
/**
* Sort the table by a column index
* @param columnIndex
* @param descending
*/
Table.prototype.sort = function (columnIndex, descending) {
this.rows.sort(function (a, b) {
var aValue = undefined;
var bValue = undefined;
if (a.cells.length > columnIndex) {
aValue = a.cells[columnIndex].value;
}
if (b.cells.length > columnIndex) {
bValue = b.cells[columnIndex].value;
}
if (aValue > bValue) {
return descending ? -1 : 1;
}
if (aValue < bValue) {
return descending ? 1 : -1;
}
return 0;
});
};
/**
* Export the table
* @param exporter
* @returns
*/
Table.prototype.export = function (exporter) {
var instance = new exporter();
return instance.export(this);
};
return Table;
}());
exports.Table = Table;