aboutsummaryrefslogtreecommitdiff
path: root/src/index.ts
blob: acb613d5a0c0b2b46c0621e937d30f255085cee4 (plain)
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
import { data, generateHeader, setHeader, header } from './lib/frame';
import {
  getMultipleColumnDetails,
  getSingleColumnDetails,
  getSingleRowDetails,
  getMultipleRowDetails,
  rangeIndex
} from './lib/locate';
import { show, head, tail } from './lib/display';
import { getSize, info } from './lib/info';
import { flatten } from './lib/data';
import { isArrayOfType, range } from './helpers/arrayFunctions';

class Izuku {
  rowdata: unknown[][] = [];
  columns: string[];
  size = 0;
  shape = '0 x 0';

  constructor(rowdata?: Array<unknown[]>, columns?: Array<string>) {
    this.rowdata = rowdata || [];
    this.columns = columns
      ? setHeader(this.rowdata, columns)
      : generateHeader(this.rowdata);
    this.size = getSize(this.rowdata);
    this.shape = `${this.rowdata.length} x ${this.columns.length}`;
  }

  public data = data;
  public header = header;
  public show = show;
  public head = head;
  public tail = tail;
  public info = info;
  public column = (column: number | string | Array<number> | Array<string>) => {
    // if a number or string is passed, return a single column
    if (typeof column === 'number' || typeof column === 'string') {
      const izSampler = getSingleColumnDetails(this, column);
      return new Izuku(izSampler.rowd, izSampler.rowh);
    } else if (Array.isArray(column) || isArrayOfType(column, 'string')) {
      const izSampler = getMultipleColumnDetails(this, column);
      return new Izuku(izSampler.rowd, izSampler.rowh);
    } else {
      throw new Error('Unexpected type of column');
    }
  };
  public row = (row: number | Array<number>) => {
    if (typeof row === 'number') {
      const izSampler = getSingleRowDetails(this, row);
      return new Izuku(izSampler.rowd, izSampler.rowh);
    } else if (Array.isArray(row)) {
      const izSampler = getMultipleRowDetails(this, row);
      return new Izuku(izSampler.rowd, this.columns);
    } else {
      throw new Error('Row must be an integer or an array of integers');
    }
  };
  public flatten = () => {
    return flatten(this.rowdata);
  };
  public rangeIndex = (index: number) => {
    return rangeIndex(this, index);
  };
}

class Frame extends Izuku {
  constructor(rowdata?: Array<unknown[]>, columns?: Array<string>) {
    super(rowdata, columns);
  }
}

export { Frame, range };