aboutsummaryrefslogtreecommitdiff
path: root/src/lib/data.ts
blob: 6a2fe34fe8c2139d931072799dfb6c8fc1c8c93c (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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { Frame } from '../index';
import { isValidJSONObject } from '../helpers/arrayFunctions';
import { readFileSync } from 'fs';

/**
 * flattenArray - flattens a 2D into a single array
 * @param array: the array to be flattened
 * @returns the flattened array
 */
export function flatten(array: any[][]): any[] {
  let flattenedArray: any[] = [];
  for (let i = 0; i < array.length; i++) {
    if (Array.isArray(array[i])) {
      flattenedArray = flattenedArray.concat(flatten(array[i]));
    } else {
      flattenedArray.push(array[i]);
    }
  }
  return flattenedArray;
}

/**
 * fromJSON - converts a JSON string into a rowdata - framedata is a 2D array
 * @param json: the JSON string to be converted
 * @returns the rowdata
 */
export function fromJSON(this: Frame, json: any): Frame {
  if (isValidJSONObject(json)) {
    const header: string[] = [];
    const rowdata: any[][] = [];
    // loop through each row
    for (let i = 0; i < json.length; i++) {
      // get all the keys and store them in the header
      for (const key in json[i]) {
        if (!header.includes(key)) {
          header.push(key);
        }
      }
    }
    // loop through each row
    for (let i = 0; i < json.length; i++) {
      // get the values of the keys present in the header, if the values are not present in the row, add null
      const row: any[] = [];
      for (let j = 0; j < header.length; j++) {
        if (json[i][header[j]] !== undefined) {
          row.push(json[i][header[j]]);
        } else {
          row.push(null);
        }
      }
      rowdata.push(row);
    }
    this.rowdata = rowdata;
    this.columns = header;
    return this;
  } else {
    throw new Error('Invalid JSON');
  }
}

/**
 * fromCSV - converts a CSV string into a rowdata - framedata is a 2D array
 * @param csv: the CSV string to be converted
 * @returns the rowdata
 */
export function fromCSV(this: Frame, csvpath: string): Frame {
  // read the csv file
  const csv = readFileSync(csvpath, 'utf8');
  const rowdata: any[][] = [];
  const rows: string[] = csv.split('\n');
  for (let i = 0; i < rows.length; i++) {
    const row: string[] = rows[i].split(',');
    rowdata.push(row);
  }
  this.rowdata = rowdata;
  if (this.columns.length === 0) {
    this.columns = rowdata[0];
    // remove the first row
    this.rowdata.shift();
  }

  // if last row contains only empty values, remove it
  if (this.rowdata[this.rowdata.length - 1].every((item) => item === '')) {
    this.rowdata.pop();
  }
  return this;
}

/**
 * searchValue = searchValue all the rows that contain the passed string or number
 * @param value: the value to be searched
 * @returns the rowdata that contains the value
 */
export function searchValue(
  iz: Frame,
  value: string | number,
  options?: {
    row?: number | Array<number>;
    column?: number | string | Array<number> | Array<string>;
    strict?: boolean;
  }
): any[][] {
  options = options || {};
  if (!options?.strict) {
    options['strict'] = false;
  }
  // if no options are passed search everything
  if (!options || (!options.row && !options.column)) {
    const rowdata: any[][] = [];
    for (let i = 0; i < iz.rowdata.length; i++) {
      for (let j = 0; j < iz.rowdata[i].length; j++) {
        if (options.strict) {
          if (String(iz.rowdata[i][j]) === String(value)) {
            rowdata.push(iz.rowdata[i]);
            break;
          }
        } else {
          if (String(iz.rowdata[i][j]).includes(String(value))) {
            rowdata.push(iz.rowdata[i]);
            break;
          }
        }
      }
    }
    return rowdata;
  } else if (options.row && !options.column) {
    // if only row is passed, search the row
    const rowdata: any[][] = [];
    // get those particular rows
    const rows = iz.row(options.row).rowdata;
    for (let i = 0; i < rows.length; i++) {
      for (let j = 0; j < rows[i].length; j++) {
        if (options.strict) {
          if (String(rows[i][j]) === String(value)) {
            rowdata.push(rows[i]);
            break;
          }
        } else {
          if (String(rows[i][j]).includes(String(value))) {
            rowdata.push(rows[i]);
            break;
          }
        }
      }
    }
    return rowdata;
  } else if (options.column && !options.row) {
    // if only column is passed, search the column
    const columnIndexes: number[] = [];
    // get those particular columns
    const columns = iz.column(options.column).rowdata;
    console.log(columns);
    for (let i = 0; i < columns.length; i++) {
      for (let j = 0; j < columns[i].length; j++) {
        if (options.strict) {
          if (String(columns[i][j]) === String(value)) {
            columnIndexes.push(i);
            break;
          }
        } else {
          if (String(columns[i][j]).includes(String(value))) {
            columnIndexes.push(i);
            break;
          }
        }
      }
    }
    console.log(columnIndexes);
    return iz.row(columnIndexes).rowdata;
  } else if (options.column && options.row) {
    // if both row and column are passed, search the row and column
    // get those particular rows
    const rows = iz.row(options.row);
    const columns = rows.column(options.column).rowdata;
    const columnIndexes: number[] = [];
    for (let i = 0; i < columns.length; i++) {
      for (let j = 0; j < columns[i].length; j++) {
        if (options.strict) {
          if (String(columns[i][j]) === String(value)) {
            columnIndexes.push(i);
            break;
          }
        } else {
          if (String(columns[i][j]).includes(String(value))) {
            columnIndexes.push(i);
            break;
          }
        }
      }
    }
    return rows.row(columnIndexes).rowdata;
  } else {
    throw new Error('Invalid options');
  }
}