Skip to content

Commit ccf520f

Browse files
authored
Merge pull request #6 from BlankRiser/new-units
Feature: Better unit returns and addition of pressure and energy units
2 parents 2c25c97 + 6d3805e commit ccf520f

24 files changed

Lines changed: 711 additions & 325 deletions

apps/docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@astrojs/starlight": "^0.35.2",
14-
"@devhaven/unit-conversion": "0.0.6",
14+
"@devhaven/unit-conversion": "workspace:*",
1515
"astro": "^5.6.1",
1616
"expressive-code-twoslash": "0.5.3",
1717
"sharp": "^0.34.2",

apps/docs/src/content/docs/api-reference.mdx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { Conversion } from "@devhaven/unit-conversion";
2424

2525
const conversion = new Conversion({ decimals: 4 });
2626
const result = conversion.value(10).from("meter").to("foot");
27+
// ^?
2728
```
2829

2930
##### isFloat?: boolean
@@ -39,15 +40,3 @@ const conversion = new Conversion({ isFloat: true });
3940
const result = conversion.value(10).from("meter").to("foot");
4041
```
4142

42-
##### includeUnit?: boolean
43-
44-
- Optional
45-
- Default: false
46-
- If true, the conversion result will include the unit as a string.
47-
48-
```ts twoslash
49-
import { Conversion } from "@devhaven/unit-conversion";
50-
51-
const conversion = new Conversion({ includeUnit: false });
52-
const result = conversion.value(10).from("meter").to("foot");
53-
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
title: "Energy"
3+
template: doc
4+
---
5+
6+
The energy category supports conversions between Joule, Kilojoule, Calorie, Calorie (International Table), Calorie (Thermochemical), Watt-Hour, Kilowatt-Hour and Electron-Volt.
7+
8+
## Supported Units
9+
10+
- Joule `J`
11+
- Kilojoule `kJ`
12+
- Calorie `cal`
13+
- Calorie International Table `cal (IT)`
14+
- Calorie Thermochemical `cal (th)`
15+
- Watt-Hour `Wh`
16+
- Kilowatt-Hour `kWh`
17+
- Electron-Volt `eV`
18+
19+
## Quick Example
20+
21+
```ts
22+
import { Conversion } from "@devhaven/unit-conversion";
23+
24+
const convert = new Conversion();
25+
26+
convert.value(0).from("joule").to("kilojoule");
27+
convert.value(212).from("kilojoule").to("joule");
28+
```
29+
30+
## Common Conversions
31+
32+
### Joule ↔ Kilojoule
33+
```ts
34+
convert.value(1).from("joule").to("kilojoule");
35+
convert.value(1).from("kilojoule").to("joule");
36+
```
37+
38+
### Joule ↔ Electron-Volt
39+
```ts
40+
convert.value(1).from("joule").to("electron-volt");
41+
convert.value(1).from("electron-volt").to("joule");
42+
```
43+
44+
### Kilojoule ↔ Calorie
45+
```ts
46+
convert.value(1).from("kilojoule").to("calorie");
47+
convert.value(1).from("calorie").to("kilojoule");
48+
```
49+
50+
### Calorie ↔ Electron Volt
51+
```ts
52+
convert.value(1).from("calorie").to("electron-volt");
53+
convert.value(1).from("electron-volt").to("calorie");
54+
```
55+
56+
### Calorie ↔ Electron Volt
57+
```ts
58+
convert.value(1).from("calorie").to("electron-volt");
59+
convert.value(1).from("electron-volt").to("calorie");
60+
```

apps/docs/src/content/docs/conversions/length.mdx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ This category is useful for engineering, construction, design, or any domain whe
99

1010
## Supported Units
1111

12-
- Metric: millimeter (mm), centimeter (cm), meter (m), kilometer (km)
13-
- Imperial / US: inch (in), foot (ft), yard (yd), mile (mi)
12+
- Metric: millimeter `mm`, centimeter `cm`, meter `m`, kilometer `km`
13+
- Imperial / US: inch `in`, foot `ft`, yard `yd`, mile `mi`
1414

1515
## Quick Example
1616

@@ -44,36 +44,36 @@ convert.value(1).from("meter").to("foot"); // 3.28
4444
### Meters ↔ Kilometers
4545

4646
```ts
47-
convert.value(1000).from("meter").to("kilometer"); // "1km"
48-
convert.value(2).from("kilometer").to("meter"); // "2000m"
47+
convert.value(1000).from("meter").to("kilometer");
48+
convert.value(2).from("kilometer").to("meter");
4949
```
5050

5151
### Meters ↔ Feet
5252

5353
```ts
54-
convert.value(1).from("meter").to("foot"); // "3.28ft"
55-
convert.value(10).from("foot").to("meter"); // "3.05m"
54+
convert.value(1).from("meter").to("foot");
55+
convert.value(10).from("foot").to("meter");
5656
```
5757

5858
### Inches ↔ Centimeters
5959

6060
```ts
61-
convert.value(1).from("inch").to("centimeter"); // "2.54cm"
62-
convert.value(30).from("centimeter").to("inch"); // "11.81in"
61+
convert.value(1).from("inch").to("centimeter");
62+
convert.value(30).from("centimeter").to("inch");
6363
```
6464

6565
### Miles ↔ Kilometers
6666

6767
```ts
68-
convert.value(1).from("mile").to("kilometer"); // "1.609km"
69-
convert.value(5).from("kilometer").to("mile"); // "3.11mi"
68+
convert.value(1).from("mile").to("kilometer");
69+
convert.value(5).from("kilometer").to("mile");
7070
```
7171

7272
### Yards ↔ Meters
7373

7474
```ts
75-
convert.value(1).from("yard").to("meter"); // "0.914m"
76-
convert.value(50).from("meter").to("yard"); // "54.68yd"
75+
convert.value(1).from("yard").to("meter");
76+
convert.value(50).from("meter").to("yard");
7777
```
7878

7979
## Configuration Options
@@ -86,8 +86,8 @@ import { Conversion } from "@devhaven/unit-conversion";
8686
const convert = new Conversion({
8787
decimals: 4, // set precision
8888
isFloat: true, // keep decimals or round
89-
includeUnit: true, // whether to append unit symbol
9089
});
9190

92-
console.log(convert.value(1).from("mile").to("kilometer")); // "1.6093km"
91+
const { value, unit } = convert.value(1).from("mile").to("kilometer");
92+
console.log(value, unit);
9393
```

apps/docs/src/content/docs/conversions/number.mdx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,39 @@ The **Number** category supports conversions between number bases such as binary
66

77
## Supported Bases
88

9-
- binary (base 2)
10-
- octal (base 8)
11-
- decimal (base 10)
12-
- hexadecimal (base 16)
9+
- binary `base 2`
10+
- octal `base 8`
11+
- decimal `base 10`
12+
- hexadecimal `base 16`
1313

1414

1515
## Quick Example
16-
```ts
17-
console.log(convert.value(255).from("decimal").to("hexadecimal")); // "ff"
1816

19-
console.log(convert.value("1010").from("binary").to("decimal")); // "10"
17+
```ts twoslash
18+
import { Conversion } from "@devhaven/unit-conversion";
19+
20+
const convert = new Conversion();
21+
22+
convert.value(255).from("decimal").to("hexadecimal")
23+
convert.value(1010).from("binary").to("decimal")
2024
```
2125

2226
## Common Conversions
2327

2428
### Binary ↔ Decimal
2529
```ts
26-
convert.value(10).from("decimal").to("binary"); // "1010"
27-
convert.value(10).from("binary").to("decimal"); // "1010"
30+
convert.value(10).from("decimal").to("binary");
31+
convert.value(10).from("binary").to("decimal");
2832
```
2933

3034
### Decimal ↔ Hexadecimal
3135
```ts
32-
convert.value("a").from("hexadecimal").to("decimal"); // "10"
33-
convert.value(15).from("decimal").to("hexadecimal"); // "f"
36+
convert.value("a").from("hexadecimal").to("decimal");
37+
convert.value(15).from("decimal").to("hexadecimal");
3438
```
3539

3640
### Octal ↔ Decimal
3741
```ts
38-
convert.value(77).from("octal").to("decimal"); // "63"
39-
convert.value(77).from("decimal").to("octal"); // "115"
42+
convert.value(77).from("octal").to("decimal");
43+
convert.value(77).from("decimal").to("octal");
4044
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: "Pressure"
3+
template: doc
4+
---
5+
6+
The Pressure category supports conversions between Pascal, Kilopascal, Bar, PSI, and standard atmosphere.
7+
8+
## Supported Units
9+
10+
- Pascal `Pa`
11+
- Kilopascal `kPa`
12+
- Bar `bar`
13+
- PSI `psi`
14+
- Standard Atmosphere `atm`
15+
16+
## Quick Example
17+
18+
```ts
19+
import { Conversion } from "@devhaven/unit-conversion";
20+
21+
const convert = new Conversion();
22+
23+
convert.value(100).from("pascal").to("kilopascal");
24+
convert.value(1).from("kilopascal").to("pascal");
25+
```
26+
27+
## Common Conversions
28+
29+
### Pascal ↔ Bar
30+
```ts
31+
convert.value(1).from("pascal").to("bar");
32+
convert.value(1).from("bar").to("pascal");
33+
```
34+
35+
### Kilopascal ↔ PSI
36+
```ts
37+
convert.value(1).from("kilopascal").to("psi");
38+
convert.value(1).from("psi").to("kilopascal");
39+
```
40+
41+
### Bar ↔ PSI
42+
```ts
43+
convert.value(1).from("bar").to("psi");
44+
convert.value(1).from("psi").to("bar");
45+
```

apps/docs/src/content/docs/conversions/temperature.mdx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,32 @@ The Temperature category supports conversions between Celsius, Fahrenheit, and K
77

88
## Supported Units
99

10-
- celsius (°C)
11-
- fahrenheit (°F)
12-
- kelvin (K)
10+
- celsius `°C`
11+
- fahrenheit `°F`
12+
- kelvin `K`
1313

1414
## Quick Example
1515

1616
```ts
17-
console.log(convert.value(0).from("celsius").to("fahrenheit")); // "32°F"
17+
import { Conversion } from "@devhaven/unit-conversion";
1818

19-
console.log(convert.value(212).from("fahrenheit").to("celsius")); // "100°C"
19+
const convert = new Conversion();
20+
21+
convert.value(0).from("celsius").to("fahrenheit");
22+
convert.value(212).from("fahrenheit").to("celsius");
2023
```
2124

2225
## Common Conversions
2326

2427

2528
### Celsius ↔ Kelvin
2629
```ts
27-
convert.value(100).from("celsius").to("kelvin"); // "373.15K"
28-
convert.value(0).from("kelvin").to("celsius"); // "-273.15°C"
30+
convert.value(100).from("celsius").to("kelvin");
31+
convert.value(0).from("kelvin").to("celsius");
2932
```
3033

3134
### Celsius ↔ Fahrenheit
3235
```ts
33-
convert.value(100).from("celsius").to("fahrenheit"); // "212°F"
34-
convert.value(32).from("fahrenheit").to("celsius"); // "0°C"
36+
convert.value(100).from("celsius").to("fahrenheit");
37+
convert.value(32).from("fahrenheit").to("celsius");
3538
```

apps/docs/src/content/docs/conversions/time.mdx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,42 @@ The **time** category provides conversions between milliseconds, seconds, minute
66

77
## Supported Units
88

9-
- millisecond (ms)
10-
- second (s)
11-
- minute (min)
12-
- hour (h)
13-
- day (d)
9+
- millisecond `ms`
10+
- second `s`
11+
- minute `min`
12+
- hour `h`
13+
- day `d`
1414

1515
## Quick Example
1616

1717
```ts twoslash
1818
import { Conversion } from "@devhaven/unit-conversion";
1919

2020
const convert = new Conversion();
21-
console.log(convert.value(60000).from("millisecond").to("minute")); // "1min"
22-
console.log(convert.value(2).from("hour").to("minute")); // "120min"
21+
22+
convert.value(60000).from("millisecond").to("minute");
23+
convert.value(2).from("hour").to("minute");
2324
```
2425

2526
## Common Conversions
2627

2728
### Day ↔ Hour
2829

2930
```ts
30-
convert.value(1).from("day").to("hour"); // "24h"
31-
convert.value(24).from("hour").to("day"); // "1d"
31+
convert.value(1).from("day").to("hour");
32+
convert.value(24).from("hour").to("day");
3233
```
3334

3435
### Second ↔ Hour
3536

3637
```ts
37-
convert.value(3600).from("second").to("hour"); // "1h"
38-
convert.value(1).from("hour").to("second"); // "3600s"
38+
convert.value(3600).from("second").to("hour");
39+
convert.value(1).from("hour").to("second");
3940
```
4041

4142
### Millisecond ↔ Second
4243

4344
```ts
44-
convert.value(1000).from("millisecond").to("second"); // "1s"
45-
convert.value(1).from("second").to("millisecond"); // "1000ms"
45+
convert.value(1000).from("millisecond").to("second");
46+
convert.value(1).from("second").to("millisecond");
4647
```

0 commit comments

Comments
 (0)