Skip to content

Commit 9bb96a6

Browse files
committed
minor code improvements
1 parent 96984a7 commit 9bb96a6

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

src/extractRangeInfo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ export function extractRangeInfo(range: string | undefined): { prefix: string; v
55

66
const matches = range.match(/(?<prefix>(\+-|±)\s*)(?<value>\d.+)/);
77

8-
if (!matches) {
8+
if (!matches || !matches.groups) {
99
return undefined;
1010
}
1111

12-
const valueString = matches.groups?.['value'];
12+
const valueString = matches.groups.value;
1313

1414
const value = Number(valueString);
1515

@@ -19,6 +19,6 @@ export function extractRangeInfo(range: string | undefined): { prefix: string; v
1919

2020
return {
2121
value,
22-
prefix: matches.groups?.prefix ?? '',
22+
prefix: matches.groups.prefix ?? '',
2323
};
2424
}

src/normalizeUnit.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ export function normalizeUnit(prevUnit: string, currentUnit: string, value: numb
66
if (prevUnitIndex >= 0 && currentUnitIndex >= 0) {
77
const unitDiff = prevUnitIndex - currentUnitIndex;
88

9-
return value * 1000 ** unitDiff;
9+
return value * UNIT_CONVERSION_MULTIPLIER ** unitDiff;
1010
}
1111
}
1212

1313
return value;
1414
}
1515

16+
const UNIT_CONVERSION_MULTIPLIER = 1000;
1617
const TIME_UNITS = ['s', 'ms', 'us', 'ns'];
1718
const ITER_UNITS = TIME_UNITS.map((unit) => `${unit}/iter`);
1819
const OPS_PER_TIME_UNIT = [...TIME_UNITS].reverse().map((unit) => `ops/${unit}`);

test/extractRangeInfo.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ describe('extractRangeInfo', () => {
2424
it('should NOT extract range with invalid number', () => {
2525
expect(extractRangeInfo('± boo')).toBeUndefined();
2626
expect(extractRangeInfo('+- boo')).toBeUndefined();
27+
expect(extractRangeInfo('± 1boo')).toBeUndefined();
28+
expect(extractRangeInfo('+- 1boo')).toBeUndefined();
2729
});
2830
});

0 commit comments

Comments
 (0)