Skip to content

Commit 975a863

Browse files
committed
Merge branch 'feat/units-converters' into chore/all-my-stuffs
# Conflicts: # package.json # pnpm-lock.yaml # src/tools/index.ts
2 parents d7ad5bd + c70c602 commit 975a863

File tree

25 files changed

+659
-0
lines changed

25 files changed

+659
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
"composerize": "^1.6.12",
112112
"countries-and-timezones": "^3.7.2",
113113
"countries-and-timezones": "^3.6.0",
114+
"convert": "^5.4.1",
114115
"country-code-lookup": "^0.1.0",
115116
"crc": "^4.3.2",
116117
"cron-validator": "^1.3.1",

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/UnitsConverter.vue

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<script setup lang="ts">
2+
import _ from 'lodash';
3+
import convert, { type Unit } from 'convert';
4+
5+
const props = withDefaults(defineProps<{
6+
supportedUnits: { [key: string]: string }
7+
defaultUnit: string
8+
labelWidth?: string
9+
unitMinWidth?: string
10+
}>(), {
11+
labelWidth: '150px',
12+
unitMinWidth: '50px',
13+
});
14+
const { supportedUnits, defaultUnit, labelWidth, unitMinWidth } = toRefs(props);
15+
const units = reactive<
16+
Record<
17+
string,
18+
{ title: string; unit: string; ref: number }
19+
>
20+
>(Object.entries(supportedUnits.value).map(([key, label]) => ({
21+
title: label,
22+
unit: key,
23+
ref: 1,
24+
})).reduce((prev, current) => ({
25+
...prev,
26+
[current.unit]: current,
27+
}), {}));
28+
29+
function update(key: string) {
30+
if (!units[key]) {
31+
return;
32+
}
33+
const { ref: value } = units[key];
34+
35+
const converter = convert(value, key as Unit);
36+
37+
_.chain(units)
38+
.omit(key)
39+
.forEach(({ unit }) => {
40+
try {
41+
units[unit].ref = converter.to(unit as Unit);
42+
}
43+
catch (e: any) {
44+
units[unit].ref = 0;
45+
}
46+
})
47+
.value();
48+
}
49+
50+
update(defaultUnit.value);
51+
</script>
52+
53+
<template>
54+
<div>
55+
<n-input-group v-for="[key, { title, unit }] in Object.entries(units)" :key="key" mb-3 w-full>
56+
<n-input-group-label :style="{ width: labelWidth }">
57+
{{ title }}
58+
</n-input-group-label>
59+
60+
<n-input-number
61+
v-model:value="units[key].ref"
62+
style="flex: 1"
63+
@update:value="() => update(key)"
64+
/>
65+
66+
<n-input-group-label :style="{ minWidth: unitMinWidth }">
67+
{{ unit }}
68+
</n-input-group-label>
69+
</n-input-group>
70+
</div>
71+
</template>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<script setup lang="ts">
2+
import UnitsConverter from '@/components/UnitsConverter.vue';
3+
4+
const supportedUnits = {
5+
deg: 'degree (°)',
6+
rad: 'radian',
7+
turn: 'turn',
8+
gradian: 'gradian',
9+
grad: 'grad',
10+
gon: 'gon',
11+
};
12+
</script>
13+
14+
<template>
15+
<UnitsConverter default-unit="deg" :supported-units="supportedUnits" label-width="150px" />
16+
</template>

src/tools/angle-converter/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Angle } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Angle Units Converter',
6+
path: '/angle-converter',
7+
description: 'Convert values between angle units',
8+
keywords: ['angle', 'converter'],
9+
component: () => import('./angle-converter.vue'),
10+
icon: Angle,
11+
createdAt: new Date('2024-08-15'),
12+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<script setup lang="ts">
2+
import UnitsConverter from '@/components/UnitsConverter.vue';
3+
4+
const supportedUnits = {
5+
'': 'square meter',
6+
'Pm²': 'square petameter',
7+
'Tm²': 'square terameter',
8+
'Gm²': 'square gigameter',
9+
'Mm²': 'square megameter',
10+
'km²': 'square kilometer',
11+
'hm²': 'square hectometer',
12+
'dam²': 'square decameter',
13+
'dm²': 'square decimeter',
14+
'cm²': 'square centimeter',
15+
'mm²': 'square millimeter',
16+
'μm²': 'square micrometer',
17+
'nm²': 'square nanometer',
18+
'pm²': 'square picometer',
19+
'fm²': 'square femtometer',
20+
'ac': 'acre',
21+
'ca': 'centiare',
22+
'da': 'deciare',
23+
'are': 'are',
24+
'daa': 'decare',
25+
'ha': 'hectare',
26+
'ft²': 'square foot (/ft2/sq ft)',
27+
'in²': 'square inch (/in2/sq in)',
28+
'yd²': 'square yard (yd2/sq yd)',
29+
'mi²': 'square mile (mi2/sq mi)',
30+
};
31+
</script>
32+
33+
<template>
34+
<UnitsConverter default-unit="" :supported-units="supportedUnits" label-width="150px" />
35+
</template>

src/tools/area-converter/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { SquaresDiagonal } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Area Units Converter',
6+
path: '/area-converter',
7+
description: 'Convert values between area units',
8+
keywords: ['area', 'converter'],
9+
component: () => import('./area-converter.vue'),
10+
icon: SquaresDiagonal,
11+
createdAt: new Date('2024-08-15'),
12+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<script setup lang="ts">
2+
import UnitsConverter from '@/components/UnitsConverter.vue';
3+
4+
const supportedUnits = {
5+
J: 'joule',
6+
PJ: 'petajoule',
7+
TJ: 'terajoule',
8+
GJ: 'gigajoule',
9+
MJ: 'megajoule',
10+
kJ: 'kilojoule',
11+
hJ: 'hectojoule',
12+
daJ: 'decajoule',
13+
dJ: 'decijoule',
14+
cJ: 'centijoule',
15+
mJ: 'millijoule',
16+
µJ: 'microjoule',
17+
nJ: 'nanojoule',
18+
pJ: 'picojoule',
19+
fJ: 'femtojoule',
20+
Wh: 'watt-hour',
21+
PWh: 'petawatt-hour',
22+
TWh: 'terawatt-hour',
23+
GWh: 'gigawatt-hour',
24+
MWh: 'megawatt-hour',
25+
kWh: 'kilowatt-hour',
26+
hWh: 'hectowatt-hour',
27+
daWh: 'decawatt-hour',
28+
dWh: 'deciwatt-hour',
29+
cWh: 'centiwatt-hour',
30+
mWh: 'milliwatt-hour',
31+
µWh: 'microwatt-hour',
32+
nWh: 'nanowatt-hour',
33+
pWh: 'picowatt-hour',
34+
fWh: 'femtowatt-hour',
35+
};
36+
</script>
37+
38+
<template>
39+
<UnitsConverter default-unit="J" :supported-units="supportedUnits" label-width="150px" />
40+
</template>

src/tools/energy-converter/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Power } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Energy Units Converter',
6+
path: '/energy-converter',
7+
description: 'Convert values between energy units',
8+
keywords: ['energy', 'converter'],
9+
component: () => import('./energy-converter.vue'),
10+
icon: Power,
11+
createdAt: new Date('2024-08-15'),
12+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<script setup lang="ts">
2+
import UnitsConverter from '@/components/UnitsConverter.vue';
3+
4+
const supportedUnits = {
5+
'N': 'newton',
6+
'PN': 'petanewton',
7+
'TN': 'teranewton',
8+
'GN': 'giganewton',
9+
'MN': 'meganewton',
10+
'kN': 'kilonewton',
11+
'hN': 'hectonewton',
12+
'daN': 'decanewton',
13+
'dN': 'decinewton',
14+
'cN': 'centinewton',
15+
'mN': 'millinewton',
16+
'µN': 'micronewton',
17+
'nN': 'nanonewton',
18+
'pN': 'piconewton',
19+
'fN': 'femtonewton',
20+
'dyn': 'dyne',
21+
'lbf': 'pound of force',
22+
'kip': 'kip',
23+
'klb': 'klb',
24+
'kipf': 'kipf',
25+
'klbf': 'klbf',
26+
'pdl': 'poundal',
27+
'kgf': 'kilogram-force',
28+
'kp': 'kilopond',
29+
'Mp': 'megapond',
30+
'tf': 'tonne-force',
31+
'metric tf': 'metric ton-force',
32+
'megagram-force': 'megagram-force',
33+
};
34+
</script>
35+
36+
<template>
37+
<UnitsConverter default-unit="N" :supported-units="supportedUnits" label-width="150px" />
38+
</template>

0 commit comments

Comments
 (0)