Skip to content

Commit 32e30c5

Browse files
committed
Merge branch 'feat/energy-computer' into chore/all-my-stuffs
# Conflicts: # src/tools/index.ts
2 parents 365fb4b + d191b69 commit 32e30c5

File tree

6 files changed

+101
-2
lines changed

6 files changed

+101
-2
lines changed

pnpm-lock.yaml

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { computeCost } from './energy-computer.service'; // Adjust the import path as needed
4+
5+
describe('computeCost', () => {
6+
it('should calculate the correct cost for valid inputs', () => {
7+
const wattage = 1000; // 1000 watts = 1 kW
8+
const duration = 5; // 5 hours
9+
const kWhCost = 0.12; // $0.12 per kWh
10+
const result = computeCost(wattage, duration, kWhCost);
11+
expect(result).toBeCloseTo(0.60); // 1 kW * 5h * 0.12 = 0.60
12+
});
13+
14+
it('should return 0 when the duration is 0', () => {
15+
const wattage = 1000;
16+
const duration = 0;
17+
const kWhCost = 0.12;
18+
const result = computeCost(wattage, duration, kWhCost);
19+
expect(result).toBe(0);
20+
});
21+
22+
it('should return 0 when the wattage is 0', () => {
23+
const wattage = 0;
24+
const duration = 5;
25+
const kWhCost = 0.12;
26+
const result = computeCost(wattage, duration, kWhCost);
27+
expect(result).toBe(0);
28+
});
29+
30+
it('should return 0 when the cost per kWh is 0', () => {
31+
const wattage = 1000;
32+
const duration = 5;
33+
const kWhCost = 0;
34+
const result = computeCost(wattage, duration, kWhCost);
35+
expect(result).toBe(0);
36+
});
37+
38+
it('should handle fractional wattage and duration correctly', () => {
39+
const wattage = 750; // 0.75 kW
40+
const duration = 2.5; // 2.5 hours
41+
const kWhCost = 0.10; // $0.10 per kWh
42+
const result = computeCost(wattage, duration, kWhCost);
43+
expect(result).toBeCloseTo(0.1875); // 0.75 kW * 2.5h * 0.10 = 0.1875
44+
});
45+
46+
it('should handle large numbers correctly', () => {
47+
const wattage = 1000000; // 1 MW
48+
const duration = 24; // 24 hours
49+
const kWhCost = 0.15; // $0.15 per kWh
50+
const result = computeCost(wattage, duration, kWhCost);
51+
expect(result).toBe(3600); // 1000 kW * 24h * 0.15 = 3600
52+
});
53+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function computeCost(wattage: number, durationHours: number, costPerKWh: number): number {
2+
const kilowatts = wattage / 1000;
3+
const energyConsumed = kilowatts * durationHours;
4+
const totalCost = energyConsumed * costPerKWh;
5+
return totalCost;
6+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<script setup lang="ts">
2+
import { computeCost } from './energy-computer.service';
3+
4+
const wattage = ref(100);
5+
const durationHours = ref(2);
6+
const kWhCost = ref(0.1);
7+
const totalCost = computed(() => computeCost(wattage.value, durationHours.value, kWhCost.value));
8+
</script>
9+
10+
<template>
11+
<div>
12+
<n-form-item label="Device Wattage" mb-1>
13+
<n-input-number v-model:value="wattage" :min="0" />
14+
</n-form-item>
15+
<n-form-item label="Usage Duration (hours)" mb-1>
16+
<n-input-number v-model:value="durationHours" :min="0" />
17+
</n-form-item>
18+
<n-form-item label="kWh Cost" mb-1>
19+
<n-input-number v-model:value="kWhCost" :min="0" />
20+
</n-form-item>
21+
22+
<n-divider />
23+
24+
<input-copyable label="Total Cost" :value="totalCost" />
25+
</div>
26+
</template>

src/tools/energy-computer/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Engine } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Energy Consumption and Expense Computer',
6+
path: '/energy-computer',
7+
description: 'Compute energy consumption and expense',
8+
keywords: ['energy', 'expense', 'watt', 'kwh', 'computer'],
9+
component: () => import('./energy-computer.vue'),
10+
icon: Engine,
11+
createdAt: new Date('2024-08-15'),
12+
});

src/tools/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import { tool as weekNumberConverter } from './week-number-converter';
5454
import { tool as wpaPskGenerator } from './wpa-psk-generator';
5555
import { tool as xpathTester } from './xpath-tester';
5656
import { tool as xsltTester } from './xslt-tester';
57+
import { tool as energyComputer } from './energy-computer';
5758

5859
import { tool as cssXpathConverter } from './css-xpath-converter';
5960
import { tool as cssSelectorsMemo } from './css-selectors-memo';
@@ -443,6 +444,8 @@ export const toolsByCategory: ToolCategory[] = [
443444
energyConverter,
444445
powerConverter,
445446
benchmarkBuilder,
447+
benchmarkBuilder,
448+
energyComputer,
446449
],
447450
},
448451
{

0 commit comments

Comments
 (0)