uconvert 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +153 -0
- package/package.json +10 -2
package/README.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
# uconvert
|
|
2
|
+
|
|
3
|
+
Unit conversion utilities.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install uconvert
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### `convert(value, options)`
|
|
14
|
+
|
|
15
|
+
Converts a numeric value from one unit to another.
|
|
16
|
+
|
|
17
|
+
**Parameters:**
|
|
18
|
+
|
|
19
|
+
- `value` (number) — The value in the source unit.
|
|
20
|
+
- `options` (object):
|
|
21
|
+
- `fromUnits` — Source unit (use `MetricUnits` or `ImperialUnits`).
|
|
22
|
+
- `toUnits` — Target unit. Must be the same dimension as `fromUnits` (e.g. length ↔ length), or an error is thrown.
|
|
23
|
+
- `roundTo` (optional) — Number of decimal places to round the result. Omit to return the unrounded value.
|
|
24
|
+
|
|
25
|
+
**Returns:** The value in the target unit (optionally rounded).
|
|
26
|
+
|
|
27
|
+
**Example:**
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { convert, MetricUnits, ImperialUnits } from "uconvert";
|
|
31
|
+
|
|
32
|
+
// 5 feet to meters, rounded to 2 decimals
|
|
33
|
+
convert(5, {
|
|
34
|
+
fromUnits: ImperialUnits.FT,
|
|
35
|
+
toUnits: MetricUnits.M,
|
|
36
|
+
roundTo: 2,
|
|
37
|
+
});
|
|
38
|
+
// => 1.52
|
|
39
|
+
|
|
40
|
+
// Same unit: returns value unchanged
|
|
41
|
+
convert(100, { fromUnits: MetricUnits.CM, toUnits: MetricUnits.CM });
|
|
42
|
+
// => 100
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### `round(value, decimalPlaces?)`
|
|
46
|
+
|
|
47
|
+
Rounds a number to a given number of decimal places.
|
|
48
|
+
|
|
49
|
+
**Parameters:**
|
|
50
|
+
|
|
51
|
+
- `value` (number) — The number to round.
|
|
52
|
+
- `decimalPlaces` (optional) — Number of digits after the decimal point. Omit to return the value unchanged.
|
|
53
|
+
|
|
54
|
+
**Returns:** The rounded number (or the original number if `decimalPlaces` is omitted).
|
|
55
|
+
|
|
56
|
+
**Example:**
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { round } from "uconvert";
|
|
60
|
+
|
|
61
|
+
round(1.2345, 2); // => 1.23
|
|
62
|
+
round(1.2345); // => 1.2345
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Exported types and enums
|
|
66
|
+
|
|
67
|
+
Use these with `convert` for type-safe unit arguments:
|
|
68
|
+
|
|
69
|
+
| Export | Description |
|
|
70
|
+
|--------|-------------|
|
|
71
|
+
| `ConvertOptions` | Options object for `convert`: `{ fromUnits, toUnits, roundTo? }`. |
|
|
72
|
+
| `Units` | Union type: `MetricUnits \| ImperialUnits`. |
|
|
73
|
+
| `MetricUnits` | Enum: `CM`, `M`, `KG`, `KM_H`. |
|
|
74
|
+
| `ImperialUnits` | Enum: `IN`, `FT`, `LB`, `MPH`. |
|
|
75
|
+
| `UnitSystem` | Enum: `METRIC`, `IMPERIAL`. |
|
|
76
|
+
| `Dimension` | Enum: `LENGTH`, `WEIGHT`, `SPEED`. |
|
|
77
|
+
|
|
78
|
+
`fromUnits` and `toUnits` must use the same dimension (e.g. both length, or both weight); otherwise `convert` throws.
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Height utility
|
|
83
|
+
|
|
84
|
+
The `height` object provides helpers for converting between centimeters and feet–inches and for parsing feet–inches strings.
|
|
85
|
+
|
|
86
|
+
**Import:**
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
import { height } from "uconvert";
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### `height.toFeetInches(valueInCm, roundTo?)`
|
|
93
|
+
|
|
94
|
+
Converts a height in centimeters to feet and inches.
|
|
95
|
+
|
|
96
|
+
**Parameters:**
|
|
97
|
+
|
|
98
|
+
- `valueInCm` (number) — Height in centimeters.
|
|
99
|
+
- `roundTo` (optional) — Number of decimal places for the inches part. Omit for unrounded.
|
|
100
|
+
|
|
101
|
+
**Returns:** A `FeetInches` tuple `[feet, inches]`.
|
|
102
|
+
|
|
103
|
+
**Example:**
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
height.toFeetInches(170); // => [5, 6.93...]
|
|
107
|
+
height.toFeetInches(170, 1); // => [5, 6.9]
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### `height.toCentimeters(feetInches)`
|
|
111
|
+
|
|
112
|
+
Converts a feet–inches tuple to centimeters.
|
|
113
|
+
|
|
114
|
+
**Parameters:**
|
|
115
|
+
|
|
116
|
+
- `feetInches` — A `FeetInches` tuple `[feet, inches]` (e.g. from `toFeetInches` or `parseFeetInches`).
|
|
117
|
+
|
|
118
|
+
**Returns:** Height in centimeters (number).
|
|
119
|
+
|
|
120
|
+
**Example:**
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
height.toCentimeters([5, 10]); // => 177.8
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### `height.parseFeetInches(input)`
|
|
127
|
+
|
|
128
|
+
Parses a string into a `[feet, inches]` tuple. Accepts formats like `"5 ft 10 in"`, `"5'10\""`, `"5 10"`, and variations with "feet"/"foot"/"inches"/"inch".
|
|
129
|
+
|
|
130
|
+
**Parameters:**
|
|
131
|
+
|
|
132
|
+
- `input` (string) — String to parse.
|
|
133
|
+
|
|
134
|
+
**Returns:** A `FeetInches` tuple `[feet, inches]`. Returns `[0, 0]` if parsing fails or input is not a string.
|
|
135
|
+
|
|
136
|
+
**Example:**
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
height.parseFeetInches("5 ft 10 in"); // => [5, 10]
|
|
140
|
+
height.parseFeetInches("5'10\""); // => [5, 10]
|
|
141
|
+
height.parseFeetInches("6 2"); // => [6, 2]
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### `FeetInches` type
|
|
145
|
+
|
|
146
|
+
Tuple type `[number, number]`: first element is feet, second is inches. Use it when passing or receiving values from `toFeetInches`, `toCentimeters`, and `parseFeetInches`.
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
import { height, type FeetInches } from "uconvert";
|
|
150
|
+
|
|
151
|
+
const fi: FeetInches = height.toFeetInches(170);
|
|
152
|
+
height.toCentimeters(fi);
|
|
153
|
+
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uconvert",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
"example": "npm run build && node example/run.mjs",
|
|
20
20
|
"example:cjs": "node example/run.cjs",
|
|
21
21
|
"format": "prettier --write .",
|
|
22
|
-
"
|
|
22
|
+
"postinstall": "husky",
|
|
23
|
+
"test": "jest"
|
|
23
24
|
},
|
|
24
25
|
"repository": {
|
|
25
26
|
"type": "git",
|
|
@@ -33,8 +34,15 @@
|
|
|
33
34
|
"url": "https://github.com/AndrewTkachuk42/uconvert/issues"
|
|
34
35
|
},
|
|
35
36
|
"homepage": "https://github.com/AndrewTkachuk42/uconvert#readme",
|
|
37
|
+
"overrides": {
|
|
38
|
+
"minimatch": "^10.2.2"
|
|
39
|
+
},
|
|
36
40
|
"devDependencies": {
|
|
41
|
+
"@types/jest": "^29.5.14",
|
|
42
|
+
"husky": "^9.1.7",
|
|
43
|
+
"jest": "^30.2.0",
|
|
37
44
|
"prettier": "^3.4.2",
|
|
45
|
+
"ts-jest": "^29.4.6",
|
|
38
46
|
"prettier-plugin-organize-imports": "^4.1.0",
|
|
39
47
|
"tsup": "^8.5.1",
|
|
40
48
|
"typescript": "^5.9.3"
|