-
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): Column value mapping capability (#64)
* feat(lib): Column value mapping capability * test(lib): Tests for applyMappings * chore(config): Ignore shrinkwrap.yaml * fix(lib): Fix mappings implementation Co-authored-by: Glitch (isair-tensorflow-load-csv) <none>
- Loading branch information
Showing
6 changed files
with
78 additions
and
0 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 |
---|---|---|
|
@@ -24,3 +24,4 @@ pnpm-debug.log* | |
dist | ||
docs | ||
coverage | ||
shrinkwrap.yaml |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { CsvTable, CsvReadOptions } from './loadCsv.models'; | ||
|
||
const applyMappings = ( | ||
table: CsvTable, | ||
mappings: NonNullable<CsvReadOptions['mappings']> | ||
) => { | ||
if (table.length < 2) { | ||
return table; | ||
} | ||
const mappingsByIndex = table[0].map((columnName) => mappings[columnName]); | ||
return table.map((row, index) => | ||
index === 0 | ||
? row | ||
: row.map((value, columnIndex) => | ||
mappingsByIndex[columnIndex] | ||
? mappingsByIndex[columnIndex](value) | ||
: value | ||
) | ||
); | ||
}; | ||
|
||
export default applyMappings; |
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
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import { CsvReadOptions } from '../src/loadCsv.models'; | ||
import applyMappings from '../src/applyMappings'; | ||
|
||
const data = [ | ||
['lat', 'lng', 'height', 'temperature'], | ||
[0.234, 1.47, 849.7, 64.4], | ||
[-293.2, 103.34, 715.2, 73.4], | ||
]; | ||
|
||
const mappings: NonNullable<CsvReadOptions['mappings']> = { | ||
height: (ft) => Number(ft) * 0.3048, // feet to meters | ||
temperature: (f) => (Number(f) - 32) / 1.8, // fahrenheit to celsius | ||
}; | ||
|
||
test('Applying mappings works correctly', () => { | ||
const mappedData = applyMappings(data, mappings); | ||
// @ts-ignore | ||
expect(mappedData).toBeDeepCloseTo( | ||
[ | ||
['lat', 'lng', 'height', 'temperature'], | ||
[0.234, 1.47, 258.98856, 18], | ||
[-293.2, 103.34, 217.99296, 23], | ||
], | ||
3 | ||
); | ||
}); | ||
|
||
test('Applying mappings does not break with a table with just headers', () => { | ||
const tableOnlyHeaders = [['lat', 'lng', 'height', 'temperature']]; | ||
const mappedData = applyMappings(tableOnlyHeaders, mappings); | ||
expect(mappedData).toMatchObject(tableOnlyHeaders); | ||
}); | ||
|
||
test('Applying mappings does not break with an empty table', () => { | ||
const mappedData = applyMappings([], mappings); | ||
expect(mappedData).toMatchObject([]); | ||
}); |