Guest User

Table Builder in JavaScript

a guest
Nov 14th, 2020
1,324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {value: true});
  3. const tslib_1 = require("tslib");
  4. const pad_1 = tslib_1.__importDefault(require("pad"));
  5. const defaultOptions = {
  6.     sortDirection: 'asc',
  7. };
  8.  
  9. class TableBuilder {
  10.     constructor(columns, options) {
  11.         this._columns = [TableBuilder._createIndexColumn()];
  12.         if (columns) {
  13.             this._columns.push(...columns);
  14.         }
  15.         this._items = [];
  16.         this._options = options ? Object.assign({}, defaultOptions, options) : defaultOptions;
  17.     }
  18.  
  19.     addRows(...rows) {
  20.         rows.forEach((row) => this._items.push(row));
  21.     }
  22.  
  23.     build() {
  24.         // Header
  25.         let result = `\`${this._buildRow(this._createHeader())}\n`;
  26.         result += pad_1.default('', this._totalWidth(), '―');
  27.         // Content
  28.         if (this._options.sortBy) {
  29.             this._sortRows();
  30.         }
  31.         this._items.forEach((row, index) => {
  32.             result += `\n${this._buildRow(row, index + 1)}`;
  33.         });
  34.         return `${result}\``;
  35.     }
  36.  
  37.     _buildRow(data, index) {
  38.         const keys = Object.keys(data).filter((key) => this._columns.find((col) => col.field === key));
  39.         keys.sort((keyA, keyB) => {
  40.             const colA = this._columns.find((col) => col.field === keyA);
  41.             const colB = this._columns.find((col) => col.field === keyB);
  42.             return colA.index - colB.index;
  43.         });
  44.         let result = index ? pad_1.default(String(index), TableBuilder._createIndexColumn().width) : '';
  45.         keys.forEach((key) => {
  46.             const column = this._columns.find((col) => col.field === key);
  47.             let content = data[key];
  48.             if (column.format && index) {
  49.                 content = column.format(data[key]);
  50.             }
  51.             result += pad_1.default(String(content), column.width);
  52.         });
  53.         return result;
  54.     }
  55.  
  56.     _createHeader() {
  57.         const header = {};
  58.         this._columns.forEach((col) => {
  59.             header[col.field] = col.label;
  60.         });
  61.         return header;
  62.     }
  63.  
  64.     _totalWidth() {
  65.         let width = 0;
  66.         this._columns.forEach((col) => (width += col.width));
  67.         return width;
  68.     }
  69.  
  70.     _sortRows() {
  71.         this._items.sort((a, b) => {
  72.             let diff = 0;
  73.             // Go through each of the sortBy columns, ordered in descending priority
  74.             for (const columnField of this._options.sortBy) {
  75.                 const field = this._columns.find((col) => col.field === columnField).field;
  76.                 diff = this._compareValues(a[field], b[field]);
  77.                 // Only continue if the cells are equal in the current column
  78.                 if (diff !== 0) {
  79.                     break;
  80.                 }
  81.             }
  82.             return diff;
  83.         });
  84.     }
  85.  
  86.     _compareValues(a, b) {
  87.         // Find the label of the column to sort by
  88.         if (this._options.sortDirection === 'desc') {
  89.             [a, b] = [b, a];
  90.         }
  91.         if (typeof a === 'string') {
  92.             return a.localeCompare(b);
  93.         } else if (typeof a === 'number') {
  94.             return a - b;
  95.         } else if (a instanceof Date) {
  96.             return a.getTime() - b.getTime();
  97.         }
  98.     }
  99.  
  100.     static _createIndexColumn() {
  101.         return {
  102.             index: 0,
  103.             label: '#',
  104.             width: 4,
  105.             field: '#',
  106.         };
  107.     }
  108. }
  109.  
  110. exports.TableBuilder = TableBuilder;
  111.  
Advertisement
Add Comment
Please, Sign In to add comment