-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
front: use new spreadsheet component
- Loading branch information
Showing
11 changed files
with
150 additions
and
169 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
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
35 changes: 6 additions & 29 deletions
35
front/src/modules/rollingStock/components/RollingStockEditor/formatSpreadSheetCurve.ts
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,39 +1,16 @@ | ||
import type { Matrix } from 'react-spreadsheet'; | ||
|
||
import type { EffortCurveForm } from 'modules/rollingStock/types'; | ||
import type { DataSheetCurve, EffortCurveForm } from 'modules/rollingStock/types'; | ||
import { kmhToMs } from 'utils/physics'; | ||
import { emptyStringRegex, onlyDigit } from 'utils/strings'; | ||
|
||
/** Remove rows which have at least 1 empty cell */ | ||
const filterUnvalidRows = (sheetValues: Matrix<{ value: string }>) => | ||
sheetValues.filter( | ||
([a, b]) => | ||
(a?.value && !emptyStringRegex.test(a.value)) || (b?.value && !emptyStringRegex.test(b.value)) | ||
); | ||
|
||
/** For each cell, filter non digit characters */ | ||
const removeNonDigitCharacters = (rows: Matrix<{ value: string }>) => | ||
rows.map((row) => row.map((cell) => onlyDigit((cell?.value || '').replaceAll(',', '.')))); | ||
|
||
const formatToEffortCurve = (rows: Matrix<string>) => | ||
const formatCurve = (rows: DataSheetCurve[]) => | ||
rows.reduce<EffortCurveForm>( | ||
(result, row) => { | ||
result.speeds.push(row[0] !== '' ? kmhToMs(Number(row[0])) : undefined); | ||
result.speeds.push(row.speed !== null ? kmhToMs(Number(row.speed)) : null); | ||
// Back-end needs effort in newton | ||
result.max_efforts.push(row[1] !== '' ? Number(row[1]) * 1000 : undefined); | ||
result.max_efforts.push(row.effort !== null ? Number(row.effort) * 1000 : null); | ||
|
||
return result; | ||
}, | ||
{ max_efforts: [], speeds: [] } | ||
); | ||
|
||
/** | ||
* Given a spreadsheet, return an EffortCurve | ||
* - remove rows which have at least 1 empty cell | ||
* - remove non digit characters in each cell | ||
* - convert rows data to EffortCurve | ||
*/ | ||
export default function formatCurve(sheetValues: Matrix<{ value: string }>) { | ||
const validRows = filterUnvalidRows(sheetValues); | ||
const numericRows = removeNonDigitCharacters(validRows); | ||
return formatToEffortCurve(numericRows); | ||
} | ||
export default formatCurve; |
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
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
Oops, something went wrong.