Compare commits

..
Author SHA1 Message Date
ShareVB 49083d7154 fix: use String.raw in unit test 2025-01-01 17:18:14 +01:00
ShareVB 11c0ac7777 fix(regex tester): optional groups/captures failing
Fix #1388
2024-11-17 22:15:54 +01:00
9 changed files with 65 additions and 109 deletions

No files matched your search

+4 -1
View File
@@ -286,6 +286,9 @@
"watchTriggerable": true,
"watchWithFilter": true,
"whenever": true,
"toValue": true
"toValue": true,
"injectLocal": true,
"provideLocal": true,
"useClipboardItems": true
}
}
+9
View File
@@ -36,6 +36,7 @@ declare global {
const h: typeof import('vue')['h']
const ignorableWatch: typeof import('@vueuse/core')['ignorableWatch']
const inject: typeof import('vue')['inject']
const injectLocal: typeof import('@vueuse/core')['injectLocal']
const isDefined: typeof import('@vueuse/core')['isDefined']
const isProxy: typeof import('vue')['isProxy']
const isReactive: typeof import('vue')['isReactive']
@@ -65,6 +66,7 @@ declare global {
const onUpdated: typeof import('vue')['onUpdated']
const pausableWatch: typeof import('@vueuse/core')['pausableWatch']
const provide: typeof import('vue')['provide']
const provideLocal: typeof import('@vueuse/core')['provideLocal']
const reactify: typeof import('@vueuse/core')['reactify']
const reactifyObject: typeof import('@vueuse/core')['reactifyObject']
const reactive: typeof import('vue')['reactive']
@@ -128,6 +130,7 @@ declare global {
const useBrowserLocation: typeof import('@vueuse/core')['useBrowserLocation']
const useCached: typeof import('@vueuse/core')['useCached']
const useClipboard: typeof import('@vueuse/core')['useClipboard']
const useClipboardItems: typeof import('@vueuse/core')['useClipboardItems']
const useCloned: typeof import('@vueuse/core')['useCloned']
const useColorMode: typeof import('@vueuse/core')['useColorMode']
const useConfirmDialog: typeof import('@vueuse/core')['useConfirmDialog']
@@ -326,6 +329,7 @@ declare module 'vue' {
readonly h: UnwrapRef<typeof import('vue')['h']>
readonly ignorableWatch: UnwrapRef<typeof import('@vueuse/core')['ignorableWatch']>
readonly inject: UnwrapRef<typeof import('vue')['inject']>
readonly injectLocal: UnwrapRef<typeof import('@vueuse/core')['injectLocal']>
readonly isDefined: UnwrapRef<typeof import('@vueuse/core')['isDefined']>
readonly isProxy: UnwrapRef<typeof import('vue')['isProxy']>
readonly isReactive: UnwrapRef<typeof import('vue')['isReactive']>
@@ -355,6 +359,7 @@ declare module 'vue' {
readonly onUpdated: UnwrapRef<typeof import('vue')['onUpdated']>
readonly pausableWatch: UnwrapRef<typeof import('@vueuse/core')['pausableWatch']>
readonly provide: UnwrapRef<typeof import('vue')['provide']>
readonly provideLocal: UnwrapRef<typeof import('@vueuse/core')['provideLocal']>
readonly reactify: UnwrapRef<typeof import('@vueuse/core')['reactify']>
readonly reactifyObject: UnwrapRef<typeof import('@vueuse/core')['reactifyObject']>
readonly reactive: UnwrapRef<typeof import('vue')['reactive']>
@@ -418,6 +423,7 @@ declare module 'vue' {
readonly useBrowserLocation: UnwrapRef<typeof import('@vueuse/core')['useBrowserLocation']>
readonly useCached: UnwrapRef<typeof import('@vueuse/core')['useCached']>
readonly useClipboard: UnwrapRef<typeof import('@vueuse/core')['useClipboard']>
readonly useClipboardItems: UnwrapRef<typeof import('@vueuse/core')['useClipboardItems']>
readonly useCloned: UnwrapRef<typeof import('@vueuse/core')['useCloned']>
readonly useColorMode: UnwrapRef<typeof import('@vueuse/core')['useColorMode']>
readonly useConfirmDialog: UnwrapRef<typeof import('@vueuse/core')['useConfirmDialog']>
@@ -610,6 +616,7 @@ declare module '@vue/runtime-core' {
readonly h: UnwrapRef<typeof import('vue')['h']>
readonly ignorableWatch: UnwrapRef<typeof import('@vueuse/core')['ignorableWatch']>
readonly inject: UnwrapRef<typeof import('vue')['inject']>
readonly injectLocal: UnwrapRef<typeof import('@vueuse/core')['injectLocal']>
readonly isDefined: UnwrapRef<typeof import('@vueuse/core')['isDefined']>
readonly isProxy: UnwrapRef<typeof import('vue')['isProxy']>
readonly isReactive: UnwrapRef<typeof import('vue')['isReactive']>
@@ -639,6 +646,7 @@ declare module '@vue/runtime-core' {
readonly onUpdated: UnwrapRef<typeof import('vue')['onUpdated']>
readonly pausableWatch: UnwrapRef<typeof import('@vueuse/core')['pausableWatch']>
readonly provide: UnwrapRef<typeof import('vue')['provide']>
readonly provideLocal: UnwrapRef<typeof import('@vueuse/core')['provideLocal']>
readonly reactify: UnwrapRef<typeof import('@vueuse/core')['reactify']>
readonly reactifyObject: UnwrapRef<typeof import('@vueuse/core')['reactifyObject']>
readonly reactive: UnwrapRef<typeof import('vue')['reactive']>
@@ -702,6 +710,7 @@ declare module '@vue/runtime-core' {
readonly useBrowserLocation: UnwrapRef<typeof import('@vueuse/core')['useBrowserLocation']>
readonly useCached: UnwrapRef<typeof import('@vueuse/core')['useCached']>
readonly useClipboard: UnwrapRef<typeof import('@vueuse/core')['useClipboard']>
readonly useClipboardItems: UnwrapRef<typeof import('@vueuse/core')['useClipboardItems']>
readonly useCloned: UnwrapRef<typeof import('@vueuse/core')['useCloned']>
readonly useColorMode: UnwrapRef<typeof import('@vueuse/core')['useColorMode']>
readonly useConfirmDialog: UnwrapRef<typeof import('@vueuse/core')['useConfirmDialog']>
@@ -1,53 +0,0 @@
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
});
});
@@ -1,6 +0,0 @@
export function computeCost(wattage: number, durationHours: number, costPerKWh: number): number {
const kilowatts = wattage / 1000;
const energyConsumed = kilowatts * durationHours;
const totalCost = energyConsumed * costPerKWh;
return totalCost;
}
@@ -1,26 +0,0 @@
<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
View File
@@ -1,12 +0,0 @@
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'),
});
+1 -7
View File
@@ -2,7 +2,6 @@ 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';
@@ -173,12 +172,7 @@ export const toolsByCategory: ToolCategory[] = [
},
{
name: 'Measurement',
components: [
chronometer,
temperatureConverter,
benchmarkBuilder,
energyComputer,
],
components: [chronometer, temperatureConverter, benchmarkBuilder],
},
{
name: 'Text',
@@ -92,6 +92,51 @@ const regexesData = [
},
],
},
{
regex: String.raw`\s([^\s\[]+)(?:\[(\d+)\])?:\s`,
text: 'Nov 11 21:03:26 abc2 def.sh[1]: \nNov 11 21:03:26 abc2 def.sh: ',
flags: 'gm',
result: [
{
captures: [
{
end: 27,
name: '1',
start: 21,
value: 'def.sh',
},
{
end: 29,
name: '2',
start: 28,
value: '1',
},
],
groups: [],
index: 20,
value: ' def.sh[1]: ',
},
{
captures: [
{
end: 60,
name: '1',
start: 54,
value: 'def.sh',
},
{
end: -1,
name: '2',
start: -1,
value: undefined,
},
],
groups: [],
index: 53,
value: ' def.sh: ',
},
],
},
];
describe('regex-tester', () => {
@@ -31,21 +31,23 @@ export function matchRegex(regex: string, text: string, flags: string) {
const captures: Array<GroupCapture> = [];
Object.entries(match).forEach(([captureName, captureValue]) => {
if (captureName !== '0' && captureName.match(/\d+/)) {
const captureIndices = indices[Number(captureName)] || [-1, -1];
captures.push({
name: captureName,
value: captureValue,
start: indices[Number(captureName)][0],
end: indices[Number(captureName)][1],
start: captureIndices[0],
end: captureIndices[1],
});
}
});
const groups: Array<GroupCapture> = [];
Object.entries(match.groups || {}).forEach(([groupName, groupValue]) => {
const groupIndices = indices.groups[groupName] || [-1, -1];
groups.push({
name: groupName,
value: groupValue,
start: indices.groups[groupName][0],
end: indices.groups[groupName][1],
start: groupIndices[0],
end: groupIndices[1],
});
});
results.push({