Skip to content

Commit 12cef5f

Browse files
refactor(conversion): Optimize conversion logic to avoid redundant steps
1 parent 91c7208 commit 12cef5f

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

pages/index.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,21 +252,36 @@ export default function ConverterPage() {
252252

253253
try {
254254
let allSteps = [];
255+
let decimalValue;
256+
let toDecimalSteps = [];
255257

256-
const toDecimalConversion = convertFromBaseToDecimal(standardizedInput.toUpperCase(), currentFromBase);
257-
const fromDecimalConversion = convertFromDecimalToBase(toDecimalConversion.result, currentToBase);
258+
if (currentFromBase === 10) {
259+
decimalValue = parseFloat(standardizedInput);
260+
} else {
261+
const toDecimalConversion = convertFromBaseToDecimal(standardizedInput.toUpperCase(), currentFromBase);
262+
decimalValue = toDecimalConversion.result;
263+
toDecimalSteps = toDecimalConversion.steps;
264+
}
265+
266+
let finalResult;
267+
let fromDecimalSteps = [];
258268

259-
allSteps.push(...toDecimalConversion.steps);
260-
allSteps.push(...fromDecimalConversion.steps);
269+
if (currentToBase === 10) {
270+
finalResult = String(decimalValue);
271+
} else {
272+
const fromDecimalConversion = convertFromDecimalToBase(decimalValue, currentToBase);
273+
finalResult = fromDecimalConversion.result;
274+
fromDecimalSteps = fromDecimalConversion.steps;
275+
}
261276

262-
const finalResult = fromDecimalConversion.result;
277+
allSteps.push(...toDecimalSteps, ...fromDecimalSteps);
263278

264279
setResult({
265280
original: standardizedInput.toUpperCase(),
266281
fromBase: currentFromBase,
267282
converted: finalResult,
268283
toBase: currentToBase,
269-
decimal: (currentFromBase !== 10 && currentToBase !== 10) ? toDecimalConversion.result : null
284+
decimal: (currentFromBase !== 10 && currentToBase !== 10) ? decimalValue : null
270285
});
271286
setSteps(allSteps);
272287
} catch (e) {

0 commit comments

Comments
 (0)