-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): Populate feature and labels in user provided order (#217)
BREAKING CHANGE: Previously column order in the CSV file was preserved, this is no longer the case, therefore changing the output.
- Loading branch information
Showing
3 changed files
with
35 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,30 @@ | ||
import { CsvTable } from './loadCsv.models'; | ||
|
||
const filterColumns = (table: CsvTable, columnNames: string[]) => { | ||
const indexKeepDecisions = table[0].map( | ||
(header) => columnNames.indexOf(header as string) > -1 | ||
/** | ||
* Filters and re-orders columns in a given CSV table. | ||
* | ||
* Where n is the number of non-header cells in `table`, `m` is the number of header cells in `table`, and h is the number of items in `headers` | ||
* | ||
* Time complexity: O(n + mh) | ||
* | ||
* Space complexity: O(n + m + h) | ||
*/ | ||
const filterColumns = (table: CsvTable, headers: string[]) => { | ||
const indexKeepDecisions = table[0].map((columnName) => | ||
headers.includes(columnName as string) | ||
); | ||
return table.map((row) => | ||
row.filter((_, index) => indexKeepDecisions[index]) | ||
const filteredColumnNames = table[0].filter((_, i) => indexKeepDecisions[i]); | ||
const indexMap = filteredColumnNames.map((columnName) => | ||
headers.indexOf(columnName as string) | ||
); | ||
return table.map((row) => { | ||
const newRow = new Array(indexMap.length); | ||
for (let i = 0, j = 0; i < row.length; i++) { | ||
if (!indexKeepDecisions[i]) continue; | ||
newRow[indexMap[j++]] = row[i]; | ||
} | ||
return newRow; | ||
}); | ||
}; | ||
|
||
export default filterColumns; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters