Skip to content

Commit

Permalink
Parse Finnish locale floats correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
jait authored and Tumetsu committed May 7, 2022
1 parent d4de2b2 commit d573a26
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
7 changes: 6 additions & 1 deletion src/statistics/html-parser/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function transformTableToObject(
}

// Then it is either number or a string
const number = parseFloat(value);
const number = parseFloatString(value);
if (Number.isNaN(number)) {
return value;
}
Expand Down Expand Up @@ -150,3 +150,8 @@ function parseTimeString(text: string) {
minutes,
};
}

export function parseFloatString(text: string): number {
// In Finnish locale, decimal separator is the comma
return parseFloat(text.replace(',', '.'));
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ describe('team-list parser', () => {

const first = teams[0];
expect(first).toEqual({
daysPerPerson: 143,
distancePerPerson: 9822,
daysPerPerson: 143.8,
distancePerPerson: 9822.2,
memberCount: 5,
name: 'Utajärven Pantterit',
placement: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ describe('html-parser', () => {
name: 'Testi Testersson',
fullName: 'Testi Testersson',
email: '[email protected]',
totalDistance: 1691,
distanceByRegularBike: 1691,
totalDistance: 1691.9,
distanceByRegularBike: 1691.9,
distanceByEbike: 0,
totalCyclingDays: 43,
},
Expand All @@ -23,18 +23,18 @@ describe('html-parser', () => {
name: 'Paula Pyöräilijä',
fullName: 'Paula Pyöräilijä',
email: '[email protected]',
totalDistance: 1531,
distanceByRegularBike: 844,
distanceByEbike: 687,
totalDistance: 1531.7,
distanceByRegularBike: 844.6,
distanceByEbike: 687.2,
totalCyclingDays: 55,
},
{
placement: 3,
name: 'Tsygä Tsygäilijä',
fullName: 'Tytti Fillarinen',
email: '[email protected]',
totalDistance: 1513,
distanceByRegularBike: 1513,
totalDistance: 1513.3,
distanceByRegularBike: 1513.3,
distanceByEbike: 0,
totalCyclingDays: 49,
},
Expand Down Expand Up @@ -83,8 +83,8 @@ describe('html-parser', () => {
{
placement: 1,
name: 'Tsygä Tsygäilijä',
totalDistance: 2060,
distanceByRegularBike: 2060,
totalDistance: 2060.1,
distanceByRegularBike: 2060.1,
distanceByEbike: 0,
totalCyclingDays: 41,
},
Expand Down
4 changes: 2 additions & 2 deletions src/statistics/html-parser/team-parser/team-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ describe('html-parser', () => {
series: TeamSeries.SMALL,
seriesPlacement: null,
distancePerPerson: 300,
totalDistance: 1234,
daysPerPerson: 26,
totalDistance: 1234.7,
daysPerPerson: 26.34,
totalDays: 358,
savedGas: 256,
savedCO2: 98,
Expand Down
3 changes: 2 additions & 1 deletion src/statistics/html-parser/team-parser/team-parser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as cheerio from 'cheerio';
import { KilometrikisaError, KilometrikisaErrorCode } from '../../../utils/error-handling';
import { parseFloatString } from '../common';
import { CheerioAPI } from 'cheerio';

interface DataValuePair {
Expand Down Expand Up @@ -110,7 +111,7 @@ const stringTitlesToKeys: Record<string, string> = {
function convertDataToTypedObject(pairs: DataValuePair[]) {
function extractValue(valueAndUnit: string) {
const [value] = valueAndUnit.split(' ');
const parsed = value ? parseFloat(value) : undefined;
const parsed = value ? parseFloatString(value) : undefined;
return !Number.isNaN(parsed) ? parsed : null;
}

Expand Down

0 comments on commit d573a26

Please sign in to comment.