Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(new tool); Energy Expense Computer #1372

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/tools/energy-computer/energy-computer.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, expect, it } from 'vitest';

import { computeCost } from './energy-computer.service'; // Adjust the import path as needed

describe('computeCost', () => {
it('should calculate the correct cost for valid inputs', () => {
const wattage = 1000; // 1000 watts = 1 kW
const duration = 5; // 5 hours
const kWhCost = 0.12; // $0.12 per kWh
const result = computeCost(wattage, duration, kWhCost);
expect(result).toBeCloseTo(0.60); // 1 kW * 5h * 0.12 = 0.60
});

it('should return 0 when the duration is 0', () => {
const wattage = 1000;
const duration = 0;
const kWhCost = 0.12;
const result = computeCost(wattage, duration, kWhCost);
expect(result).toBe(0);
});

it('should return 0 when the wattage is 0', () => {
const wattage = 0;
const duration = 5;
const kWhCost = 0.12;
const result = computeCost(wattage, duration, kWhCost);
expect(result).toBe(0);
});

it('should return 0 when the cost per kWh is 0', () => {
const wattage = 1000;
const duration = 5;
const kWhCost = 0;
const result = computeCost(wattage, duration, kWhCost);
expect(result).toBe(0);
});

it('should handle fractional wattage and duration correctly', () => {
const wattage = 750; // 0.75 kW
const duration = 2.5; // 2.5 hours
const kWhCost = 0.10; // $0.10 per kWh
const result = computeCost(wattage, duration, kWhCost);
expect(result).toBeCloseTo(0.1875); // 0.75 kW * 2.5h * 0.10 = 0.1875
});

it('should handle large numbers correctly', () => {
const wattage = 1000000; // 1 MW
const duration = 24; // 24 hours
const kWhCost = 0.15; // $0.15 per kWh
const result = computeCost(wattage, duration, kWhCost);
expect(result).toBe(3600); // 1000 kW * 24h * 0.15 = 3600
});
});
6 changes: 6 additions & 0 deletions src/tools/energy-computer/energy-computer.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function computeCost(wattage: number, durationHours: number, costPerKWh: number): number {
const kilowatts = wattage / 1000;
const energyConsumed = kilowatts * durationHours;
const totalCost = energyConsumed * costPerKWh;
return totalCost;
}
26 changes: 26 additions & 0 deletions src/tools/energy-computer/energy-computer.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<script setup lang="ts">
import { computeCost } from './energy-computer.service';

const wattage = ref(100);
const durationHours = ref(2);
const kWhCost = ref(0.1);
const totalCost = computed(() => computeCost(wattage.value, durationHours.value, kWhCost.value));
</script>

<template>
<div>
<n-form-item label="Device Wattage" mb-1>
<n-input-number v-model:value="wattage" :min="0" />
</n-form-item>
<n-form-item label="Usage Duration (hours)" mb-1>
<n-input-number v-model:value="durationHours" :min="0" />
</n-form-item>
<n-form-item label="kWh Cost" mb-1>
<n-input-number v-model:value="kWhCost" :min="0" />
</n-form-item>

<n-divider />

<input-copyable label="Total Cost" :value="totalCost" />
</div>
</template>
12 changes: 12 additions & 0 deletions src/tools/energy-computer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Engine } from '@vicons/tabler';
import { defineTool } from '../tool';

export const tool = defineTool({
name: 'Energy Consumption and Expense Computer',
path: '/energy-computer',
description: 'Compute energy consumption and expense',
keywords: ['energy', 'expense', 'watt', 'kwh', 'computer'],
component: () => import('./energy-computer.vue'),
icon: Engine,
createdAt: new Date('2024-08-15'),
});
8 changes: 7 additions & 1 deletion src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { tool as base64FileConverter } from './base64-file-converter';
import { tool as base64StringConverter } from './base64-string-converter';
import { tool as basicAuthGenerator } from './basic-auth-generator';
import { tool as emailNormalizer } from './email-normalizer';
import { tool as energyComputer } from './energy-computer';

import { tool as asciiTextDrawer } from './ascii-text-drawer';

Expand Down Expand Up @@ -172,7 +173,12 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Measurement',
components: [chronometer, temperatureConverter, benchmarkBuilder],
components: [
chronometer,
temperatureConverter,
benchmarkBuilder,
energyComputer,
],
},
{
name: 'Text',
Expand Down
Loading