Compare commits

...
Author SHA1 Message Date
renovate[bot] ec117e6f6f chore(deps): update dependency typescript to v7 2026-11-20 23:31:26 +00:00
ShareVB d07563cf5e fix: update nginx for CVE
Fix #600
2026-11-19 09:59:15 +01:00
ShareVB cf6df4e9f3 ci: fix e2e 2026-11-15 23:06:50 +01:00
JoAdo07 91ee2ab7a8 fix(percentage-calculator): clear the result when an input is cleared
n-input-number emits null when its text is cleared, while the computeds only
treated undefined as a missing value. The arithmetic then coerced null to 0,
so emptying To showed -100 and emptying X showed 0.

The three formulas moved into a service that treats null and undefined alike,
with unit tests for the cleared cases and an e2e case that clears a filled
field.

Fixes #1906
2026-11-15 23:03:42 +01:00
9 changed files with 533 additions and 232 deletions

No files matched your search

+1 -1
View File
@@ -26,7 +26,7 @@ RUN pnpm build \
-exec gzip -9 -n -k {} +
# production stage
FROM nginxinc/nginx-unprivileged:stable-alpine@sha256:f5116ed4606f3eb015dd4a8e28cb9b351476dc0ad4bc6e9f93b19cec130cdf03 AS production-stage
FROM nginx/nginx-unprivileged:alpine AS production-stage
LABEL maintainer="ShareVB <sharevb@gmail.com>" \
org.opencontainers.image.authors="ShareVB <sharevb@gmail.com>"
+1 -1
View File
@@ -469,7 +469,7 @@
"oxlint-tsgolint": "^7.0.2001",
"rollup-plugin-visualizer": "^7.0.1",
"semver": "^7.8.5",
"typescript": "~6.0.3",
"typescript": "~7.0.0",
"unocss": "^66.7.4",
"unocss-preset-scrollbar": "^4.0.0",
"unplugin-icons": "^23.0.1",
+409 -208
View File
File diff suppressed because it is too large. Load diff
@@ -58,7 +58,7 @@ test.describe('Tool - Paste image', () => {
});
test('Has title', async ({ page }) => {
await expect(page).toHaveTitle('Paste image - Toolbox');
await expect(page).toHaveTitle('Paste Image - IT Tools');
});
test('A pasted image is added to the history and can be downloaded', async ({ page }) => {
@@ -33,4 +33,22 @@ test.describe('Tool - Percentage calculator', () => {
await page.getByTestId('numberFrom').locator('input').pressSequentially('123\n');
await expect(page.getByTestId('percentageIncreaseDecrease').locator('input')).toHaveValue('');
});
test('Displays empty results after an input is cleared', async ({ page }) => {
await page.getByTestId('percentageX').locator('input').pressSequentially('40\n');
await page.getByTestId('percentageY').locator('input').pressSequentially('50\n');
await expect(page.getByTestId('percentageResult').locator('input')).toHaveValue('20');
await page.getByTestId('numberFrom').locator('input').pressSequentially('40\n');
await page.getByTestId('numberTo').locator('input').pressSequentially('46\n');
await expect(page.getByTestId('percentageIncreaseDecrease').locator('input')).toHaveValue('15');
await page.getByTestId('percentageY').locator('input').clear();
await page.getByTestId('numberTo').locator('input').clear();
await page.getByTestId('percentageX').locator('input').clear();
await page.getByTestId('numberFrom').locator('input').clear();
await expect(page.getByTestId('percentageResult').locator('input')).toHaveValue('');
await expect(page.getByTestId('percentageIncreaseDecrease').locator('input')).toHaveValue('');
});
});
@@ -0,0 +1,58 @@
import { describe, expect, it } from 'vitest';
import {
computeIsWhatPercentOf,
computePercentageIncreaseDecrease,
computePercentageOf,
} from './percentage-calculator.service';
describe('percentage-calculator', () => {
describe('computePercentageOf', () => {
it('computes a percentage of a value', () => {
expect(computePercentageOf(123, 456)).toBe('560.88');
expect(computePercentageOf(0, 200)).toBe('0');
});
it('returns an empty result when an input is missing', () => {
expect(computePercentageOf(undefined, 200)).toBe('');
expect(computePercentageOf(200, undefined)).toBe('');
expect(computePercentageOf(null, 200)).toBe('');
expect(computePercentageOf(200, null)).toBe('');
});
});
describe('computeIsWhatPercentOf', () => {
it('computes what percent x is of y', () => {
expect(computeIsWhatPercentOf(123, 456)).toBe('26.973684210526315');
});
it('returns an empty result when an input is missing', () => {
expect(computeIsWhatPercentOf(undefined, 12)).toBe('');
expect(computeIsWhatPercentOf(12, undefined)).toBe('');
expect(computeIsWhatPercentOf(null, 12)).toBe('');
expect(computeIsWhatPercentOf(12, null)).toBe('');
});
it('returns an empty result when the divisor is zero', () => {
expect(computeIsWhatPercentOf(12, 0)).toBe('');
});
});
describe('computePercentageIncreaseDecrease', () => {
it('computes the increase and the decrease', () => {
expect(computePercentageIncreaseDecrease(123, 456)).toBe('270.7317073170732');
expect(computePercentageIncreaseDecrease(40, 46)).toBe('15');
expect(computePercentageIncreaseDecrease(46, 40)).toBe('-13.043478260869565');
});
it('returns an empty result when an input is missing', () => {
expect(computePercentageIncreaseDecrease(undefined, 46)).toBe('');
expect(computePercentageIncreaseDecrease(40, undefined)).toBe('');
expect(computePercentageIncreaseDecrease(null, 46)).toBe('');
expect(computePercentageIncreaseDecrease(40, null)).toBe('');
});
it('returns an empty result when the reference value is zero', () => {
expect(computePercentageIncreaseDecrease(0, 46)).toBe('');
});
});
});
@@ -0,0 +1,35 @@
export type PercentageInput = number | null | undefined;
export { computePercentageOf, computeIsWhatPercentOf, computePercentageIncreaseDecrease };
function isMissing(value: PercentageInput): value is null | undefined {
return value === null || value === undefined;
}
function formatFinite(value: number): string {
return Number.isFinite(value) ? value.toString() : '';
}
function computePercentageOf(x: PercentageInput, y: PercentageInput): string {
if (isMissing(x) || isMissing(y)) {
return '';
}
return formatFinite((x / 100) * y);
}
function computeIsWhatPercentOf(x: PercentageInput, y: PercentageInput): string {
if (isMissing(x) || isMissing(y)) {
return '';
}
return formatFinite((100 * x) / y);
}
function computePercentageIncreaseDecrease(from: PercentageInput, to: PercentageInput): string {
if (isMissing(from) || isMissing(to)) {
return '';
}
return formatFinite(((to - from) / from) * 100);
}
@@ -1,35 +1,24 @@
<script setup lang="ts">
import { useI18n } from 'vue-i18n';
import {
computeIsWhatPercentOf,
computePercentageIncreaseDecrease,
computePercentageOf,
} from './percentage-calculator.service';
const { t } = useI18n();
const percentageX = ref();
const percentageY = ref();
const percentageResult = computed(() => {
if (percentageX.value === undefined || percentageY.value === undefined) {
return '';
}
return ((percentageX.value / 100) * percentageY.value).toString();
});
const percentageResult = computed(() => computePercentageOf(percentageX.value, percentageY.value));
const numberX = ref();
const numberY = ref();
const numberResult = computed(() => {
if (numberX.value === undefined || numberY.value === undefined) {
return '';
}
const result = (100 * numberX.value) / numberY.value;
return !Number.isFinite(result) || Number.isNaN(result) ? '' : result.toString();
});
const numberResult = computed(() => computeIsWhatPercentOf(numberX.value, numberY.value));
const numberFrom = ref();
const numberTo = ref();
const percentageIncreaseDecrease = computed(() => {
if (numberFrom.value === undefined || numberTo.value === undefined) {
return '';
}
const result = ((numberTo.value - numberFrom.value) / numberFrom.value) * 100;
return !Number.isFinite(result) || Number.isNaN(result) ? '' : result.toString();
});
const percentageIncreaseDecrease = computed(() => computePercentageIncreaseDecrease(numberFrom.value, numberTo.value));
</script>
<template>
@@ -32,7 +32,7 @@ test.describe('Tool - Whitespace cleaner', () => {
});
test('Has correct title', async ({ page }) => {
await expect(page).toHaveTitle('Whitespace cleaner - Toolbox');
await expect(page).toHaveTitle('Whitespace Cleaner - IT Tools');
});
test('U1 updates the output on input with no button press', async ({ page }) => {