Skip to content

Commit 4be63e4

Browse files
committed
style: add castToNumber method
1 parent 7864f52 commit 4be63e4

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

packages/app/src/hooks/useChartConfig.tsx

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,22 @@ export const splitChartConfigs = (config: ChartConfigWithOptDateRange) => {
5252
return [config];
5353
};
5454

55-
export const computeRatio = (
56-
numeratorInput: string | number,
57-
denominatorInput: string | number,
58-
) => {
59-
let numerator: number;
60-
let denominator: number;
61-
62-
if (typeof numeratorInput === 'string') {
63-
if (numeratorInput.trim() === '') {
55+
const castToNumber = (value: string | number) => {
56+
if (typeof value === 'string') {
57+
if (value.trim() === '') {
6458
return NaN;
6559
}
66-
numerator = Number(numeratorInput);
67-
} else {
68-
numerator = numeratorInput;
60+
return Number(value);
6961
}
62+
return value;
63+
};
7064

71-
if (typeof denominatorInput === 'string') {
72-
if (denominatorInput.trim() === '') {
73-
return NaN;
74-
}
75-
denominator = Number(denominatorInput);
76-
} else {
77-
denominator = denominatorInput;
78-
}
65+
export const computeRatio = (
66+
numeratorInput: string | number,
67+
denominatorInput: string | number,
68+
) => {
69+
const numerator = castToNumber(numeratorInput);
70+
const denominator = castToNumber(denominatorInput);
7971

8072
if (isNaN(numerator) || isNaN(denominator) || denominator === 0) {
8173
return NaN;

0 commit comments

Comments
 (0)