Socialify

Folder ..

Viewing frame.ts
96 lines (89 loc) • 2.4 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
import { Frame } from '../index';
import { getSize } from './info';
/**
 * data prints the data of the frame in console.table format. It also sets the new data to the frame if data is passed as a parameter
 * @param rowdata: the rowdata to be sent to the frame
 * @returns the current frame
 */
export function data(
  this: Frame,
  rowdata?: Array<unknown[]>
): unknown[][] | any {
  if (rowdata) {
    this.rowdata = rowdata;
    this.size = getSize(this.rowdata);
  }

  return this;
}

/**
 * header sets the names of the columns of the frame
 * @param header: the header to be attached to the frame
 * @returns the current frame
 */
export function header(
  this: Frame,
  header?: Array<string>
): Array<string> | any {
  if (!header?.length) {
    this.columns = generateHeader(this.rowdata);
  } else {
    this.columns = setHeader(this.rowdata, header);
  }
  return this;
}

/**
 * setHeader sets the names of the columns of the frame
 * @param rowdata: the rowdata to be sent to the frame
 * @param header: the header to be attached to the frame
 * @returns a new header
 */

export function setHeader(rowdata: any[][], header: any[]): Array<string> {
  if (rowdata?.length) {
    const maxSizedArrayLength = rowdata.reduce((acc, curr) => {
      return acc.length > curr.length ? acc : curr;
    }).length;
    const newHeaderArray = Array(maxSizedArrayLength).fill('');
    for (let i = 0; i < maxSizedArrayLength; i++) {
      if (header[i]) {
        newHeaderArray[i] = header[i];
      } else {
        newHeaderArray[i] = `Column ${i + 1}`;
      }
    }
    return newHeaderArray;
  } else {
    return header;
  }
}

/**
 * generateHeader generates the names of the columns of the frame
 * @param rowdata: the rowdata to be sent to the frame
 * @returns a new header
 */

export function generateHeader(rd: Array<any[]>): Array<string> {
  if (rd?.length) {
    const maxSizedArrayLength = rd.reduce((acc, curr) => {
      return acc.length > curr.length ? acc : curr;
    }).length;
    const header: Array<string> = [];
    for (let i = 0; i < maxSizedArrayLength; i++) {
      header.push(`Column ${i + 1}`);
    }
    return header;
  } else {
    return [];
  }
}

/**
 * title sets the title of the frame
 * @param title: the title to be attached to the frame
 * @returns the current frame
 */
export function title(this: Frame, title?: string): string | any {
  if (title) {
    this.tableTitle = title;
  } else {
    this.tableTitle = '';
  }
  return this;
}