Socialify

Folder ..

Viewing display.ts
172 lines (162 loc) • 4.8 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { Frame } from '../index';
import { Table } from '../helpers/table';

/**
 * getTable returns the data compatible with table.table
 * @param rowdata the rowdata to be sent to the frame
 * @param columns the header columns to be sent to the frame
 * @param indexRow index of the row to be printed
 * @returns Array of arrays
 */
function getTable(
  rowdata: unknown[][],
  columns: string[],
  indexRow?: any[],
  title?: any
): any {
  const maxSizedArrayLength = rowdata.reduce((acc, curr) => {
    return acc.length > curr.length ? acc : curr;
  }).length;
  const frameRows: unknown[][] = [];
  for (let i = 0; i < rowdata.length; i++) {
    const row = rowdata[i];
    const rowLength = row.length;
    const rowArray = [];
    rowArray.push(indexRow ? indexRow[i] : i);
    for (let j = 0; j < maxSizedArrayLength; j++) {
      if (j < rowLength) {
        rowArray.push(row[j]);
      } else {
        rowArray.push('');
      }
    }
    frameRows.push(rowArray);
  }
  return {
    title: title ? title : '',
    data: [['Index', ...columns], ...frameRows]
  };
}

/**
 * displayTable prints the table to the console
 * @param rowdata the rowdata to be sent to the frame
 */
export function displayTable(rowdata: any): void {
  // Convert row data into object with keys and values, keys are the column names stored in rowdata[0]
  const headerObject: any = [];
  rowdata.data[0].forEach((column: any) => {
    headerObject.push({
      name: column,
      alignment: 'left',
      paddingLeft: 2,
      bold: true,
      paddingRight: 2
    });
  });
  const table = new Table({
    columns: headerObject,
    title: String(rowdata.title)
  });

  const rowdataObject = rowdata.data.map((row: any[]) => {
    const rowObject: any = {};
    row.forEach((value: any, index: string | number) => {
      rowObject[rowdata.data[0][index]] = value;
    });
    return rowObject;
  });

  // remove the first row from rowdataObject
  const rowdataObjectWithoutHeader = rowdataObject.slice(1);
  table.addRows(rowdataObjectWithoutHeader);
  table.printTable();
}

/**
 * show prints the frame in console.table format
 * @returns the current frame
 * @throws Error if the frame is empty
 */
export function show(this: Frame): void {
  if (this.rowdata.length === 0) {
    console.log('No data found');
  } else {
    const numberOfRows = this.rowdata.length;
    if (numberOfRows < 7) {
      displayTable(
        getTable(this.rowdata, this.columns, undefined, this.tableTitle)
      );
    } else {
      const firstThreeRows = this.rowdata.slice(0, 3);
      const lastThreeRows = this.rowdata.slice(numberOfRows - 3);
      const middleRow = [];
      for (let i = 0; i < this.columns.length; i++) {
        middleRow.push('...');
      }
      const indexRow = [
        0,
        1,
        2,
        '...',
        numberOfRows - 3,
        numberOfRows - 2,
        numberOfRows - 1
      ];
      const combinedRow = [...firstThreeRows, [...middleRow], ...lastThreeRows];
      displayTable(
        getTable(combinedRow, this.columns, indexRow, this.tableTitle)
      );
    }
  }
}

/**
 * showAll prints the frame without truncating
 * @returns the current frame
 */
export function showAll(this: Frame): void {
  if (this.rowdata.length === 0) {
    console.log('No data found');
  } else {
    displayTable(
      getTable(this.rowdata, this.columns, undefined, this.tableTitle)
    );
  }
}

/**
 * head prints maximum first n rows of the frame
 * @param n: the number of rows to be returned
 * @returns the first n rows of the frame as array of arrays
 * @throws Error if the frame is empty
 */
export function head(this: Frame, n = 5): void {
  if (this.rowdata.length === 0) {
    console.log('No data found');
  } else {
    // Check if n is greater than the number of rows
    if (n > this.rowdata.length) {
      n = this.rowdata.length;
    }

    // Generate the index row
    const indexRow = this.rowdata.map((row, index) => index);
    const data = this.rowdata.slice(0, n);
    displayTable(getTable(data, this.columns, indexRow, this.tableTitle));
  }
}

/**
 * tail prints maximum last n rows of the frame
 * @param n: the number of rows to be returned
 * @returns the last n rows of the frame as array of arrays
 * @throws Error if the frame is empty
 */
export function tail(this: Frame, n = 5): void {
  if (this.rowdata.length === 0) {
    console.log('No data found');
  } else {
    // Check if n is greater than the number of rows
    if (n > this.rowdata.length) {
      n = this.rowdata.length;
    }

    // Generate the index row
    const indexRow = this.rowdata.map((row, index) => index);
    const data = this.rowdata.slice(this.rowdata.length - n);
    // Slice the index row to match the data
    const slicedIndexRow = indexRow.slice(indexRow.length - n);
    displayTable(getTable(data, this.columns, slicedIndexRow, this.tableTitle));
  }
}