Skip to content

Commit 22f329e

Browse files
committed
canonicalize units
1 parent d973fea commit 22f329e

4 files changed

Lines changed: 28 additions & 9 deletions

File tree

src/canonicalizeUnit.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function canonicalizeUnit(u: string): string {
2+
return u.trim().toLowerCase().replace(/µs|μs/g, 'us');
3+
}

src/normalizeBenchmarkResult.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { extractRangeInfo } from './extractRangeInfo';
44

55
export function normalizeBenchmarkResult(
66
prevBenchResult: BenchmarkResult | undefined | null,
7-
currentBenchResult: BenchmarkResult,
7+
currenBenchResult: BenchmarkResult,
88
): BenchmarkResult {
99
if (!prevBenchResult) {
10-
return currentBenchResult;
10+
return currenBenchResult;
1111
}
1212

1313
const prevUnit = prevBenchResult.unit;
14-
const currentUnit = currentBenchResult.unit;
15-
const currentRange = currentBenchResult.range;
14+
const currentUnit = currenBenchResult.unit;
15+
const currentRange = currenBenchResult.range;
1616
const currentRangeInfo = extractRangeInfo(currentRange);
1717

18-
const normalizedValue = normalizeValueByUnit(prevUnit, currentUnit, currentBenchResult.value);
19-
const normalizedUnit = currentBenchResult.value !== normalizedValue ? prevUnit : currentUnit;
18+
const normalizedValue = normalizeValueByUnit(prevUnit, currentUnit, currenBenchResult.value);
19+
const normalizedUnit = currenBenchResult.value !== normalizedValue ? prevUnit : currentUnit;
2020
const normalizedRangeInfo = currentRangeInfo
2121
? {
2222
prefix: currentRangeInfo.prefix,
@@ -25,7 +25,7 @@ export function normalizeBenchmarkResult(
2525
: undefined;
2626

2727
return {
28-
...currentBenchResult,
28+
...currenBenchResult,
2929
value: normalizedValue,
3030
unit: normalizedUnit,
3131
range: normalizedRangeInfo ? `${normalizedRangeInfo.prefix}${normalizedRangeInfo.value}` : currentRange,

src/normalizeValueByUnit.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import { canonicalizeUnit } from './canonicalizeUnit';
2+
13
export function normalizeValueByUnit(prevUnit: string, currentUnit: string, value: number): number {
24
for (const units of SUPPORTED_UNITS) {
3-
const prevUnitIndex = units.indexOf(prevUnit);
4-
const currentUnitIndex = units.indexOf(currentUnit);
5+
const prev = canonicalizeUnit(prevUnit);
6+
const current = canonicalizeUnit(currentUnit);
7+
const prevUnitIndex = units.indexOf(prev);
8+
const currentUnitIndex = units.indexOf(current);
59

610
if (prevUnitIndex >= 0 && currentUnitIndex >= 0) {
711
const unitDiff = prevUnitIndex - currentUnitIndex;

test/normalizeValueByUnit.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ describe('normalizeValueByUnit', () => {
3737
expect(normalizeValueByUnit('ops/s', 'ops/ns', 12)).toBe(12_000_000_000);
3838
});
3939

40+
it('handles microsecond symbol variants', () => {
41+
expect(normalizeValueByUnit('ms', 'µs', 12)).toBe(0.012);
42+
expect(normalizeValueByUnit('ms', 'μs', 12)).toBe(0.012);
43+
expect(normalizeValueByUnit('us', 'µs', 12)).toBe(12);
44+
expect(normalizeValueByUnit('us', 'μs', 12)).toBe(12);
45+
expect(normalizeValueByUnit('µs', 'μs', 12)).toBe(12);
46+
expect(normalizeValueByUnit('ops/µs', 'ops/s', 12_000_000)).toBe(12);
47+
});
48+
it('tolerates surrounding whitespace and case', () => {
49+
expect(normalizeValueByUnit(' S ', ' MS ', 12)).toBe(0.012);
50+
});
51+
4052
it('NOT normalize when new unit is not supported', () => {
4153
expect(normalizeValueByUnit('unknown1', 'unknown2', 12)).toBe(12);
4254
});

0 commit comments

Comments
 (0)