..
Viewing
locate.ts
122 lines (115 loc) • 3.8 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 | import { flatten } from './data';
interface Izuku {
rowdata: unknown[][];
columns: string[];
}
/**
* checkIfColumnExists - check if a column exists in the frame
* @param frame: the frame to be checked
* @param column: the column index or name to be checked
* @returns true if the column exists
*/
function checkIfColumnExists(frame: Izuku, column: number | string): boolean {
if (typeof column === 'number') {
return column < frame.columns.length;
} else if (typeof column === 'string') {
return frame.columns.includes(column);
} else {
throw new Error('column must be a number or a string');
}
}
/**
* checkIfRowExists - check if a row exists in the frame
* @param frame: the frame to be checked
* @param row: the row index to be checked
* @returns true if the row exists
*/
function checkIfRowExists(frame: Izuku, row: number): boolean {
return row < frame.rowdata.length;
}
/**
* column returns a single column of the frame, option is either the column name or the column index
* @param column: the column to be returned
* @returns a single column of the frame as array of arrays
*/
export function getSingleColumnDetails(iz: Izuku, column: number | string) {
if (checkIfColumnExists(iz, column)) {
let extractedColumn: any[] = [];
let columnName = '';
if (typeof column === 'string') {
extractedColumn = iz.rowdata.map((row) => {
return row[iz.columns.indexOf(column)];
});
columnName = column;
}
if (typeof column === 'number') {
extractedColumn = iz.rowdata.map((row) => {
return row[column];
});
columnName = iz.columns[column]
? iz.columns[column]
: `column${column + 1}`;
}
extractedColumn = extractedColumn.map((item) => [item]);
return { rowd: extractedColumn, rowh: [columnName] };
} else {
throw new Error(`Column ${column} does not exist`);
}
}
/**
* getMultipleColumnDetails - returns multiple columns of the frame
* @param columns: an array of column names or indexes
* @returns an array of arrays containing the columns
*/
export function getMultipleColumnDetails(
iz: Izuku,
columns: Array<number | string>
) {
const extractedColumns: any[][] = [];
const columnNames: string[] = [];
columns.forEach((column) => {
const columnDetails = getSingleColumnDetails(iz, column);
extractedColumns.push(columnDetails.rowd);
columnNames.push(columnDetails.rowh[0]);
});
// transpose the columns
const transposedColumns = extractedColumns[0].map((col, i) => {
return extractedColumns.map((row) => row[i]);
});
return { rowd: transposedColumns, rowh: columnNames };
}
/**
* getSingleRowDetails - returns a single row of the frame
* @param row: the row to be returned
* @returns a single row of the frame as array of arrays
* @throws Error if the row does not exist
* @throws Error if the row is not a number
*/
export function getSingleRowDetails(iz: Izuku, row: number) {
if (checkIfRowExists(iz, row)) {
return { rowd: [iz.rowdata[row]], rowh: iz.columns };
} else {
throw new Error(`Row ${row} does not exist`);
}
}
/**
* getMultipleRowDetails - returns multiple rows of the frame
* @param rows: an array of row indexes
* @returns an array of arrays containing the rows
*/
export function getMultipleRowDetails(iz: Izuku, rows: Array<number>) {
const extractedRows: any[][] = [];
rows.forEach((row) => {
const rowDetails = getSingleRowDetails(iz, row);
extractedRows.push(rowDetails.rowd[0]);
});
return { rowd: extractedRows, rowh: iz.columns };
}
/**
* rangeIndex returns the element at the specified index of the complete frame
* @param index: the index of the element to be returned
* @returns the element at the specified index of the complete frame
*/
export function rangeIndex(iz: Izuku, index: number): any {
return flatten(iz.rowdata)[index];
}
|
|