Socialify

Folder ..

Viewing index.ts
105 lines (100 loc) • 3.0 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
import { data, generateHeader, setHeader, header, title } from './lib/frame';
import {
  getMultipleColumnDetails,
  getSingleColumnDetails,
  getSingleRowDetails,
  getMultipleRowDetails,
  rangeIndex
} from './lib/locate';
import { show, head, tail, showAll } from './lib/display';
import { getSize, info } from './lib/info';
import {
  flatten,
  fromJSON,
  fromCSV,
  searchValue,
  sort,
  removeDuplicates
} from './lib/data';
import { toJSON, toCSV } from './lib/export';
import { isArrayOfType, range, flattenJSON } from './helpers/arrayFunctions';

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

  constructor(
    rowdata?: Array<unknown[]>,
    columns?: Array<string>,
    tableTitle?: 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}`;
    this.tableTitle = tableTitle || '';
  }

  public data = data;
  public header = header;
  public show = show;
  public showAll = showAll;
  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);
  };
  public fromJSON = fromJSON;
  public fromCSV = fromCSV;
  public find = (
    value: string | number,
    options?: {
      row?: number | Array<number>;
      column?: number | string | Array<number> | Array<string>;
      strict?: boolean;
    }
  ) => {
    const rowdata = searchValue(this, value, options);
    return new Izuku(rowdata, this.columns);
  };
  public title = title;
  public toJSON = toJSON;
  public toCSV = toCSV;
  public sort = sort;
  public removeDuplicates = removeDuplicates;
}

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

export { Frame, range, flattenJSON };