Skip to content

Commit 5d5b149

Browse files
committed
chore: update weight units to include milligram
1 parent c5a3a23 commit 5d5b149

6 files changed

Lines changed: 42 additions & 14 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
<p align="center">
1212
<a href="https://github.com/BlankRiser/unit-conversion/actions?query=branch%3Amain"><img src="https://github.com/BlankRiser/unit-conversion/actions/workflows/release.yml/badge.svg?event=push&branch=main" alt="Zod CI status" /></a>
13-
<a href="https://opensource.org/licenses/apache-2-0" rel="nofollow"><img src="https://img.shields.io/github/license/Blankriser/unit-conversion" alt="License"></a>
13+
<a href="https://opensource.org/licenses/apache-2-0" rel="nofollow"><img src="https://img.shields.io/github/license/Blankriser/unit-conversion" alt="license"></a>
14+
<a href="https://www.npmjs.com/package/@devhaven/unit-conversion" rel="nofollow"><img src="https://img.shields.io/npm/v/@devhaven/unit-conversion" alt="npm"></a>
15+
<a href="https://jsr.io/@devhaven/unit-conversion" rel="nofollow"><img src="https://img.shields.io/jsr/v/@devhaven/unit-conversion" alt="jsr"></a>
1416
</p>
1517

1618

src/constants/units.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type LengthUnits =
99
| "foot"
1010
| "yard"
1111
| "mile";
12-
export type WeightUnits = "gram" | "kilogram" | "pound" | "ounce" | "ton";
12+
export type WeightUnits = "milligram" | "gram" | "kilogram" | "pound" | "ounce" | "ton";
1313

1414
export type VolumeUnits =
1515
| "liter"
@@ -34,12 +34,20 @@ export type TimeUnits =
3434
| "month"
3535
| "year";
3636

37+
export type NumberUnits =
38+
| "base10"
39+
| "binary"
40+
| "hexadecimal"
41+
| "base2"
42+
| "base8"
43+
3744
export type AllUnits =
3845
| TemperatureUnits
3946
| LengthUnits
4047
| WeightUnits
4148
| VolumeUnits
42-
| TimeUnits;
49+
| TimeUnits
50+
| NumberUnits
4351

4452
export type UnitCategory<T extends AllUnits> = T extends TemperatureUnits
4553
? TemperatureUnits
@@ -51,14 +59,17 @@ export type UnitCategory<T extends AllUnits> = T extends TemperatureUnits
5159
? VolumeUnits
5260
: T extends TimeUnits
5361
? TimeUnits
54-
: never;
62+
: T extends NumberUnits
63+
? NumberUnits
64+
: never;
5565

5666
export type ALL_UNITS = {
5767
temperature: TemperatureUnits[];
5868
length: LengthUnits[];
5969
weight: WeightUnits[];
6070
volume: VolumeUnits[];
6171
time: TimeUnits[];
72+
number: NumberUnits[];
6273
};
6374

6475
/**
@@ -85,7 +96,7 @@ export const UNITS: ALL_UNITS = {
8596
"yard",
8697
"mile",
8798
],
88-
weight: ["gram", "kilogram", "pound", "ounce", "ton"],
99+
weight: ["milligram","gram", "kilogram", "pound", "ounce", "ton"],
89100
volume: [
90101
"liter",
91102
"milliliter",
@@ -109,4 +120,7 @@ export const UNITS: ALL_UNITS = {
109120
"month",
110121
"year",
111122
],
123+
number: ["base10", "binary", "hexadecimal", "base2", "base8"],
112124
};
125+
126+
export const NO_UNITS_CATEGORIES = ["number"]

src/main.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type ConversionConfig, DEFAULT_CONFIG } from "./constants/config";
22
import { LABELS } from "./constants/labels";
3-
import type { AllUnits, UnitCategory } from "./constants/units";
3+
import { NO_UNITS_CATEGORIES, type AllUnits, type UnitCategory } from "./constants/units";
44
import { getUnitCategory } from "./utils/common";
55
import {
66
CONVERSION_FACTORS,
@@ -45,7 +45,7 @@ class ValueWithFrom<T extends number> {
4545
*/
4646
class FromUnit<T extends number, F extends AllUnits> {
4747
constructor(
48-
private val: T,
48+
private value: T,
4949
private fromUnit: F,
5050
private config: ConversionConfig
5151
) {}
@@ -64,7 +64,7 @@ class FromUnit<T extends number, F extends AllUnits> {
6464
}
6565

6666
// Convert from source unit to base unit, then to target unit
67-
const baseValue = fromUnitFactors.toBase(this.val);
67+
const baseValue = fromUnitFactors.toBase(this.value);
6868
const numericResult = toUnitFactors.fromBase(baseValue);
6969

7070
const finalValue =
@@ -75,6 +75,9 @@ class FromUnit<T extends number, F extends AllUnits> {
7575
// Return with unit label if includeUnit is true (default)
7676
if (this.config.includeUnit !== false) {
7777
const unitLabel = LABELS[unit as keyof typeof LABELS] || unit;
78+
if (NO_UNITS_CATEGORIES.includes(category.toString())) {
79+
return finalValue;
80+
}
7881
return `${finalValue}${unitLabel}`;
7982
}
8083

src/utils/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export function getUnitCategory(unit: AllUnits): keyof typeof CONVERSION_FACTORS
2626
if (isUnitInCategory(unit, "weight")) return "weight";
2727
if (isUnitInCategory(unit, "volume")) return "volume";
2828
if (isUnitInCategory(unit, "time")) return "time";
29+
if (isUnitInCategory(unit, "number")) return "number";
2930

3031
throw new Error(`Unknown unit: ${unit}`);
3132
}
@@ -38,4 +39,4 @@ function isUnitInCategory<T extends keyof typeof UNITS>(
3839
// We deliberately widen the array element type to AllUnits for the includes check,
3940
// then use the type predicate to expose the precise narrowed type to callers.
4041
return (UNITS[category] as ReadonlyArray<AllUnits>).includes(unit);
41-
}
42+
}

src/utils/conversion-factors.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ export interface ConversionFactorGroups {
44
};
55
}
66

7+
type ConversionFactorCallback = (v: number) => number;
8+
79
export interface ConversionFactor {
810
isBaseUnit?: boolean;
9-
toBase: (v: number) => number;
10-
fromBase: (v: number) => number;
11+
toBase: ConversionFactorCallback;
12+
fromBase: ConversionFactorCallback;
1113
}
1214

1315
/**
1416
* Comprehensive conversion factors for various unit categories.
15-
*
17+
*
1618
* This object contains conversion factors organized by unit categories (temperature, length, weight, volume, time).
1719
* Each category defines a base unit and conversion functions to/from that base unit for all supported units
1820
* in that category.
19-
*
21+
*
2022
* @constant
2123
* @type {ConversionFactorGroups}
22-
*
24+
*
2325
* @property {Object} temperature - Temperature conversions with Kelvin as base unit
2426
* @property {Object} length - Length conversions with meter as base unit
2527
* @property {Object} weight - Weight conversions with gram as base unit
@@ -88,6 +90,11 @@ export const CONVERSION_FACTORS: ConversionFactorGroups = {
8890
toBase: (v: number) => v,
8991
fromBase: (v: number) => v,
9092
},
93+
milligram: {
94+
isBaseUnit: true,
95+
toBase: (v: number) => v / 1000,
96+
fromBase: (v: number) => v * 1000,
97+
},
9198
kilogram: {
9299
toBase: (v: number) => v * 1000,
93100
fromBase: (v: number) => v / 1000,

tests/conversion.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ test("Test weight conversions", ()=>{
8484
expect(convert.value(weightValue).from("gram").to("pound")).toBe("0.0022lb");
8585
expect(convert.value(weightValue).from("gram").to("ounce")).toBe("0.0353oz");
8686
expect(convert.value(weightValue).from("gram").to("ton")).toBe("0ton");
87+
expect(convert.value(weightValue).from("gram").to("milligram")).toBe("1000mg");
8788

8889
expect(convert.value(weightValue).from("kilogram").to("gram")).toBe("1000g");
8990
expect(convert.value(weightValue).from("kilogram").to("pound")).toBe("2.2046lb");

0 commit comments

Comments
 (0)