|
| 1 | +/* eslint-disable @typescript-eslint/ban-ts-comment */ |
| 2 | +import { CsvReadOptions } from '../src/loadCsv.models'; |
| 3 | +import applyMappings from '../src/applyMappings'; |
| 4 | + |
| 5 | +const data = [ |
| 6 | + ['lat', 'lng', 'height', 'temperature'], |
| 7 | + [0.234, 1.47, 849.7, 64.4], |
| 8 | + [-293.2, 103.34, 715.2, 73.4], |
| 9 | +]; |
| 10 | + |
| 11 | +const mappings: NonNullable<CsvReadOptions['mappings']> = { |
| 12 | + height: (ft) => Number(ft) * 0.3048, // feet to meters |
| 13 | + temperature: (f) => (Number(f) - 32) / 1.8, // fahrenheit to celsius |
| 14 | +}; |
| 15 | + |
| 16 | +test('Applying mappings works correctly', () => { |
| 17 | + const mappedData = applyMappings(data, mappings); |
| 18 | + // @ts-ignore |
| 19 | + expect(mappedData).toBeDeepCloseTo( |
| 20 | + [ |
| 21 | + ['lat', 'lng', 'height', 'temperature'], |
| 22 | + [0.234, 1.47, 258.98856, 18], |
| 23 | + [-293.2, 103.34, 217.99296, 23], |
| 24 | + ], |
| 25 | + 3 |
| 26 | + ); |
| 27 | +}); |
| 28 | + |
| 29 | +test('Applying mappings does not break with a table with just headers', () => { |
| 30 | + const tableOnlyHeaders = [['lat', 'lng', 'height', 'temperature']]; |
| 31 | + const mappedData = applyMappings(tableOnlyHeaders, mappings); |
| 32 | + expect(mappedData).toMatchObject(tableOnlyHeaders); |
| 33 | +}); |
| 34 | + |
| 35 | +test('Applying mappings does not break with an empty table', () => { |
| 36 | + const mappedData = applyMappings([], mappings); |
| 37 | + expect(mappedData).toMatchObject([]); |
| 38 | +}); |
0 commit comments