Skip to content

Commit

Permalink
[DDW-737] Improve code readability in the formatCpuInfo function
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonmaslowski committed Jan 17, 2022
1 parent 242fc0b commit 49c0835
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions source/renderer/app/utils/formatCpuInfo.js
Expand Up @@ -5,30 +5,38 @@ import type {
} from '../../../common/types/environment.types';
import { formattedNumber } from './formatters';

type FormatArgs = {
modelName: string,
speedValue: string,
};

const format = ({ modelName, speedValue }: FormatArgs) =>
`${modelName} @ ${speedValue}GHz`;

const formatCpuInfo = (cpu: Cpu): string => {
const { model, speed } = cpu?.[0] || ({}: CpuThreadData);
if (!model) return '';

const [modelName, modelSpeedSection] = model
.split('@')
.map((stringPart) => stringPart.trim().replace(/\s+/g, ' '));
if (!speed) {
const modelSpeedSectionExpressedInGHz = !!modelSpeedSection?.match(
/GHz/
)?.[0];
if (!modelSpeedSection && !modelSpeedSectionExpressedInGHz)
return modelName;

const rawSpeedValue = modelSpeedSection.match(/([\d,.]+)\s*GHz/)?.[1];
const parsedSpeedValue = parseFloat(rawSpeedValue);
if (!parsedSpeedValue) return modelName;

const speedValue = formattedNumber(parsedSpeedValue, 2);
return `${modelName} @ ${speedValue}GHz`;

if (speed) {
const speedValue = formattedNumber(speed / 1000, 2);
return format({ modelName, speedValue });
}

const speedValue = formattedNumber(speed / 1000, 2);
return `${modelName} @ ${speedValue}GHz`;
const modelSpeedSectionExpressedInGHz = !!modelSpeedSection?.match(/GHz/);
if (!modelSpeedSection && !modelSpeedSectionExpressedInGHz) {
return modelName;
}

const rawSpeedValue = modelSpeedSection.match(/([\d,.]+)\s*GHz/)?.[1];
const parsedSpeedValue = parseFloat(rawSpeedValue);
if (!parsedSpeedValue) return modelName;

const speedValue = formattedNumber(parsedSpeedValue, 2);
return format({ modelName, speedValue });
};

export default formatCpuInfo;

0 comments on commit 49c0835

Please sign in to comment.