Socialify

Folder ..

Viewing data.ts
258 lines (250 loc) • 7.5 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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');
  }
}

/**
 * sort - sorts the rowdata based on the column index
 * @param column: the column index or column name to be sorted
 * @param order: the order of the sort
 * @returns the sorted rowdata
 */
export function sort(
  this: Frame,
  column: number | string,
  ord?: 'accending' | 'descending'
) {
  const order = ord ? ord : 'ascending';
  const colums = this.columns;
  const rowdata = this.rowdata;
  const columnIndex =
    typeof column === 'number' ? column : colums.indexOf(column);
  if (columnIndex === -1) {
    throw new Error('Invalid column index');
  }
  // sort the rowdata based on the column index, if the value is null or undefined, sort it to the end
  const sorted = rowdata.sort((a: any, b: any) => {
    if (a[columnIndex] === null || a[columnIndex] === undefined) {
      return 1;
    } else if (b[columnIndex] === null || b[columnIndex] === undefined) {
      return -1;
    } else {
      if (order === 'ascending') {
        return a[columnIndex] > b[columnIndex] ? 1 : -1;
      } else {
        return a[columnIndex] < b[columnIndex] ? 1 : -1;
      }
    }
  });
  this.rowdata = sorted;
  return this;
}

/**
 * removeDuplicates - removes the duplicate rows from a column
 * @param column: the column index or column name
 * @returns the rowdata without duplicates
 */
export function removeDuplicates(this: Frame, column: string | number): Frame {
  const columnIndex =
    typeof column === 'number' ? column : this.columns.indexOf(column);
  if (columnIndex === -1) {
    throw new Error('Invalid column index');
  }
  const rowdata = this.rowdata;

  // remove the duplicates from rowdata based on column index
  const unique = rowdata.filter((row: any[], index: number) => {
    for (let i = 0; i < index; i++) {
      if (row[columnIndex] === rowdata[i][columnIndex]) {
        return false;
      }
    }
    return true;
  });
  this.rowdata = unique;
  return this;
}