From e1477d251ca8d61506874e83a8b47c68cbee1ffa Mon Sep 17 00:00:00 2001 From: Anshgrover23 Date: Mon, 10 Feb 2025 19:57:12 +0530 Subject: [PATCH] support kohm unit --- src/utils/convert-si-unit-to-number.ts | 20 ++++++++++++++++++++ tests/parse-and-convert-si-unit.test.ts | 7 +++++++ 2 files changed, 27 insertions(+) diff --git a/src/utils/convert-si-unit-to-number.ts b/src/utils/convert-si-unit-to-number.ts index 1f5a0b5..e0a9fa2 100644 --- a/src/utils/convert-si-unit-to-number.ts +++ b/src/utils/convert-si-unit-to-number.ts @@ -214,6 +214,26 @@ export const parseAndConvertSiUnit = < const unit = unit_reversed.split("").reverse().join("") const numberPart = v.slice(0, -unit.length) + if (unit.toLowerCase().endsWith("ohm")) { + const prefixPart = unit.slice(0, -3) + if (prefixPart === "") { + return { + parsedUnit: unit, + unitOfValue: "Ω", + value: Number.parseFloat(numberPart) as any, + } + } + if (!(prefixPart in si_prefix_multiplier)) { + throw new Error(`Unsupported unit prefix: ${prefixPart}`) + } + const siMultiplier = + si_prefix_multiplier[prefixPart as keyof typeof si_prefix_multiplier] + return { + parsedUnit: unit, + unitOfValue: "Ω", + value: (Number.parseFloat(numberPart) * siMultiplier) as any, + } + } if ( unit in si_prefix_multiplier && !unitMappingAndVariantSuffixes.has(unit) diff --git a/tests/parse-and-convert-si-unit.test.ts b/tests/parse-and-convert-si-unit.test.ts index 3a24c4f..fefd7d3 100644 --- a/tests/parse-and-convert-si-unit.test.ts +++ b/tests/parse-and-convert-si-unit.test.ts @@ -156,4 +156,11 @@ test("parseAndConvertSiUnit", () => { unitOfValue: null, value: -6, }) + + // Test kohm unit + expect(parseAndConvertSiUnit("10kohm")).toEqual({ + parsedUnit: "kohm", + unitOfValue: "Ω", + value: 10000, + }) })